我使用以下查询来获取years和number of movies released in that year哪个(指一部电影)有一个女演员。
years
number of movies released in that year
它执行成功,但是要花4分钟才能执行。 如何在更短的时间内获得结果?
此外,在此执行期间,我的磁盘使用率超过了70%。
SELECT m.year, COUNT(m.id) FROM movies m JOIN roles r ON m.id=r.movie_id JOIN actors a ON r.actor_id=a.id WHERE a.gender='F' GROUP BY m.year;
我想你可以用
存在
而不是JOIN。
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;
显示每年的电影总数以及以上输出。
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;