简体   繁体   English

为什么这个MySQL查询在一台服务器上比另一台服务器慢?

[英]Why is this MySQL query slower on one server than another?

I have an SQL query I'm trying to optimise. 我有一个SQL查询,我正在尝试优化。 Interestingly, on one MySQL server, the query runs in around 0.06 seconds, which I'm happy with, but on another server, the query takes closer to 0.2 seconds (twice as long). 有趣的是,在一个MySQL服务器上,查询运行大约0.06秒,我很满意,但在另一台服务器上,查询接近0.2秒(两倍长)。

SELECT fo.facets_id, fo.facets_options_id, fo.value
FROM facets_options fo
INNER JOIN categories_facets cf ON cf.facets_options_id=fo.facets_options_id
INNER JOIN categories c ON c.categories_id=cf.categories_id
WHERE fo.facets_id="2"
AND fo.language_id = "1"
AND c.enabled=1
GROUP BY fo.facets_options_id
ORDER BY fo.value ASC;

The "EXPLAIN" output on both servers is identical: 两台服务器上的“EXPLAIN”输出完全相同:

id          select_type          table  type   possible_keys                                             key               key_len  ref                        rows Extra
1           SIMPLE               fo     ref    PRIMARY,facets_options_id,facets_id,facets_id_2           facets_id         4        const                      986  Using where; Using temporary; Using filesort
1           SIMPLE               cf     ref    PRIMARY,categories_id,categories_id_2,facets_options_id   facets_options_id 4        mydb.fo.facets_options_id  37   
1           SIMPLE               c      eq_ref PRIMARY,enabled                                           PRIMARY           4        mydb.cf.categories_id      1    Using where

The server on which the query runs slowly is using MySQL 5.1.66, and the server on which it is fast is using MySQL 5.5.29. 查询运行缓慢的服务器使用MySQL 5.1.66,而运行速度快的服务器使用的是MySQL 5.5.29。

Is it likely that the newer version of MySQL is doing a better job of optimizing the query? 新版本的MySQL是否可能在优化查询方面做得更好? Or is there something else that can be causing it? 或者还有其他可能导致它的东西? How can I find out more about what's going on behind the scenes? 我怎样才能更多地了解幕后发生的事情?

I assume your hardware is the same on both servers. 我假设你的硬件在两台服务器上都是一样的。

You can have a look with the profiler where the difference comes from. 您可以通过分析器查看差异来源。

SET profiling = 1;
SELECT /*yourquery...*/
SHOW PROFILES;
/*lookup the id for your query*/
SHOW PROFILE FOR QUERY 1 /*or whatever id your query has*/

You'll get a detailed list, what took how long and so on... 你会得到一个详细的清单,花了多长时间等等......

Also there's EXPLAIN EXTENDED , with which you can see the optimized query. 还有EXPLAIN EXTENDED ,您可以使用它来查看优化查询。

An example is here in the manual. 一个例子是这里的说明书中无。

Apart from that, it may well be, that the difference comes from different MySQL versions. 除此之外,差异可能来自不同的MySQL版本。

Percona did investigate performance improvements from 5.1 to 5.5. Percona确实调查了从5.1到5.5的性能改进。 Result is here . 结果在这里

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

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