gpt4 book ai didi

具有多个子查询的 MySQL 查询太慢。我怎样才能加快速度?

转载 作者:行者123 更新时间:2023-11-30 00:21:59 25 4
gpt4 key购买 nike

我有一个 MySQL 查询,它完全符合我的要求,但处理时间在 110 到 130 秒之间。问题是它与一个软件协同工作,该软件在查询后 20 秒就会超时。

有什么办法可以加快查询速度吗?我正在考虑将数据库移至另一台服务器,但在走这条路之前还有其他更优雅的选择吗?

-- 1 Give me a list of IDs & eBayItemIDs
-- 2 where it is flagged as bottom tier
-- 3 Where it has been checked less than 168 times
-- 4 Where it has not been checked in the last hour
-- 5 Or where it was never checked but appears on the master list.


-- 1 Give me a list of IDs & eBayItemIDs
SELECT `id`, eBayItemID
FROM `eBayDD_Main`
-- 2 where it is flagged as bottom tier
WHERE `isBottomTier`='0'

-- 3 Where it has been checked less than 168 times
AND (`id` IN
(SELECT `mainid`
FROM `eBayDD_History`
GROUP BY `mainid`
HAVING COUNT(`mainID`) < 168)
-- 4 Where it has not been checked in the last hour
AND id IN
(SELECT `mainID`
FROM `eBayDD_History`
GROUP BY `mainID`
HAVING ((TIME_TO_SEC(TIMEDIFF(NOW(), MAX(`dateCollected`)))/60)/60) > 1))
-- 5 Or where it was never checked but appears on the master list.
OR (`id` IN
(SELECT `id`
FROM `eBayDD_Main`)
AND `id` NOT IN
(SELECT `mainID`
FROM `eBayDD_History`))

最佳答案

如果我正确理解了逻辑,您应该能够用以下逻辑替换此逻辑:

select m.`id`, m.eBayItemID 
from `eBayDD_Main` m left outer join
(select `mainid`, count(`mainID`) as cnt,
TIME_TO_SEC(TIMEDIFF(NOW(), MAX(`dateCollected`)))/60)/60) as dc
from `eBayDD_History`
group by `mainid`
) hm
on m.mainid = hm.mainid
where m.`isBottomTier` = '0' and hm.cnt < 168 and hm.dc > 1 or
hm.mainid is null;

关于具有多个子查询的 MySQL 查询太慢。我怎样才能加快速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com