Optimize MySQL query

0

I have a query that takes 0.2650 seconds to run and I would like to optimize it, I have done a lot of tests (Add new indexes, delete them, create views) but nothing has improved the performance of the following query:

SELECT enlace_click.id,enlace_click.enlace,enlace_click.cantidad
FROM enlace_click
INNER JOIN enlace ON enlace_click.enlace = enlace.id
ORDER BY enlace_click.cantidad ASC , enlace_click.enlace DESC

Mostrando filas 0 - 29 (total de 802430, La consulta tardó 0.2650 seg)

EXPLAIN:

id  select_type table type          possible_keys       key      key_len  ref                                  rows    Extra    
1   SIMPLE            enlace_click  ALL UNIQUE,enlace   NULL     NULL     NULL                                 1074631 Using filesort
1   SIMPLE            enlace        eq_ref  PRIMARY     PRIMARY   4       gnula_serie.enlace_click.enlace      1       Using index

Searching why it may take so long:

SELECT enlace_click.id,enlace_click.enlace,enlace_click.cantidad
FROM enlace_click
INNER JOIN enlace ON enlace_click.enlace = enlace.id
ORDER BY enlace_click.cantidad ASC , enlace_click.enlace ASC

Mostrando filas 0 - 29 (total de 802430, La consulta tardó 0.0016 seg)

EXPLAIN:

 id select_type table   type            possible_keys       key     key_len ref                             rows    Extra   
 1  SIMPLE              enlace_click    ALL UNIQUE,enlace   NULL    NULL    NULL                            1074631 Using filesort
 1  SIMPLE              enlace          eq_ref  PRIMARY     PRIMARY 4       gnula_serie.enlace_click.enlace 1       Using index


SELECT enlace_click.id,enlace_click.enlace,enlace_click.cantidad
FROM enlace_click
INNER JOIN enlace ON enlace_click.enlace = enlace.id

Mostrando filas 0 - 29 (total de 802489, La consulta tardó 0.0009 seg)

EXPLAIN:

 id select_type   table           type    possible_keys     key       key_len   ref                               rows     Extra    
1   SIMPLE        enlace_click    ALL     UNIQUE,enlace     NULL      NULL      NULL                              1074670  NULL
1   SIMPLE        enlace          eq_ref  PRIMARY           PRIMARY   4         gnula_serie.enlace_click.enlace   1        Using index

INDEX:

Nombre de la clave  Tipo    Único   Empaquetado Columna     Cardinalidad    Cotejamiento    Nulo
cantidad,enlace     BTREE   No      No          cantidad    2023            A               No
                                                enlace      1074590         A               No
    
asked by Rene Limon 17.04.2017 в 08:18
source

1 answer

0

According to the result of EXPLAIN that shows the main problem is when ordering the records. The way to optimize this query is to create an index in the enlace_click table that contains the columns: (cantidad, enlace) . It is important that the order in which the columns appear in the index definition is the same as the one used in the ORDER BY clause, in this case first appears cantidad and then enlace .

On the other hand:

  • Is it necessary to do the intersection ( inner join ) with the table enlace ? Since in the projection ( select ) there are no fields in the table enlace .
  • Can there be records in the enlace_click table with values in the enlace column that do not exist in the enlace table?
answered by 17.04.2017 в 23:12