此 SQL 游标的改进 - 它真的很慢!
我有一个使用游标的 sql 过程,但它真的很慢。我希望通过使用基于集合的操作或类似的东西来提高速度,但我不知道如何将其应用到这个特定的过程:
declare @isMulti bit
declare @QuestionID int
declare db_cursor cursor FAST_FORWARD for
select distinct QuestionID
from tblQuestions (nolock)
where ID=@ID
open db_cursor
fetch next from db_cursor into @QuestionID
while @@FETCH_STATUS = 0
begin
--check if @isMulti is true or not for the current question
if(@isMulti=1)
begin
update tblAnswers
set col1 = 1, col2 = 1, col3 = (select count(*) from tblAnswers where QuestionID=@QuestionID and ID=@ID)
end
else if(@isMulti=0)
begin
update tblAnswers
set col1 = AnswerID, col2 = 1, col3 = (select LEN(count(*)) from tblAnswers where QuestionID=@QuestionID and ID=@ID)
end
fetch next from db_cursor into @QuestionID
end
close db_cursor
deallocate db_cursor
感谢您提供的任何帮助!
I have a sql procedure in which I use a cursor, but it's really really slow. I'm looking to improve the speed by using set-based operation or such things, but I've no idea how to apply it to this specific procedure:
declare @isMulti bit
declare @QuestionID int
declare db_cursor cursor FAST_FORWARD for
select distinct QuestionID
from tblQuestions (nolock)
where ID=@ID
open db_cursor
fetch next from db_cursor into @QuestionID
while @@FETCH_STATUS = 0
begin
--check if @isMulti is true or not for the current question
if(@isMulti=1)
begin
update tblAnswers
set col1 = 1, col2 = 1, col3 = (select count(*) from tblAnswers where QuestionID=@QuestionID and ID=@ID)
end
else if(@isMulti=0)
begin
update tblAnswers
set col1 = AnswerID, col2 = 1, col3 = (select LEN(count(*)) from tblAnswers where QuestionID=@QuestionID and ID=@ID)
end
fetch next from db_cursor into @QuestionID
end
close db_cursor
deallocate db_cursor
Thanks for any help provided!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
泰德,我相信缓慢的原因可能是,除了您使用游标的事实之外,每次通过游标都会更新完整的 tblAnswers 。我预计答案表中有多行,因为在设计过程中使用了游标。在决定从游标更改为基于集合的操作之前,您是否会考虑向答案表的更新添加 WHERE 子句。
关于我的答案
如果 udf 很昂贵,那么我会在问题表中添加一列,或者如果无法修改问题表,则创建一个新表。在插入或更新问题时使用触发器,使用“multi”标志和函数结果填充新列。
使用下面的代码作为模型更新 sp 中的答案表。使用问题 ID 和“multi”标志的值调用 SP。
Ted, I believe the cause of the slowness may be, beside the fact you are using a cursor, that the complete tblAnswers is updated every time through the cursor. I am expecting there are multiple rows in the answer table because a cursor was used during the design. Until the decision is made on the changing from a cursor to a set-based op would you consider adding a WHERE clause to the update for the answer table.
On to my answser
If the udf is expensive then I would add a column to the questions table or create a new table if modifing the question table is not possible. Populate the new column with a flag for "multi", with the result of the function, using a trigger when the question is inserted or updated.
Update the answers table in a sp using the code below as a model. Call the SP with the question ID and the value of the "multi" flag.
我可能遗漏了一些东西,但是如果您从 where 子句中取出 @QuestionId,为什么这个位不能在光标之外工作?:
编辑
不了解有关我不确定的元数据的更多信息如何处理问题的多元素,但这应该是答案的好方法:
I may be missing something, but why doesn't this bit work outside the cursor if you take out the @QuestionId from the where clause?:
EDIT
Without knowing more about the metadata I'm not sure about how to process the multi element to the questions, but this should be a good way to the answer:
您可以通过连接更新答案表,即连接 ID 上的问题答案,然后使用 where 子句限制 @ID。
You can update the answer table across a join, i.e join Answers to Questions on the ID and then restrict on the @ID using a where clause.