日期:2014-05-18  浏览次数:20560 次

数据查询之诡异,请高手赐教...
我数据库有这样几个表:jldinfo(1799条数据),zdjbxx(80条数据),zddainfo(15条数据),energy(134015条数据)

我用这条语句查询一秒钟就查出来:
select count(*)?
from jldinfo as c?
inner join zdjbxx as a on a.zdid=c.zdid?
inner join zddainfo as b on a.zdid=b.zdid?
inner join energy as d on d.teNumber=a.zddzm and d.measurePoint=c.zdcldh?
where d.type=2 and d.subtype=1 and datediff(day,'2012-3-18',d.copytime)=0?
and a.zdlx not in (5,6,7)
and c.dwid in ('1','2','3','4')?

但是我将上面这个语句最后那个4删掉,就要查30秒:
select count(*)?
from jldinfo as c?
inner join zdjbxx as a on a.zdid=c.zdid?
inner join zddainfo as b on a.zdid=b.zdid?
inner join energy as d on d.teNumber=a.zddzm and d.measurePoint=c.zdcldh?
where d.type=2 and d.subtype=1 and datediff(day,'2012-3-18',d.copytime)=0?
and a.zdlx not in (5,6,7)
and c.dwid in ('1','2','3')?

这个是为甚么呀?请高手赐教呀...

------解决方案--------------------
看执行计划,有没有走索引
------解决方案--------------------
1.建议对表:jldinfo,zdjbxx,zddainfo的zdid字段建立索引.
2.and a.zdlx not in (5,6,7)
? and c.dwid in ('1','2','3','4') ?
这两句改为:
and a.zdlx not between 5 and 7
and c.dwid between '1' and '4' ?




------解决方案--------------------
探讨

引用:
估计是 因为 hash join 和 nest loop join的关系


有办法解决这个问题么?