sql2000中的连接两个表的查询语句的执行路径对性能的影响

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秒中

   1in 的方式

     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秒;

你可能感兴趣的:(sql2000)