简体   繁体   English

Mysql 从慢日志查询

[英]Mysql Query FROM SLOW LOG

I have problem with query.我有查询问题。

Query: http://pastebin.com/RNW0vgJX Products: 10 000 Time: ~18s查询: http : //pastebin.com/RNW0vgJX产品:10 000 时间:~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.就像 Mark Ba​​ker 建议的那样,对查询做一个解释,它有助于找出索引可能被遗忘的地方。

In basic every primary and foreign key should have one.基本上每个主键和外键都应该有一个。

Also distinct products .* is probably very expensive.同样不同的products .* 可能非常昂贵。 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.使用 where WHERE 子句用于限制哪些行与下一个表匹配或发送到客户端。 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.除非您特别打算从表中获取或检查所有行,否则如果 Extra 值不是 Using where 并且表连接类型是 ALL 或索引,则您的查询可能会出错。 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.即使您对 WHERE 子句的所有部分都使用索引,您可能会看到 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.使用临时表来解析查询,MySQL 需要创建一个临时表来保存结果。 This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.如果查询包含以不同方式列出列的 GROUP BY 和 ORDER BY 子句,则通常会发生这种情况。
  • using filesort MySQL must do an extra pass to find out how to retrieve the rows in sorted order.使用 filesort MySQL 必须执行额外的检查以找出如何按排序顺序检索行。 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.排序是通过根据连接类型遍历所有行并存储排序键和指向与 WHERE 子句匹配的所有行的行的指针来完成的。 The keys then are sorted and the rows are retrieved in sorted order然后对键进行排序,并按排序顺序检索行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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