我有一个左连接:
$query = "SELECT a.`id`, a.`documenttitle`, a.`committee`, a.`issuedate`, b.`tagname`
FROM `#__document_management_documents` AS a
LEFT JOIN `#__document_managment_tags` AS b
ON a.id = b.documentid
".$tagexplode."
".$issueDateText."
AND a.committee in (".$committeeQueryTextExplode.")
AND a.documenttitle LIKE '".$documentNameFilter."%'
GROUP BY a.id ORDER BY a.documenttitle ASC
";对于4000条记录,它真的很慢,大约7秒
你知道我做错了什么吗?
SELECT a.`id`, a.`documenttitle`, a.`committee`, a.`issuedate`, b.`tagname`
FROM `w4c_document_management_documents` AS a
LEFT JOIN `document_managment_tags` AS b
ON a.id = b.documentid WHERE a.issuedate >= ''
AND a.committee in ('1','8','9','10','11','12','13','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47')
AND a.documenttitle LIKE '%' GROUP BY a.id ORDER BY a.documenttitle ASC发布于 2014-02-21 00:51:27
我会在a.committee上建立一个索引,并对文档标题列建立全文索引。IN和LIKE对我来说是直接的标志。发行日期也应该有一个索引,因为你是>=它
发布于 2014-02-21 00:53:53
尝试在MySQL客户端中运行以下命令:
show index from #__document_management_documents;
show index from #_document_management_tags;检查各个表中的id和documentid字段上是否有键/索引。如果没有,MySQL将执行全表扫描以查找这些值。在这些字段上创建索引使搜索时间成为对数,因为它将它们排序在存储在索引文件中的二叉树中。更好的做法是使用主键(如果可能),因为这样行数据就存储在叶中,这就为MySQL节省了查找数据的另一次I/O操作。
也可能只是因为IN和>=操作符的性能很差,在这种情况下,您可能需要重写查询或重新设计表。
发布于 2014-02-21 01:11:11
如上所述,尝试找出您的列是否有索引。您甚至可以在查询开始时在MySQL客户机中执行"EXPLAIN“命令,以查看查询是否实际使用了索引。您将在“key”列和“Extra”列中看到。获取更多信息here
这将帮助您优化查询。group by causes使用temporary和filesort,这会导致MySQL创建一个临时表并遍历每一行。如果你可以使用PHP来分组,它会更快。
https://stackoverflow.com/questions/21914146
复制相似问题