Ver Mensaje Individual
  #9 (permalink)  
Antiguo 17/03/2018, 08:03
Avatar de mortiprogramador
mortiprogramador
Colaborador
 
Fecha de Ingreso: septiembre-2009
Ubicación: mortuoria
Mensajes: 3.805
Antigüedad: 14 años, 7 meses
Puntos: 214
Respuesta: Group By o Join?

De nada Lucky_Sky, sin embargo la solución que daba Libras era algo así:

Código SQL:
Ver original
  1. SELECT * FROM gestiones g1
  2. LEFT JOIN
  3. (
  4.     SELECT MAX(fecha) fecha, id_gestion FROM gestiones ORDER BY fecha DESC
  5. ) g2 ON (g1.fecha = g2.fecha AND g1.id_gestion = g2.id_gestion)
  6. WHERE sop = 'H' AND id_inmueble = '250' GROUP BY id_cliente;

Y teniendo en cuenta lo del rendimiento, haciendo explain de ambas consultas
veo lo siguiente:

Código BASH:
Ver original
  1. Usando IN
  2. +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+                                                                                                                                                        
  3. | id | select_type        | table            | type            |  possible_keys | key     | key_len | ref  | rows | Extra                                         |                                                                                                                                                      
  4. +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+                                                                                                                                                        
  5. |  1 | PRIMARY            | gestiones        | ALL             | NULL           | NULL    | NULL    | NULL |   25 | Using where; Using temporary;  Using filesort |                                                                                                                                                      
  6. |  2 | DEPENDENT SUBQUERY | gestiones        | unique_subquery | PRIMARY        | PRIMARY | 4       | func |    1 | Using where                                   |                                                                                                                                                      
  7. +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+                                                                                                                                                                                                                                                                                                      
  8. Usando LEFT JOIN
  9. +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+
  10. | id | select_type | table            | type | possible_keys | key  | key_len | ref  | rows | Extra                                        |
  11. +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+
  12. |  1 | PRIMARY     | g1               | ALL  | NULL          | NULL | NULL    | NULL |   25 | Using where; Using temporary; Using filesort |
  13. |  1 | PRIMARY     | <derived2>       | ALL  | NULL          | NULL | NULL    | NULL |    1 |                                              |
  14. |  2 | DERIVED     | gestiones        | ALL  | NULL          | NULL | NULL    | NULL |   25 | Using temporary                              |
  15. +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+

Luego, agregando algunos index sobre los campos claves de la consulta

Código BASH:
Ver original
  1. Usando IN
  2. +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+
  3. | id | select_type        | table            | type            | possible_keys                | key     | key_len | ref  | rows | Extra       |
  4. +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+
  5. |  1 | PRIMARY            | gestiones        | index           | NULL                         | index6  | 5       | NULL |   25 | Using where |
  6. |  2 | DEPENDENT SUBQUERY | gestiones        | unique_subquery | PRIMARY,index3,index4,index5 | PRIMARY | 4       | func |    1 | Using where |
  7. +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+
  8. Usando LEFT JOIN
  9. +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+
  10. | id | select_type | table            | type  | possible_keys | key    | key_len | ref  | rows | Extra                                        |
  11. +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+
  12. |  1 | PRIMARY     | g1               | ALL   | index3,index4 | NULL   | NULL    | NULL |   25 | Using where; Using temporary; Using filesort |
  13. |  1 | PRIMARY     | <derived2>       | ALL   | NULL          | NULL   | NULL    | NULL |    1 |                                              |
  14. |  2 | DERIVED     | gestiones        | index | NULL          | index2 | 9       | NULL |   25 | Using index; Using temporary                 |
  15. +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+


Y aprovechando a Libras, quisiera saber un poco más sobre estas diferencias,
y la eficiencia, con eso tal vez Lucky_Sky se anime a usar esa solución.
__________________
"Si consigues ser algo más que un hombre, si te entregas a un ideal, si nadie puede detenerte, te conviertes en algo muy diferente."
Visita piggypon.com

Última edición por mortiprogramador; 17/03/2018 a las 08:40