简体   繁体   中英

Mysql left join very slow

I have a left join:

$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

  ";

It's really slow abaout 7 seconds on 4000 records

Any ideas what I might be doing wrong

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

I would put an index on a.committee, and full text index the doctitle col. The IN and LIKE are immediate flags to me. Issue date should also have an index because you are >= it

Try running the following commands in a MySQL client:

show index from #__document_management_documents;
show index from #_document_management_tags;

Check to see if there are keys/indexes on the id and documentid fields from the respective tables. If there aren't, MySQL will be doing a full table scan to lookup the values. Creating indexes on these fields makes the search time logarithmic, because it sorts them in a binary tree which is stored in the index file. Even better is to use primary keys (if possible), because that way the row data is stored in the leaf, which saves MySQL another I/O operation to lookup the data.

It could also simply be that the IN and >= operators have bad performance, in which case you might have to rewrite your queries or redesign your tables.

As mentioned above, try to find if your columns have index. You can even do "EXPLAIN" command in your MySQL client at the start of your query to see if the query is actually using indexes. You will see in the 'key' columns and 'Extra' column. Get more information here

This will help you optimize your query. Also group by causes using temporary and filesort which causes MySQL to create a temporary table and going through each rows. If you could use PHP to group by it would be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM