【发布时间】:2015-05-23 20:53:35
【问题描述】:
SELECT SQL_NO_CACHE link.stop, stop.common_name, locality.name, stop.bearing, stop.latitude, stop.longitude
FROM service
JOIN pattern ON pattern.service = service.code
JOIN link ON link.section = pattern.section
JOIN naptan.stop ON stop.atco_code = link.stop
JOIN naptan.locality ON locality.code = stop.nptg_locality_ref
GROUP BY link.stop
上述查询大约需要 800 毫秒 - 1000 毫秒才能运行。
如果我附加 group_concat 语句,则查询需要 8 - 10 秒:
SELECT SQL_NO_CACHE link.stop, link.stop, stop.common_name, locality.name, stop.bearing, stop.latitude, stop.longitude, group_concat(service.line) lines
如何更改此查询,使其使用group_concat 语句在 2 秒内运行?
SQL 小提琴:http://sqlfiddle.com/#!9/414fe
EXPLAIN 两个查询的语句:http://i.imgur.com/qrURgzV.png
【问题讨论】:
-
您能否发布
EXPLAIN的查询结果? (我注意到你的一张表在 MyISAM 中并且有一个地理空间索引。)你可能想读这个:use-the-index-luke.com -
@OllieJones 嗨 Ollie,我在问题中添加了
EXPLAIN,很抱歉没有提及 MyISAM 表 -
在我看来,粗略的估计,在使用 GROUP_CONCAT 解决方案的 SQL 中没有问题。
-
当您优化 GROUP BY 查询时,结果集中的所有列都是相关的。这是因为复合索引可以产生很大的性能差异。
-
@OllieJones 根据这些信息,我确保所有选定的字段现在都在查询中