SQLite optimized records: Indexing speeds up queries

Source: Internet
Author: User
Tags sqlite database

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
01 //创建数据库
02 SQLiteDatabase  database = SQLiteDatabase.openOrCreateDatabase(databaseFile,  "test123", null);
03 database.execSQL("create table if not exists t1(a,b)");
04 //创建索引
05 database.execSQL("create index if not exists ia on t1(a,b)");
06 //插入10w条数据
07 for(inti = 0; i < 100000; i++) {
08     database.execSQL("insert into t1(a, b) values(?, ?)",newString[] { "" + i, "name"+ Math.random() * i });
09 }
10 //查询一条数据
11 Log.v("test","开始查询");
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);
17             }
18             Log.v("test", "查找结束");

This address http://tweetyf.org/2012/06/sqlite_optimization_using_index.html

SQLite optimized records: Indexing speeds up queries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.