是的,它需要是一个光标
光标的查询非常简单 - 只需几分之一秒即可完成。
但查询返回超过300,000行
第一次致电
FETCH NEXT FROM cursor
需要永远 - 比如10分钟。
发生了什么事?我该怎么做才能解决这个问题?
有时它跑得很快。
如果我有一段时间没有运行TSQL,那么有时会非常快。
set nocount off
DECLARE @randCountNew Int;
select @randCountNew = COUNT(*)
from [docSVsys] with (nolock)
where [docSVsys].[visibility] in (0)
and [docSVsys].[rand] = 1 ;
select @randCountNew;
DECLARE @sIDprecict Int;
DECLARE @randCountThis Int;
DECLARE @valueIDthis SmallInt;
DECLARE @lockIDthis TinyInt;
select 'start CRREATE predict_cursor '
DECLARE predict_cursor CURSOR FOR
SELECT [predict].[sID], [docSVenum1pred].[randCount], [docFieldLock].[lockID], [docSVenum1].[valueID]
FROM [docSVsys] as [predict] with (nolock)
left join [docSVsys] as [sample] with (nolock) on [sample].[docHash] = [predict].[docHash]
and [sample].[rand] = 1
left join [docFieldLock] with (nolock) on [docFieldLock].[sID] = [predict].[sID]
and [docFieldLock].[fieldID] = 61
and [docFieldLock].[lockID] >= 3
left join [docSVenum1pred] with (nolock) on [docSVenum1pred].[sID] = [predict].[sID]
and [docSVenum1pred].[enumID] = 61
left join [docSVenum1] with (nolock) on [docSVenum1].[sID] = [sample].[sID]
and [docSVenum1].[enumID] = 61
WHERE
([predict].[RAND] is null or [predict].[RAND] = 0)
and [predict].[textUniqueWordCount] > 10
and [predict].[visibility] in (0)
and [sample].[docHash] is null
and [docFieldLock].[sID] is null
and ([docSVenum1pred].[randCount] is null or [docSVenum1pred].[randCount] <> @randCountNew)
--and [predict].[sID] = 379045
ORDER BY [predict].[sID];
OPEN predict_cursor
Select 'FETCH NEXT FROM predict_cursor'
FETCH NEXT FROM predict_cursor INTO @sIDprecict, @randCountThis, @lockIDthis, @valueIDthis
Select 'starting cursor'
CLOSE predict_cursor;
DEALLOCATE predict_cursor;
Select 'done'
每个列都被编入索引,这些连接在PK,FK
上答案 0 :(得分:2)
确保您始终关闭并取消分配游标 - 或尽可能避免使用游标。
CLOSE predict_cursor;
DEALLOCATE predict_cursor;