Sqlite查询优化技巧——将LIKE语句转换为比较语句 -转
2016-01-17 12:46
393 查看
项目中用到了数据库,其中一个表有33万条数据。对这个表进行查询,执行类似“SELECT * FROM table1 where ds_key like 'asd%%'”这样的语句,在我的Touch上用了大概11秒,体验很差。
调查发现,虽然已经对table1增加了索引,但是在实际执行的时候,如果查询条件包含LIKE,就不会使用索引。
想知道查询是不是用了索引,可以参考http://www.sqlite.org/eqp.html这个链接提供的方法进行验证。
而改成“SELECT * FROM table1 where ds_key >= 'asd' and ds_key < 'ase'”这样的语句,查询时就会使用到索引,花费时间只有零点几秒,效率提高了几十倍。
当然,这只针对查询以某个字符串开头的记录的情况,而对于like '%%asd%%'这样的就没法优化了。
下面的问题就是如何获得输入关键词的“下一个比它大”的词了。
考虑到字段中还包含中文,最终我写了这样一段代码:
复制代码
//获取字符串的下一个比它大的字符串
- (NSString*)nextStringAfterString:(NSString*)text
{
NSString *nextString = nil;
int length = text.length;
NSString *prefixString = [text substringToIndex:length - 1];
NSString *lastString = [text substringFromIndex:length - 1];
const char* lastChar = [lastString UTF8String];
int lastLength = strlen(lastChar);
if (lastLength == 1) {//ASCII
char newChar1[2];
newChar1[0] = lastChar[0] + 1;
newChar1[1] = 0;
NSString *newString1 = [NSString stringWithUTF8String:newChar1];
nextString = [prefixString stringByAppendingString:newString1];
}
else if (lastLength == 3) {//中文
char newChar2[4];
newChar2[0] = lastChar[0];
newChar2[1] = lastChar[1];
newChar2[2] = lastChar[2] + 1;
newChar2[3] = 0;
NSString *newString2 = [NSString stringWithUTF8String:newChar2];
nextString = [prefixString stringByAppendingString:newString2];
}
return nextString;
}
如果返回的是nil,还是使用LIKE查询,不过这种情况貌似不会出现。
调查发现,虽然已经对table1增加了索引,但是在实际执行的时候,如果查询条件包含LIKE,就不会使用索引。
想知道查询是不是用了索引,可以参考http://www.sqlite.org/eqp.html这个链接提供的方法进行验证。
而改成“SELECT * FROM table1 where ds_key >= 'asd' and ds_key < 'ase'”这样的语句,查询时就会使用到索引,花费时间只有零点几秒,效率提高了几十倍。
当然,这只针对查询以某个字符串开头的记录的情况,而对于like '%%asd%%'这样的就没法优化了。
下面的问题就是如何获得输入关键词的“下一个比它大”的词了。
考虑到字段中还包含中文,最终我写了这样一段代码:
复制代码
//获取字符串的下一个比它大的字符串
- (NSString*)nextStringAfterString:(NSString*)text
{
NSString *nextString = nil;
int length = text.length;
NSString *prefixString = [text substringToIndex:length - 1];
NSString *lastString = [text substringFromIndex:length - 1];
const char* lastChar = [lastString UTF8String];
int lastLength = strlen(lastChar);
if (lastLength == 1) {//ASCII
char newChar1[2];
newChar1[0] = lastChar[0] + 1;
newChar1[1] = 0;
NSString *newString1 = [NSString stringWithUTF8String:newChar1];
nextString = [prefixString stringByAppendingString:newString1];
}
else if (lastLength == 3) {//中文
char newChar2[4];
newChar2[0] = lastChar[0];
newChar2[1] = lastChar[1];
newChar2[2] = lastChar[2] + 1;
newChar2[3] = 0;
NSString *newString2 = [NSString stringWithUTF8String:newChar2];
nextString = [prefixString stringByAppendingString:newString2];
}
return nextString;
}
如果返回的是nil,还是使用LIKE查询,不过这种情况貌似不会出现。
相关文章推荐
- SQLite学习手册(索引和数据分析/清理)-转
- Sqlite优化记录:使用全文索引加快检索速度-转
- Sqlite优化记录:使用全文索引加快检索速度-转
- 在mac上安装mysql5.7.10
- sqlserver中文件组Filegroup的使用
- sqlite 按拼音排序
- 关于sqlite断电或者存储空间不足造成的数据文件损坏问题-转
- 数据库的基本操作
- mysql之临时表【整理】
- sqlite 交叉编译-转
- sqlite配置选项
- Activiti工作流(二)——入门Demo及数据库
- 数据库一次性插入10w条数据,怎么插入效率快
- mongodb数据备份
- ORACLE 更新关联多张表
- redis的安装以及常见的使用
- SQL IF while 游标
- Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0.jar的问题
- 通过jdbc实现对数据库中数据的增删查改操作
- 数据库事物之冲突可串行化、优先图、视图等价和视图可串行化