请告知如何改善此慢查询。
SELECT response.`reasonid`
FROM response
INNER JOIN ACTION ON action.actionid = response.actionid
WHERE
response.respdate BETWEEN 20160305
AND 20160905
AND
(
(
response.reasonid = 'Prospect Call'
AND response.typeid = '0'
AND action.typeid = '9'
)
OR
(
response.typeid = '1000'
AND action.typeid = '1'
)
)
有索引:
response.actionid / response.reasonid / response.typeid / action.typeid / response.respdate
解释结果:
table type possible_keys key key_len ref rows Extra
ACTION range PRIMARY,idx_actiontypeid idx_actiontypeid 5 \N 310617 Using where; Using index
response ref idx_respdate2,idx_actionid, idx_actionid 5 ACTION.actionid 1 Using where
idx_reasonid,idx_resptypeid
答案 0 :(得分:0)
尝试此查询将一些索引列添加到连接
SELECT response.`reasonid`
FROM response
INNER JOIN ACTION ON action.actionid = response.actionid and response.typeid in( '1000','0') and action.typeid in('0','1')
WHERE
response.respdate BETWEEN 20160305
AND 20160905
AND
(
(
response.reasonid = 'Prospect Call'
AND response.typeid = '0'
AND action.typeid = '9'
)
OR
(
response.typeid = '1000'
AND action.typeid = '1'
)
)
答案 1 :(得分:0)
试试: 将表格的大小缩小到只需要使用的内容:
select a.*
from (
SELECT response.`reasonid`
FROM response
WHERE response.respdate BETWEEN 20160305 AND 20160905
) a
INNER JOIN ACTION ON action.actionid = a.actionid
WHERE (
response.reasonid = 'Prospect Call'
AND response.typeid = '0'
AND action.typeid = '9'
)
OR
(
response.typeid = '1000'
AND action.typeid = '1'
)
答案 2 :(得分:0)
请尝试在查询中指定日期,因为它们在数据库中:
SELECT response.`reasonid`
FROM response
INNER JOIN ACTION ON action.actionid = response.actionid
WHERE
response.respdate BETWEEN "2016-03-05"
AND "2016-09-05"
AND
(
(
response.reasonid = 'Prospect Call'
AND response.typeid = '0'
AND action.typeid = '9'
)
OR
(
response.typeid = '1000'
AND action.typeid = '1'
)
)
并检查这是否加快了您的查询速度。 如果将其写为数值(20160305),则数据库必须在比较之前对每一行进行隐式类型转换,这可能会导致性能降低。
答案 3 :(得分:0)
您的查询不应该有单独的索引,而是复合甚至覆盖索引。其他人没有评论我感到惊讶。我为索引提供了以下建议
表索引 响应(typeid,respdate,reasoned,actionid) 动作(actionid,typeid)
然后我调整了查询以使用UNION而不是OR
gtags