在sql server 2k5上优化SP

时间:2012-01-18 14:10:39

标签: tsql

我问了一个问题query large table。这是对同一个问题的回应。 where子句中有4列。我用while循环替换了光标。但没多大帮助。旧代码和新代码之间的差异仅为1秒。但有一点我注意到了。有一列[col name] varchar(255) allow nulls。如果我不查询该列,则新代码的执行时间为一半。可以做些什么来提高速度? where子句中的所有列都已编制索引。任何帮助表示赞赏。

而'loop是示例代码。

declare @temp table (col1 INT PRIMARY KEY,
            col2 VARCHAR(10),
            col3 VARCHAR(40),
            col4 VARCHAR(40)    )
INSERT INTO @temp (col1,col2,col3,col4)              
SELECT col1,col2,SUBSTRING(col3,1,40),col4 
FROM dbo.Clients WHERE col5 <> 4
ORDER BY col1
select @from=1,@to=@@rowcout
SELECT TOP 1 @col1 = col1,@col2=col2,@col3=col3,@col4=col4 FROM @Temp ORDER BY col1
while @from <= @to
begin
    insert into table2 (....) select ... from table3 where [col name1] = par1 and ([col name2] =par2 or [col name2] = par3] )
    --this one takes time
    insert into table2 (...)  select .. from table3 where [col name] like @par1 AND ([col name] like par2 or [col name] like par3)
SELECT TOP 1 @col1 = col1,@col2=col2,@col3=col3,@col4=col4 FROM @Temp where col1 > @col1 ORDER BY col1
set @from = @from + 1
end

0 个答案:

没有答案