case study building recommender system inside database adding an index to the table now you must add indices to speed up retrieval as needed in real-time system this is shown in the next listing listing creating an index create function to easily create indices indices will quicken retrieval def createindex(columncursor)sql 'create index % on movie_comparison (% );(columncolumncursor execute(sqlcreateindex('bucket ',cursorcreateindex('bucket ',cursorcreateindex('bucket ',cursorput index on bit buckets with the data indexed we can now move on to the "model building part in this case study no actual machine learning or statistical model is implemented instead we'll use far simpler techniquestring distance calculation two strings can be compared using the hamming distance as explained earlier in the theoretical intro to the case study step model building to use the hamming distance in the database we need to define it as function creating the hamming distance function we implement this as user-defined function this function can calculate the distance for -bit integer (actually * )as shown in the following listing listing creating the hamming distance sql ''create function hammingdistancea biginta biginta biginta bigintb bigintb bigintb bigintb bigint returns int deterministic return bit_count( bit_count( bit_count( bit_count( )''define function it takes input arguments strings of length for the first customer and another strings of length for the second customer this way we can compare customers side-by-side for movies cursor execute(sqlthis runs the query sql '''select hammingdistanceb' ', ' ', ' ', ' , ' ', ' ', ' ', ' )''pd read_sql(sql,mcthe function is stored in database you can only do this oncerunning this code second time will result in an erroroperationalerror( 'function hammingdistance already exists'to check this function you can run this sql statement with fixed strings notice the "bbefore each stringindicating that you're passing bit values the outcome of this particular test should be which indicates the series of strings differ in only places