select * from table1 where ID in (select ID from table1 where id>1000 except select id from table2 where ..... )
程序第一次运行时总是因数据量大而超时,然后再运行一下才出结果。放在数据库中运行正常出结果。
两个表中ID都有索引
我想是否能改写一下SQL让它运行得更有效率
抱歉只有20分了
------解决思路----------------------
SELECT T1.*试下,不知道会不会快点
FROM table1 T1
LEFT JOIN table2 T2 ON T1.id=T2.id AND .....--(table2表的条件,列名最好加前缀T2.)
WHERE T1.id>1000 AND T2.id IS NULL
------解决思路----------------------
select * from table1
where ID >1000
and not exists(select 1 from table2 where table1.ID=table2.id)
------解决思路----------------------
返回的数据量大吗?通常这种超时是因为data cache不够而blocking的
------解决思路----------------------
join的性能消耗要远小于in
------解决思路----------------------
查询分析器默认是没有超时的,就算执行一整天页不会出错。
既然程序可能会出错,说明超时时间太短,应该把默认的 CommandTimeout 改大点。