为什么这个查询要花这么长时间?
这个查询出现在我的mysql系统上的慢日志中,
# Query_time: 37 Lock_time: 0 Rows_sent: 5 Rows_examined: 405199
select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created,
application_price.retail_price, euroapps.count FROM application_price INNER JOIN
euroapps ON euroapps.id = application_price.application_id WHERE
application_price.storefront_id = '143441' AND application_price.retail_price <= 0
ORDER BY created DESC LIMIT 5;
你看到它检查了405,199行,这可能是查询时间长的原因吗?
类似的查询从未出现在我的慢速日志中,该查询是:
select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created,
application_price.retail_price, euroapps.count FROM application_price INNER JOIN
euroapps ON euroapps.id = application_price.application_id WHERE
application_price.storefront_id = '$store' AND application_price.retail_price > 0
ORDER BY created DESC LIMIT 5
这是解释的输出:
mysql> explain select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created, application_price.retail_price, euroapps.count FROM application_price INNER JOIN euroapps ON euroapps.id = application_price.application_id WHERE application_price.storefront_id = '143441' AND application_price.retail_price <= 0 ORDER BY created DESC LIMIT 5;
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
| 1 | SIMPLE | application_price | range | PRIMARY,idx_storedfront_price_id | idx_storedfront_price_id | 9 | NULL | 110491 | Using where; Using index; Using temporary; Using filesort |
| 1 | SIMPLE | euroapps | eq_ref | PRIMARY | PRIMARY | 4 | itunesapps.application_price.application_id | 1 | |
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
2 rows in set (0.00 sec)
This query is appearing in my slow log on a mysql system,
# Query_time: 37 Lock_time: 0 Rows_sent: 5 Rows_examined: 405199
select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created,
application_price.retail_price, euroapps.count FROM application_price INNER JOIN
euroapps ON euroapps.id = application_price.application_id WHERE
application_price.storefront_id = '143441' AND application_price.retail_price <= 0
ORDER BY created DESC LIMIT 5;
You see it examines 405,199 rows, could that be the cause of the long query time?
A similar query never shows up in my slow log, that query is:
select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created,
application_price.retail_price, euroapps.count FROM application_price INNER JOIN
euroapps ON euroapps.id = application_price.application_id WHERE
application_price.storefront_id = '$store' AND application_price.retail_price > 0
ORDER BY created DESC LIMIT 5
Here is the output of explain:
mysql> explain select euroapps.id, euroapps.name, euroapps.imageurl, euroapps.created, application_price.retail_price, euroapps.count FROM application_price INNER JOIN euroapps ON euroapps.id = application_price.application_id WHERE application_price.storefront_id = '143441' AND application_price.retail_price <= 0 ORDER BY created DESC LIMIT 5;
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
| 1 | SIMPLE | application_price | range | PRIMARY,idx_storedfront_price_id | idx_storedfront_price_id | 9 | NULL | 110491 | Using where; Using index; Using temporary; Using filesort |
| 1 | SIMPLE | euroapps | eq_ref | PRIMARY | PRIMARY | 4 | itunesapps.application_price.application_id | 1 | |
+----+-------------+-------------------+--------+----------------------------------+--------------------------+---------+---------------------------------------------+--------+-----------------------------------------------------------+
2 rows in set (0.00 sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看执行计划,这将有助于缩小原因范围。
http://dev.mysql.com/doc/refman /5.5/en/execution-plan-information.html
You ought to look at the execution plan, that will help narrow down the cause.
http://dev.mysql.com/doc/refman/5.5/en/execution-plan-information.html
查看 WHERE 子句,您可以看到您使用
application_price.storefront_id
作为过滤因子。但是,在您的解释中,它没有显示为可能的键,这意味着它没有索引 - 这意味着需要全表扫描。
另一个因素是
application_price.retail_price
,正如 Jason Swett 建议的那样 - 索引您的 application_price.storefront_id ,您应该会看到更好的性能(Jason 您可能应该发布您的评论作为答案)。
Looking at your WHERE clause, you can see that you use
application_price.storefront_id
as filtering factor.However, in your EXPLAIN, it doesn't appear as possible key meaning it's not indexed - meaning that full table scan is required.
The other factor is
application_price.retail_price
, you can see what RANGE in explain means - however its cardinality is apparently low - hence so many rows.As Jason Swett suggested - index your application_price.storefront_id and you should see better performance (and Jason you should probably post your comment as an answer).
解释的“行”列表明 MySQL 估计它将必须检查 application_price 表中的 110491 行。也是使用临时的;在此表上使用文件排序。
我建议您为 application_price 添加一个索引,其中包含(storefront_id、application_id、retail_price、created)(如果“created”是 application_price 的字段)。这些字段的组合应该会有所帮助。
The "rows" column of the explain indicates that MySQL eastimates that it will have to examine 110491 rows from the application_price table. Also it is Using temporary; Using filesort on this table.
I suggst you add an index for application_price that includes (storefront_id, application_id, retail_price, created) if "created" is a field of application_price. Som combination of these fields should help.