问答详情
源自:2-8 子查询的优化

请问如下子查询的优化,为什么性能变差。

如果是两个表之间的连接查询:

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


请问,为什么这里,子查询的性能要优于连接查询?



提问者:阿理理 2020-06-23 10:01

个回答

  • 一缕阳光v520
    2020-07-26 13:57:51

    命中率低,因为film和actor是多对多关系吧(我没看具体的表结构)?导致查询的IO大,所以性能低