select top 10 * from ViewBgbh where 综合判定='/' and (报告编号 in (select 报告编号 from ViewJyxm where 结果!='')) and (报告编号 not in (select 报告编号 from ViewJyxm where 结果='')) order by 报告编号以上在sql2008中需2秒,有点长,有何优化方法(exists方法一样)
调试欢乐多
报告编号 in (select 报告编号 from viewjyxm where 结果!='')
报告编号 not in (select 报告编号 from viewjyxm where 结果='')
这两句的效果是一样的吧?
不矛盾,viewjyxm中有同一报告编号有多条数据,结果有为空的也有不为空的,我需要的是全部不为空的,并且必须在viewjyxm存在,因此需要应用上述的sql
inner join ViewJyxm as b on a.报告编号=b.报告编号 and a.综合判定='/' and b.结果!=''
inner join ViewJyxm as c on a.报告编号<>b.报告编号 and a.综合判定='/' and b.结果=''
order by 报告编号
然后再加索引,少用in ,not in之类的。
思路就是先取所有结果不为空的(一次查询),再取所有结果为空的(第二次查询),然后再做一遍检查,去掉结果为空的你的sql重复查询次数太多了