Mysql
 sql >> Base de données >  >> RDS >> Mysql

Requête suivante prenant trop de temps à s'exécuter. Comment l'optimiser

Je pense que vous pouvez utiliser

au lieu de JOINDRE.

SELECT m.year, COUNT(m.id) FROM movies m 
where 
exists (select * from roles r where r.movie_id=m.id and 
exists(select * from actors a where a.id=r.actor_id and a.gender='F'))
group by  m.year;

Pour afficher le nombre total de films par an avec la sortie ci-dessus.

select t1.year,t1.count,t2.total from 
(
SELECT m.year as year, COUNT(m.id) as count FROM movies m 
where exists (select * from roles r where r.movie_id=m.id and exists(select * from actors a where a.id=r.actor_id and a.gender='F'))
group by  m.year
)t1 
join
(select year,count(m.id) as total from movies m group by m.year) t2
on t1.year=t2.year;