阿理理
2020-06-23 10:01
如果是两个表之间的连接查询:
select film_id from film_actor where actor_id in (select actor_id from actor where first_name = 'sandra')
select film_id from film_actor join actor on (film_actor.actor_id = actor.actor_id and actor.first_name = 'sandra')
这里,join查询性能优于子查询, 比较好理解。性能如下:
子查询: Query_time: 0.008001 Lock_time: 0.000000 Rows_sent: 56 Rows_examined: 5462
join查询: Query_time: 0.000996 Lock_time: 0.000996 Rows_sent: 56 Rows_examined: 256
但3个表的子查询:
课件中的子查询:
select title, release_year, length from film where film_id in(select film_id from film_actor where actor_id in (select actor_id from actor where first_name = 'sandra'));
我自己写的连接查询:
select title, release_year, length from film join film_actor join actor on (film.film_id = film_actor.film_id and film_actor.actor_id = actor.actor_id and actor.first_name = 'sandra');
执行日志:
子查询: Query_time: 0.005999 Lock_time: 0.000000 Rows_sent: 56 Rows_examined: 1000
join查询: Query_time: 0.008032 Lock_time: 0.000000 Rows_sent: 56 Rows_examined: 11924
请问,为什么这里,子查询的性能要优于连接查询?
命中率低,因为film和actor是多对多关系吧(我没看具体的表结构)?导致查询的IO大,所以性能低
性能优化之MySQL优化
101639 学习 · 221 问题
相似问题