首页 > 数据库 > > 讨论一个sql执行效率的问题
数据库版本: mysql 5.0
表结构:
create table A( id int(11), bid int(11), KEY `id` (`id`), KEY `bid` (`bid`) )create table B( id int(11), cid int(11), KEY `id` (`id`), KEY `cid` (`cid`) )
初始化:
A表有200w数据
B表有1000条数据
select * from B where cid = 1 的结果集是(1,3,5,10) ,不管cid=*,结果集都在10以内。
对比以下三个sql的执行效率:
SQL1:select a.* from A,B where a.bid = b.id and b.cid = 1SQL2:select * from A where a.bid in (select * from B where cid = 1) SQL3:select * from A where a.bid in (1,3,5,10)
我现在测试的情况是 SQL1 和 SQL 3 数据相当,SQL2最慢. explain 看到的SQL1和SQL3都走了索引,但是SQL2没走.
还要分别考虑以下2种情况:
1.B表也是大表,但是select * from B where cid = 1 的结果集还是(1,3,5,10)
2.B表也是大表,但是select * from B where cid = 1 的结果集很大
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/151/viewspace-3127953/ ,如需转载,请注明出处,否则将追究法律责任。