首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >低速SQL查询

低速SQL查询
EN

Stack Overflow用户
提问于 2014-08-09 13:53:58
回答 1查看 68关注 0票数 1

我在Server中有16000行,我想将文档号物理地分配给记录,并且使用下面的代码,当我的记录是8000时,它可以工作得很好,但是当记录增加到16000时,它会给我一个超时错误,请帮助我提高我的查询的性能?

代码语言:javascript
复制
declare @NewDoc int;
set @NewDoc=0
declare t1_cursor cursor dynamic for select  documentheaderid,documentnumber from Accounting.DocumentHeader where FinancialPeriodFK=@FinancialPeriodFK and Date>=@FromDate and Date<=@ToDate order by Date
open t1_cursor

fetch next from t1_cursor
while(@@fetch_status=0)
begin
set @NewDoc=@NewDoc+1;


update  Accounting.DocumentHeader set DocumentNumber=@NewDoc where current of t1_cursor
fetch next from t1_cursor
end 
close t1_cursor
deallocate t1_cursor
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-09 14:31:25

尝试这样的方法--避免任何row-by-agonizing-row处理(如游标或while循环),避免

代码语言:javascript
复制
-- creates a temporary "inline" view of the data you're interested in and 
-- assigns each row a consecutive number
;WITH DataToUpdate AS
(
    SELECT
        DocumentHeaderId,
        DocumentNumber,
        NewDocNum = ROW_NUMBER() OVER(ORDER BY [Date])
    FROM
        Accounting.DocumentHeader
    WHERE
        FinancialPeriodFK = @FinancialPeriodFK
        AND Date >= @FromDate
        AND Date <= @ToDate
)
-- update the base table to make use of that new document number 
UPDATE dh
SET dh.DocumentNumber = dtu.NewDocNum
FROM Accounting.DocumentHeader dh
INNER JOIN DataToUpdate dtu ON dh.DocumentHeaderId = dtu.DocumentHeaderId

这应该是显着地加快您的处理时间的

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25219283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档