SQL查询not in,或是同一字段多个!=效率超低,怎样优化??
在一个只有1330条记录的表A中有个字段comname,对comname不等于“广州”,“深圳”,“珠海”,“中山”等条件进行筛选数据,使用!=、and进行筛选查询时效率如下:
select * from A where comname !='广州';结果:1191条,用时:0.563秒。
select * from A where comname !='广州' and A.comname !='深圳';结果:1080条,用时:0.516秒。
select * from A where comname !='广州' and A.comname !='深圳' and A.comname !='珠海';结果:1006条,用:13.065秒。 -_-!!!
select * from A where comname !='广州' and A.comname !='深圳' and A.comname !='珠海' and A.comname !='中山';结果:974条,用时:25.128秒。 -_-!!!
使用not in 进行筛选时和上面大致相同:
select * from A where comname not in ('广州');结果:1191条,用时:0.596秒。
select * from A where comname not in ('广州','深圳');结果:1080条,用时:0.547秒。
select * from A where comname not in ('广州','深圳','珠海');结果:1006条,用时:13.844秒。 -_-!!!
select * from A where comname not in ('广州','深圳','珠海','中山');结果:974条,用时:26.145秒。 -_-!!!
同一字段不等于筛选超过2个时,效率明显超低,而且个数越多用时越多。上面的结果都是多次测试,大致用时如上。
请教老鸟们是否遇到同样问题,为什么会这样,怎样去优化?
PS:数据库为oracle 10g。使用工具pl/sql。