关于SQL优化中的In和Exists
关于SQL优化中的In和Exists
SQL语句:
select b.text from basetable b , extable e
where b.id=e.id
select b.text from basetable b where b.id in (select e.id from extable e)
很多人都说使用in比较快,这是事实,因为笛卡尔积比较小
有人说使用exist更快:
select b.text from basetable b where exists (select 'x' from extable e where b.id=e.id)
经过测试(百万数据),使用exist的确快
但是,如果将b.text修改为count(b.text),也就是统计条数的话,就没有什么优势了
我想问两个问题
1、为什么exists会比in快
2、为什么使用count来统计的时候会没有优势
谢谢