使用ORDER BY时MySQL查询速度较慢

使用ORDER BY时MySQL查询速度较慢,mysql,performance,Mysql,Performance,我有一个疑问: 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 =

我有一个疑问:

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秒,没有订单时仅需约0.001秒

你知道为什么点菜这么慢吗

编辑 说明:

编辑2 索引 如果您的查询没有订单,将在10行限制后终止


使用ORDER BY full结果集需要生成、排序,然后返回前10行。

尽管您永远无法达到仅输出10行的速度,而不是获取所有行,然后对它们进行排序,然后输出前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
希望有帮助

INDEX(released, cDate)
可能有帮助


避免或可能有帮助。

您的ORDER BY column article.cDate应该被索引,这将停止ORDER BY article.cDate花费很长时间,因为列值将被索引,然后可以以指数速度排列

你选择了什么。。。显示,看起来order by子句中使用的列没有索引。@AbhikChakraborty请参阅编辑检查此链接是否正确您的架构缺少一些索引我建议将索引ArticleRelease、sDate和userreleased。。。对表进行备份,查看现有索引,如果尚未添加上述索引,则添加它们。@AbhikChakraborty好的,我已添加了上述索引,但仍有约0.2秒的时间。还有其他的优化方法吗?不幸的是没有。速度与列的索引相同