Sqlite优化记录:使用全文索引加快检索速度-转

通过创建索引,SQLite数据库查询效率显著提升。实验显示,在10万条数据的表中,查询时间从550ms降低至8ms。文章提供了一个在Android环境下实现的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

凡是数据库中,索引的存在就是为了提高查询速度的,数据库的索引有点类似于书本上面的目录的概念,因为在英文中都是index,事实上也就是目录。

其算法应该叫做“倒排索引”,这个其实也类似于搜索引擎里面的基本算法。

测试:10w条数据,没有索引的情况下,查询一条数据大约需要550ms以上。

建立索引后,数据库的体积增大了3倍左右,但是同样的查询却减少到8ms的级别,提升了70倍

有时候关于sqlite数据库出错或者没法用的情况看这里

下面是在android手机上面的测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//创建数据库
SQLiteDatabase  database = SQLiteDatabase.openOrCreateDatabase(databaseFile,   "test123" null );
database.execSQL( "create table if not exists t1(a,b)" );
//创建索引
database.execSQL( "create index if not exists ia on t1(a,b)" );
//插入10w条数据
for  ( int  i =  0 ; i <  100000 ; i++) {
     database.execSQL( "insert into t1(a, b) values(?, ?)" , new  String[] {  ""  + i,  "name"  + Math.random() * i });
}
//查询一条数据
Log.v( "test" , "开始查询" );
             Cursor cursor = database.rawQuery( "select * from t1 where a='88980'" , null );
             while (cursor.moveToNext()){
                 String a = cursor.getString(cursor.getColumnIndex( "a" ));
                 String b = cursor.getString(cursor.getColumnIndex( "b" ));
                 Log.v( "test" , "查找结果---------->"  "a: " +a+ "  " + "b: " +b);
             }
             Log.v( "test" "查找结束" );

本文地址http://tweetyf.org/2012/06/sqlite_optimization_using_index.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值