1 # ! /Usr/bin/ENV Python 2 3 ''' 4 Genearte the SQLite DB 5 ''' 6 Import Time 7 Import Apsw 8 9 10 Loop_insert = 10000 11 Loops _search = 1000 12 13 Def Gendb (): 14 Loop_time = Loop_insert 15 DB = apsw. Connection ( " : Memory: " ) 16 Cursor =DB. cursor () 17 Cursor.exe cute ( " Create Table big (itemall string ); " ) 18 Cursor.exe cute ( " Begin; " ) 19 For I In Xrange (loop_time ): 20 Cursor.exe cute ( " Insert into big (itemall) values (% 010d ); " % I) 21 22 Cursor.exe cute ( " Commit; " ) 23 24 Cursor.exe cute ( " Create Table small (idx int primary key, V1 int, V2 char ); " ) 25 Cursor.exe cute ( " Begin; " ) 26 For I In Xrange (loop_time ): 27 Cursor.exe cute ( " Insert into small (V1, V2) values (% d, % 010d ); " %(I, I )) 28 29 Cursor.exe cute ( " Commit; " ) 30 31 Return DB 32 33 34 Def Test1 (db ): 35 C =DB. cursor () 36 Start = Time. Time () 37 For I In Xrange (loop_search ): 38 C2 = c.exe cute ( " Select * from big where itemall like '% 010d' limit 1; " % I) 39 Values = C2.fetchall () 40 If Len (values)> 0: 41 Pass 42 End = Time. Time () 43 44 Print " Duration test1 " , End- Start 45 46 47 Def Test2 (db ): 48 C = DB. cursor () 49 Start = Time. Time () 50 For I In Xrange (loop_search ): 51 C2 = c.exe cute ( " Select * from small where V1 = '% 010d' and v2 = % 10d limit 1; " % (I, I )) 52 Values = C2.fetchall () 53 If Len (values)> 0: 54 Pass 55 End = Time. Time () 56 57 Print " Duration Test2 " , End- Start 58 59 60 Def Main (): 61 DB = Gendb () 62 Test1 (db) 63 Test2 (db) 64 65 66 If _ Name __ = ' _ Main __ ' : 67 Main ()