سؤال

I have an SQL query I'm trying to optimise. 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).

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:

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.

Is it likely that the newer version of MySQL is doing a better job of optimizing the query? 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.

An example is here in the manual.

Apart from that, it may well be, that the difference comes from different MySQL versions.

Percona did investigate performance improvements from 5.1 to 5.5. Result is here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top