In general database, the existence of the index is to improve the query speed, the database index is somewhat similar to the concept of the table above the book, because in English is index, in fact, is the directory.
The algorithm should be called "inverted Index", which is actually similar to the basic algorithm inside the search engine.
Test: 10w data, without an index, it takes about 550ms to query a piece of data.
After indexing, the size of the database increased by about 3 times times, but the same query was reduced to a 8ms level, up 70 times times
Sometimes there's something wrong with the SQLite database or it doesn't work.
Below is the test code on the Android phone
View Source code Printing Help
02 |
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123" , null ); |
03 |
database.execSQL( "create table if not exists t1(a,b)" ); |
05 |
database.execSQL( "create index if not exists ia on t1(a,b)" ); |
07 |
for ( int i = 0 ; i < 100000 ; i++) { |
08 |
database.execSQL( "insert into t1(a, b) values(?, ?)" , new String[] { "" + i, "name" + Math.random() * i }); |
12 |
Cursor cursor = database.rawQuery( "select * from t1 where a=‘88980‘" , null ); |
13 |
while (cursor.moveToNext()){ |
14 |
String a = cursor.getString(cursor.getColumnIndex( "a" )); |
all |
; string B = cursor.getstring (Cursor.getcolumnindex (
|
16 |
Log.v( "test" , "查找结果---------->" + "a: " +a+ " " + "b: " +b); |
18 |
Log.v( "test" , "查找结束" ); |
This address http://tweetyf.org/2012/06/sqlite_optimization_using_index.html
SQLite optimized records: Indexing speeds up queries