![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
17/03/2018, 08:03
|
![Avatar de mortiprogramador](http://static.forosdelweb.com/customavatars/avatar320585_1.gif) | Colaborador | | Fecha de Ingreso: septiembre-2009 Ubicación: mortuoria
Mensajes: 3.805
Antigüedad: 15 años, 5 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 originalSELECT * FROM gestiones g1 LEFT JOIN ( SELECT MAX(fecha) fecha, id_gestion FROM gestiones ORDER BY fecha DESC ) g2 ON (g1.fecha = g2.fecha AND g1.id_gestion = g2.id_gestion) 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 originalUsando IN +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+ | 1 | PRIMARY | gestiones | ALL | NULL | NULL | NULL | NULL | 25 | Using where; Using temporary; Using filesort | | 2 | DEPENDENT SUBQUERY | gestiones | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where | +----+--------------------+------------------+-----------------+---------------+---------+---------+------+------+----------------------------------------------+ Usando LEFT JOIN +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+ | 1 | PRIMARY | g1 | ALL | NULL | NULL | NULL | NULL | 25 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | gestiones | ALL | NULL | NULL | NULL | NULL | 25 | Using temporary | +----+-------------+------------------+------+---------------+------+---------+------+------+----------------------------------------------+
Luego, agregando algunos index sobre los campos claves de la consulta
Código BASH:
Ver originalUsando IN +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+ | 1 | PRIMARY | gestiones | index | NULL | index6 | 5 | NULL | 25 | Using where | | 2 | DEPENDENT SUBQUERY | gestiones | unique_subquery | PRIMARY,index3,index4,index5 | PRIMARY | 4 | func | 1 | Using where | +----+--------------------+------------------+-----------------+------------------------------+---------+---------+------+------+-------------+ Usando LEFT JOIN +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+ | 1 | PRIMARY | g1 | ALL | index3,index4 | NULL | NULL | NULL | 25 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | gestiones | index | NULL | index2 | 9 | NULL | 25 | Using index; Using temporary | +----+-------------+------------------+-------+---------------+--------+---------+------+------+----------------------------------------------+
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 |