我遇到MySQL查询运行速度极慢的问题。每个人需要10分钟以上。我试图用LEFT JOINT重写它,但“关节”不是我最好的专业知识))
我希望有人能够帮助我。
select subscriberId
from segments
where (segmentId = 29)
and subscriberId not in
(select subscriberId from que where (campaignId = 31 or campaignId = 32))
order by rand()
limit 20000
所有索引都已到位。非常感谢。
答案 0 :(得分:1)
尝试使用联接
SELECT sg.subscriberId
FROM segments sg JOIN que q
ON (sg.subscriberId = q.subscriberId and sg.segmentId = 29)
WHERE (q.campaignId <> 31 AND q.campaignId <> 32)
答案 1 :(得分:1)
尝试将NOT IN
替换为NOT EXISTS
select s.subscriberId
from segments s
where (s.segmentId = 29)
and NOT EXISTS (select * from que q
where s.subscriberId = q.subscriberId AND
(q.campaignId = 31 or q.campaignId = 32))
order by rand()
limit 20000
答案 2 :(得分:0)
我认为,你的问题在于
order by rand()
尝试删除此内容并查看时间。
更新
添加索引:
alter table
que
add index (subscriberId
)
试试这个:
que
我通过rand()看到0.08秒和0.13的顺序。
不要使用phpmyadmin进行检查,因为它有更多时间来呈现结果。我正在使用控制台mysql客户端
subscriberId
select segments.subscriberId from segments
left join que on que.subscriberId = segments.subscriberId and ( que.campaignId = 31 or que.campaignId = 32 )
where segments.segmentId = 29 and que.id is null
limit 20000
20000 rows in set (0.13 sec)