mysql - Slow result with LEFT JOIN -


i have simple sql query , if limit more 10, time result exceeds 15 seconds. question is, why it's slow ? here request:

select e.id_pays tbl_usager left join tbl_ville b on a.id_ville = b.id_ville left join tbl_departement c on b.id_departement = c.id_departement left join tbl_pays e on c.id_pays = e.id_pays  e.id_pays='1' limit 8 

first, left joins unnecessary query. where clause turns them inner joins.

second, final join unnecessary, because can take value departement table.

select d.id_pays tbl_usager u join      tbl_ville v      on u.id_ville = v.id_ville join      tbl_departement d      on v.id_departement = d.id_departement d.id_pays = '1' limit 8; 

that help. next, assume id_* variables primary keys in respective tables. finally, create index on id_pays:

create index idx_department_2 on tbl_department(id_pays, id_departement); 

these should query.


Comments