现在需要很长时间才能运行。
查询是:
select count(id), variety_id, name
from tblItem
where order_id IN (
select order_id
from tblItem
where variety_id=4005
order by order_id DESC)
AND variety_id != 4005
GROUP BY variety_id
order by count(id) DESC
LIMIT 5;
我在variety_id和order_id上有索引。我基本上是在尝试构建一个推荐引擎。该查询正在寻找人们在购买variety_id 4005时购买的前5个项目。但就像我说它需要长时间运行。
有没有人可以优化此查询?
答案 0 :(得分:4)
试试这个:
select count(t1.id), t1.variety_id, t1.name
from tblItem t1
inner join tblItem t2 ON t2.order_id = t1.order_id and t2.variety_id = 4005
where t1.variety_id != 4005
GROUP BY t1.variety_id, t1.name
ORDER BY count(t1.id) DESC
LIMIT 5;
答案 1 :(得分:2)
我经常发现MySQL不能很好地优化WHERE ... IN (SELECT ...)
,而JOIN
效果更好;我已经读过最近的MySQL版本更好,所以它可能依赖于版本。此外,您应该使用COUNT(*)
,除非该列可以是NULL
,并且您需要忽略计数中的空值。
SELECT COUNT(*) count, variety_id, name
FROM tblItem AS t1
JOIN (SELECT DISTINCT order_id
FROM tblItem
WHERE variety_id = 4005) AS t2
ON t1.order_id = t2.order_id
WHERE t1.variety_id != 4005
GROUP BY variety_id
ORDER BY count DESC
LIMIT 5
需要使用DISTINCT
的子查询来防止将计数乘以交叉积中的匹配行数。