字符串由8-32位(随机)的0-9(随机)组成,数据量10000000行,然后做自动补全(只要10行最新的数据)。任意关键字(数字、英文、中文)查询,接口响应速度在超过200ms不及格, 100-200ms普通, 100ms以下优秀。
备注:自动补全功能参考百度搜索。最简单实现的sql如下:
--注意这个sql达不到要求,只是要求完成类似的功能
select id from table where name like '%aa%' order by id desc limit 10;
1 使用limit查询
1.1 值存在且数量较多,查询较快
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('139') order by objectid desc limit 10;
1.2 值存在返回的数量较少,查询较慢
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('YNHX') order by objectid desc limit 10;
1.2 值不存在,查询较慢
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('中国') order by objectid desc limit 10;
2 不使用limit查询
2.1 值存在且返回的数量较多,查询较慢
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('139');
2.2 值存在返回的数量较少,查询较快
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('YNHX');
2.3 值不存在,查询较快
explain (analyze,verbose,costs,buffers,timing)
select objectid from cond.enterprises where keys@@toTsquery('中国');
3 存在的问题
根据第1节和第2节的查询情况,它们完全是相互矛盾的,运行效率完全相反:
- 当使用limit时,如果关键字返回的行数比较多时,limit可有效的提升查询速度;
- 不使用limit时,情况正好和使用limit相反;