现在有两个表,都有10万条记录A表
--------------------
id name age sexB表
-----------------------
id address phone birth其中B表中的ID集合,是A表ID的子集。
现在要执行如下语句:
select count(*) from A where id in (select id from B)发现效率很低,怎么改比较好。各位兄弟给点建议,多谢!
--------------------
id name age sexB表
-----------------------
id address phone birth其中B表中的ID集合,是A表ID的子集。
现在要执行如下语句:
select count(*) from A where id in (select id from B)发现效率很低,怎么改比较好。各位兄弟给点建议,多谢!
select count(*) from A where and age>18 and age<60 and id in (select id from B)
2.对经常出现在条件中的非主键字段,比如age,建索引;
还有别的方法吗?
然后用inner join来查询
select * from a,b where a.id=b.id在我的应用中好像效率有限。
经测试exists子句确实效率更高。
一般采用“联机视图”来替代“IN”操作。语法如下:
select count(*) from A,(select id from B) x
where A.ID = x.ID
and ...其中select id from b为联机视图,你可以在这里添加各种条件。
其次是exists语句的效率是最高的。
这种数据量的情况下,不要用in去实现,效率太差。
如果你的内存够大的话,内连接会比exists效率高很多。
在我本地的测试中楼上的sql是要经历两个Table Access Full的。
而是用等值连接只需要对较小的表作一个Table Access Full,对另一个表则是by index的。
希望楼主能贴出两种SQL的执行计划作进一步分析。