select * from b where id in (select id from a where leibie=1)
select * from b , a where b.id=a.id and a.leibie=1
----------------------------------------------------------
B
select (select mc from b where b.id=a.id) from a
select b.mc from a, b where a.id=b.id
上诉A,B中的两种方案,哪种更效率呢 ? 为什么呢?~ 个中有缺点又是什么呢?~
...全文
2804打赏收藏
讨论SQL语句联合查询的效率问题
A select * from b where id in (select id from a where leibie=1) select * from b , a where b.id=a.id and a.leibie=1 ---------------------------------------------------------- B select (select mc from b where b.id=a.id) from a select b.mc from a, b whe
A方案还是要看情况的
in查询不一定会比两表连接要慢,这取决与子查询返回的记录数,如果子查询返回的记录数量非常少的情况下,b 表非常大的情况下in查询也是非常快的
B方案
select (select mc from b where b.id=a.id) from a 是相关子查询,它会对a表做全表扫描,并且会一条一条的扫描,如果与b表有id相等的时候才会返回记录。如果a表很小只有不到10条记录而b表非常大,执行的效率也会非常快,反之a表非常大效率就慢了