我有这个问题:
SELECT article.id
FROM article
INNER JOIN article_cCountry ON article.id = ID1 AND ID2 = 1
INNER JOIN user_cCountry ON article.uId = user_cCountry.ID1 AND user_cCountry.ID2 = 1
LEFT JOIN user ON article.uId = user.ID
WHERE article.released = "TRUE"
AND article.sDate < now()
AND article.cDate != "0000-00-00 00:00:00"
AND (article.eDate > now() OR article.eDate = 0)
AND ( (user.released = true) OR (article.uId = 0) )
ORDER BY article.cDate DESC
LIMIT 0, 10
查询需要约0.3秒,而ORDER BY只需约0.001秒。
为什么ORDER BY这么慢的想法?
答案 0 :(得分:3)
如果没有ORDER BY
,您的查询将在10行(LIMIT)之后终止。
需要生成ORDER BY
完整结果集,排序,然后返回前10行。
答案 1 :(得分:0)
虽然你永远无法达到只输出10行的速度,然后对所有行进行排序,然后排序em然后输出前10行,你可以做一些小技巧来记录内存中的记录集
SELECT id FROM (
SELECT article.id,article.cDate
FROM article
INNER JOIN article_cCountry ON article.id = ID1 AND ID2 = 1
INNER JOIN user_cCountry ON article.uId = user_cCountry.ID1 AND user_cCountry.ID2 = 1
LEFT JOIN user ON article.uId = user.ID
WHERE article.released = "TRUE"
AND article.sDate < now()
AND article.cDate != "0000-00-00 00:00:00"
AND (article.eDate > now() OR article.eDate = 0)
AND ( (user.released = true) OR (article.uId = 0) )
)
ORDER BY cDate DESC
LIMIT 0, 10
希望有所帮助
答案 2 :(得分:0)
INDEX(released, cDate)
may help
Avoiding OR
may help.
答案 3 :(得分:0)
Your ORDER BY
column (article.cDate
) should be indexed, this will stop the ORDER BY article.cDate
taking a long time, as the column values will be indexed and then can be arranged exponentially faster.