【发布时间】:2012-05-29 23:18:04
【问题描述】:
使用 ORDER BY 运行此程序需要 10 多秒,并最终导致我的网站在高流量时崩溃。
select *
from tbluserinfluences, tblcontent, tblusers
where tblcontent.userid = tblusers.id
and tbluserinfluences.userid = tblusers.id
and tbluserinfluences.lcase_influence = 'pink floyd'
order by tblcontent.score desc
limit 0, 160
不使用 ORDER BY 运行相同的查询只需几毫秒。
select *
from tbluserinfluences, tblcontent, tblusers
where tblcontent.userid = tblusers.id
and tbluserinfluences.userid = tblusers.id
and tbluserinfluences.lcase_influence = 'pink floyd'
order by tblcontent.score desc
limit 0, 160
这里是解释
有什么想法吗?我愿意将其拆分为多个查询、创建临时表或任何其他有帮助的东西。这个查询让我(和我的用户)感到厌烦。
谢谢!
【问题讨论】:
-
您也可以显示您的表架构吗?
-
SHOW CREATE TABLE tbluser 有什么影响;显示创建表 tblcontent;和 SHOW CREATE TABLE tblusers 返回?您可能需要在分数列上添加索引
-
你在这些表上的索引是什么?
-
tblcontent.score 上有一个索引,但我不确定它是否有帮助或正在使用。
-
@eggyal 所有相关的 cmets 都已编入索引。删除 ORDER BY 使查询超快的事实让我知道(可能)所有其他索引都很好。我认为..?
标签: mysql sql performance sql-order-by