2010-06-10 17 views
8

che sto cercando di ottimizzare questa query:Mysql query lente: INNER JOIN + ORDER BY cause FileSort

 
SELECT `posts`.* FROM `posts` INNER JOIN `posts_tags` 
    ON `posts`.id = `posts_tags`.post_id 
    WHERE (((`posts_tags`.tag_id = 1))) 
    ORDER BY posts.created_at DESC; 

La dimensione delle tabelle è 38k righe e 31k e ​​MySQL utilizza "FileSort" così diventa piuttosto lento. Ho provato ad usare diversi indici, senza fortuna.

 
CREATE TABLE `posts` (
    `id` int(11) NOT NULL auto_increment, 
    `created_at` datetime default NULL, 
    PRIMARY KEY (`id`), 
    KEY `index_posts_on_created_at` (`created_at`), 
    KEY `for_tags` (`trashed`,`published`,`clan_private`,`created_at`) 
) ENGINE=InnoDB AUTO_INCREMENT=44390 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

CREATE TABLE `posts_tags` (
    `id` int(11) NOT NULL auto_increment, 
    `post_id` int(11) default NULL, 
    `tag_id` int(11) default NULL, 
    `created_at` datetime default NULL, 
    `updated_at` datetime default NULL, 
    PRIMARY KEY (`id`), 
    KEY `index_posts_tags_on_post_id_and_tag_id` (`post_id`,`tag_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=63175 DEFAULT CHARSET=utf8 
 
+----+-------------+------------+--------+--------------------------+--------------------------+---------+---------------------+-------+-----------------------------------------------------------+ 
| id | select_type | table  | type | possible_keys   | key      | key_len | ref     | rows | Extra              | 
+----+-------------+------------+--------+--------------------------+--------------------------+---------+---------------------+-------+-----------------------------------------------------------+ 
| 1 | SIMPLE  | posts_tags | index | index_post_id_and_tag_id | index_post_id_and_tag_id | 10  | NULL    | 24159 | Using where; Using index; Using temporary; Using filesort | 
| 1 | SIMPLE  | posts  | eq_ref | PRIMARY     | PRIMARY     | 4  | .posts_tags.post_id |  1 |               | 
+----+-------------+------------+--------+--------------------------+--------------------------+---------+---------------------+-------+-----------------------------------------------------------+ 
2 rows in set (0.00 sec) 

Che tipo di indice ho bisogno di definire per evitare mysql con FileSort? È possibile quando il campo dell'ordine non è nella clausola where?

aggiornamento: Profiling risultati:

 
mysql> show profile for query 1; 
+--------------------------------+----------+ 
| Status       | Duration | 
+--------------------------------+----------+ 
| starting      | 0.000027 | 
| checking query cache for query | 0.037953 | 
| Opening tables     | 0.000028 | 
| System lock     | 0.010382 | 
| Table lock      | 0.023894 | 
| init       | 0.000057 | 
| optimizing      | 0.010030 | 
| statistics      | 0.000026 | 
| preparing      | 0.000018 | 
| Creating tmp table    | 0.128619 | 
| executing      | 0.000008 | 
| Copying to tmp table   | 1.819463 | 
| Sorting result     | 0.001092 | 
| Sending data     | 0.004239 | 
| end       | 0.000012 | 
| removing tmp table    | 0.000885 | 
| end       | 0.000006 | 
| end       | 0.000005 | 
| query end      | 0.000006 | 
| storing result in query cache | 0.000005 | 
| freeing items     | 0.000021 | 
| closing tables     | 0.000013 | 
| logging slow query    | 0.000004 | 
| cleaning up     | 0.000006 | 
+--------------------------------+----------+ 

Update2:

reale query (alcuni campi più booleani, indici più inutili)

SELECT `posts`.* FROM `posts` INNER JOIN `posts_tags` 
    ON `posts`.id = `posts_tags`.post_id 
    WHERE ((`posts_tags`.tag_id = 7971)) 
     AND (((posts.trashed = 0) 
     AND (`posts`.`published` = 1 
     AND `posts`.`clan_private` = 0)) 
     AND ((`posts_tags`.tag_id = 7971))) 
    ORDER BY created_at DESC LIMIT 0, 10;

Empty set (1.25 sec)

Con out ORDER BY - 0,01 secondi.

 

+----+-------------+------------+--------+-----------------------------------------+-----------------------+---------+---------------------+-------+--------------------------+ 
| id | select_type | table  | type | possible_keys       | key     | key_len | ref     | rows | Extra     | 
+----+-------------+------------+--------+-----------------------------------------+-----------------------+---------+---------------------+-------+--------------------------+ 
| 1 | SIMPLE  | posts_tags | index | index_posts_tags_on_post_id_and_tag_id | index_posts_tags_... | 10  | NULL    | 23988 | Using where; Using index | 
| 1 | SIMPLE  | posts  | eq_ref | PRIMARY,index_posts_on_trashed_and_crea | PRIMARY    | 4  | .posts_tags.post_id |  1 | Using where    | 
+----+-------------+------------+--------+-----------------------------------------+-----------------------+---------+---------------------+-------+--------------------------+ 

SOLUZIONE

  1. Query aggiornato a "ORDER BY posts_tags.created_at DESC" (due piccoli cambiamenti nel codice di applicazione)
  2. Indice aggiunto: index_posts_tags_on_created_at.

Questo è tutto!

risposta

3

Si avrebbe bisogno di un po 'denormalizzare, e copiare il campo posts.created_at nella tabella post_tags (ho chiamato post_created_at, si potrebbe chiamarla come volete):

CREATE TABLE `posts_tags` (
    `id` int(11) NOT NULL auto_increment, 
    `post_id` int(11) default NULL, 
    `tag_id` int(11) default NULL, 
    `post_created_at` datetime default NULL, 
    `created_at` datetime default NULL, 
    `updated_at` datetime default NULL, 
    PRIMARY KEY (`id`), 
    KEY `index_posts_tags_on_post_id_and_tag_id` (`post_id`,`tag_id`) 
) ENGINE=InnoDB; 

e quindi aggiungere un indice a posts_tags su

(tag_id, post_created_at) 

che permetteranno la query per ottenere tutti i messaggi per un tag, nell'ordine corretto, senza FileSort.

+0

Grazie! Ho aggiornato query da utilizzare per l'ordinamento posts_tags.created_at (non c'è bisogno di sapere tag data di creazione) e ha aggiunto index_posts_tags_on_created_at ... non più filesort! :) – Alexander

+0

È possibile informare il pianificatore di query che due colonne in tabelle diverse sono uguali, quindi non dovresti specificare esplicitamente che dovrebbe usare posts_tags.created_at invece di posts.created_at? – sorenbs

0

la chiave index_posts_on_created_at è ordinato ascendente, ma si desidera che i risultati ordinati decrescente

+0

Usa filesort con entrambe le ASC e desc ordini :( – Alexander

1

provare a cambiare KEY index_posts_tags_on_post_id_and_tag_id (post_id, tag_id) per chiave index_posts_tags_tag_id (tag_id) e ripubblicare Spiega.

Qual è la distribuzione di TagID con Post_Tags?