sql2000中的连接两个表的查询语句的执行路径对性能的影响
有两个表
表1 pay_itemdetail 大概 2-3百万条数据
表2 pay_builddetail 大概 55 条数据
1原语句,效率非常低,用时20秒以上
select * from pay_itemdetail with(nolock)
where builditemid in (select id from pay_builddetail where buildid = 1)
2 改进的语句求count ,又两种方法,连接,用时1秒中
1)in 的方式
select count(1) from pay_itemdetail with(nolock)
where builditemid in (select id from pay_builddetail where buildid = 1)
2)连接的方式
select count(1) from pay_itemdetail a with(nolock),pay_builddetail b with(nolock)
where b.buildid=100 and a.builditemid = b.id
3 最后的改进 top ,用时0秒
select top 1 a.* from pay_itemdetail a with(nolock),pay_builddetail b with(nolock)
where b.buildid=1 and a.builditemid =b.id
4 求top时如果没有记录匹配,速度很慢,用时10秒
select top 1 a.* from pay_itemdetail a with(nolock),pay_builddetail b with(nolock)
where b.buildid=100 and a.builditemid =b.id
5. 对4进行进一步分析祥解
用
select top 1 a.* from pay_itemdetail a with(nolock),pay_builddetail b with(nolock)
where b.buildid=1 and a.builditemid =b.id
来取第一条符合条件的 pay_itemdetail 中的数据,当 b.buildid=1 条件成立时,执行时间只用0秒;