I've two queries. The first one counts the results and the second one gets the date that I need.
My database has ~150000 rows. They take some while to run.
How can i speed things up?
$sql ="SELECT COUNT(DISTINCT articolo.id) FROM articolo
INNER JOIN l_articolo_formato ON l_articolo_formato.id_articolo=articolo.id
INNER JOIN formato ON formato.id=l_articolo_formato.id_formato
INNER JOIN l_articolo_genere ON l_articolo_genere.id_articolo=articolo.id
INNER JOIN genere ON genere.id=l_articolo_genere.id_genere
INNER JOIN l_articolo_rental ON l_articolo_rental.id_articolo=articolo.id
INNER JOIN rental ON rental.id=l_articolo_rental.id_rental
WHERE formato.nome = :var_value AND genere.adult = 0 AND rental.nome ='0'" ;
$s=$pdo->prepare($sql);
$s->bindValue(':var_value',$var_value);
$s->execute();
$num_ris=$s->fetchColumn(0);
// il numero totale di pagine
$all_pages = ceil($num_ris / $ris_per_pag);
//la ricerca
$sql ="SELECT articolo.id,articolo.titolo,articolo.anno_produzione,articolo.immagine,articolo.prezzo_dettaglio FROM articolo
INNER JOIN l_articolo_formato ON l_articolo_formato.id_articolo=articolo.id
INNER JOIN formato ON formato.id=l_articolo_formato.id_formato
INNER JOIN l_articolo_genere ON l_articolo_genere.id_articolo=articolo.id
INNER JOIN genere ON genere.id=l_articolo_genere.id_genere
INNER JOIN l_articolo_rental ON l_articolo_rental.id_articolo=articolo.id
INNER JOIN rental ON rental.id=l_articolo_rental.id_rental
WHERE formato.nome = :var_value AND genere.adult = 0 AND rental.nome ='0' LIMIT $first,$ris_per_pag" ;
$s=$pdo->prepare($sql);
$s->bindValue(':var_value',$var_value);
$s->execute();
$s->setFetchMode(PDO::FETCH_BOTH);
So this is the explain of the first query
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE articolo index PRIMARY PRIMARY 4 NULL 42092 Using index
1 SIMPLE l_articolo_formato ref PRIMARY PRIMARY 4 uz49pu08_StoreDb.articolo.id 1 Using index
1 SIMPLE l_articolo_rental ref PRIMARY PRIMARY 4 uz49pu08_StoreDb.l_articolo_formato.id_articolo 1 Using where; Using index
1 SIMPLE l_articolo_genere ref PRIMARY PRIMARY 4 uz49pu08_StoreDb.l_articolo_formato.id_articolo 1 Using where; Using index
1 SIMPLE genere eq_ref PRIMARY PRIMARY 4 uz49pu08_StoreDb.l_articolo_genere.id_genere 1 Using where
1 SIMPLE rental ALL PRIMARY NULL NULL NULL 2 Using where; Using join buffer
1 SIMPLE formato eq_ref PRIMARY PRIMARY 4 uz49pu08_StoreDb.l_articolo_formato.id_formato 1 Using where