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

一个查询的效率
begin  
print   CONVERT(VARCHAR(30),GETDATE(),9)
declare   @t   table(n   int,id   int,num   int,sum1   int,list1   int)
insert   into   @t   values(1,1,1,0,0)
insert   into   @t   values(2,2,3,0,0)
insert   into   @t   values(3,3,2,0,0)
insert   into   @t   values(4,2,2,0,0)
insert   into   @t   values(5,2,1,0,0)
--这里的数据是假的,正常有1000条左右

update   @t
set   sum1=id+num

print   CONVERT(VARCHAR(30),GETDATE(),9)
/********************************************************
--下面这句用于排列名次,效率很低,怎么做能提高效率??????
********************************************************/
update   @t
set   list1=x.m
from   (
select   t1.n   as   nx,sum(   case   when   t2.sum1> t1.sum1   then   1   else   0   end)+1   as   m   from   @t   t1,@t   t2   group   by   t1.n   )  
x
where   n=x.nx
print   CONVERT(VARCHAR(30),GETDATE(),9)
select   *   from   @t
end  



------解决方案--------------------
這樣呢?

begin
print CONVERT(VARCHAR(30),GETDATE(),9)
declare @t table(n int,id int,num int,sum1 int,list1 int)
insert into @t values(1,1,1,0,0)
insert into @t values(2,2,3,0,0)
insert into @t values(3,3,2,0,0)
insert into @t values(4,2,2,0,0)
insert into @t values(5,2,1,0,0)
--这里的数据是假的,正常有1000条左右

update @t
set sum1=id+num

print CONVERT(VARCHAR(30),GETDATE(),9)
/********************************************************
--下面这句用于排列名次,效率很低,怎么做能提高效率??????
********************************************************/

update x
set list1= (Select Count(n) + 1 From @t Where sum1 > x.sum1)
From @t x
select * from @t
end