简体   繁体   中英

Mysql Query FROM SLOW LOG

I have problem with query.

Query: http://pastebin.com/RNW0vgJX Products: 10 000 Time: ~18s

How I Can optymalize this query?

Like Mark Baker suggested, do an explain on the query it helps out a lot to see where indexes might be forgotten.

In basic every primary and foreign key should have one.

Also distinct products .* is probably very expensive. You could rewrite the query like

select * from `products` where productid in (your first query ). 

This is usually a lot faster than a distinct on a all columns of a table.

explain results: In the explain the foreign key indexes are looking good. there is some delay in the grouping and sorting:

  • Using where A WHERE clause is used to restrict which rows to match against the next table or send to the client. Unless you specifically intend to fetch or examine all rows from the table, you may have something wrong in your query if the Extra value is not Using where and the table join type is ALL or index. Even if you are using an index for all parts of a WHERE clause, you may see Using where if the column can be NULL.
  • using temporary To resolve the query, MySQL needs to create a temporary table to hold the result. This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.
  • using filesort MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause. The keys then are sorted and the rows are retrieved in sorted order

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