Solr与MySQL查询性能对比
本文简单对比下Solr与MySQL的查询性能速度。
测试数据量:10407608 Num Docs: 10407608
这里对MySQL的查询时间都包含了从MySQL Server获取数据的时间。
在项目中一个最常用的查询,查询某段时间内的数据,SQL查询获取数据,30s左右
SELECT * FROM `tf_hotspotdata_copy_test` WHERE collectTime BETWEEN '2014-12-06 00:00:00' AND '2014-12-10 21:31:55';
对collectTime建立索引后,同样的查询,2s,快了很多。
Solr索引数据:
<!--Index Field for HotSpot--><field name="CollectTime" type="tdate" indexed="true" stored="true"/><field name="IMSI" type="string" indexed="true" stored="true"/><field name="IMEI" type="string" indexed="true" stored="true"/><field name="DeviceID" type="string" indexed="true" stored="true"/>
Solr查询,同样的条件,72ms
"status": 0,
"QTime": 72,
"params": {
"indent": "true",
"q": "CollectTime:[2014-12-06T00:00:00.000Z TO 2014-12-10T21:31:55.000Z]",
"_": "1434617215202",
"wt": "json"
}
好吧,查询性能提高的不是一点点,用Solrj代码试试:
SolrQuery params = new SolrQuery();
params.set("q", timeQueryString);
params.set("fq", queryString);
params.set("start", 0);
params.set("rows", Integer.MAX_VALUE);
params.setFields(retKeys);
QueryResponse response = server.query(params);Solrj查询并获取结果集,结果集大小为220296,返回5个字段,时间为12s左右。
为什么需要这么长时间?上面的"QTime"只是根据索引查询的时间,如果要从solr服务端获取查询到的结果集,solr需要读取stored的字段(磁盘IO),再经过Http传输到本地(网络IO),这两者比较耗时,特别是磁盘IO。
时间对比:
查询条件 | 时间 |
MySQL(无索引) | 30s |
MySQL(有索引) | 2s |
Solrj(select查询) | 12s |
如何优化?看看只获取ID需要的时间:
SQL查询只返回id,没有对collectTime建索引,10s左右:
SELECT id FROM `tf_hotspotdata_copy_test` WHERE collectTime BETWEEN '2014-12-06 00:00:00' AND '2014-12-10 21:31:55';
SQL查询只返回id,同样的查询条件,对collectTime建索引,0.337s,很快。
Solrj查询只返回id,7s左右,快了一点。
id Size: 220296
Time: 7340
时间对比:
查询条件(只获取ID) | 时间 |
MySQL(无索引) | 10s |
MySQL(有索引) | 0.337s |
Solrj(select查询) | 7s |
继续优化。。
关于Solrj获取大量结果集速度慢的一些类似问题:
http://stackoverflow.com/questions/28181821/solr-performance#
http://grokbase.com/t/lucene/solr-user/11aysnde25/query-time-help
http://lucene.472066.n3.nabble.com/Solrj-performance-bottleneck-td2682797.html
这个问题没有好的解决方式,基本的建议都是做分页,但是我们需要拿到大量数据做一些比对分析,做分页没有意义。
偶然看到一个回答,solr默认的查询使用的是"/select" request handler,可以用"/export" request handler来export结果集,看看solr对它的说明:
It's possible to export fully sorted result sets using a special rank query parser and response writer specifically designed to work together to handle scenarios that involve sorting and exporting millions of records. This uses a stream sorting techniquethat begins to send records within milliseconds and continues to stream results until the entire result set has been sorted and exported.
Solr中已经定
原创不易,完成人机校验,阅读全文