This is kind of a part 2 to this question: http://stackoverflow.com/questions/3319842/select-rows-and-update-same-rows-for-locking
I have a table in my database that hold millions of records. I have created a stored procedure to pull X amount of records and mark them as being locked so when my application calls the next set of X amount it will only pull the records that are not locked and so and on and so on… I am finding that with millions of records this is not very efficient. The queries are taking a while to run. Does anyone have some suggestions to increase my query efficiency while keeping the same idea for locking records? Below is the stored procedure:
Set Rowcount @topCount
SELECT *
into #temp_candidates
FROM dbo.Candidates
Where isTested = 0 and
isLocked = 0 and
validated = 0 and
validationId is null
UPDATE dbo.Candidates
SET islocked = 1,
validationId = @validationId,
validationDateTime = @validationDateTime
WHERE id IN (SELECT id FROM #temp_candidates)
Select *
from dbo.Candidates
where id in (SELECT id FROM #temp_candidates) IF EXISTS (SELECT NULL FROM
tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..#temp_candidates'))
BEGIN
DROP TABLE #temp_candidates
END
END