I\'m writing some SQL in a stored procedure to reduce a dataset to a limited random number of rows that I want to report on.
The report starts with a Group
It is undeterministic how many times the SELECT
statement involving NEWID()
will be executed.
If you get a nested loops anti semi join between QueryResults
and cte_temp
and there is no spool in the plan it will likely be re-evaluated as many times as there are rows in QueryResults
this means that for each outer row the set that is being compared against with NOT IN
may be entirely different.
Instead of using a CTE you can materialize the results into a temporary table to avoid this.
INSERT INTO #T
SELECT TOP(@SampleLimit) UserId
FROM QueryResults
WHERE ( GroupId = @GroupId )
GROUP BY UserId
ORDER BY NEWID()
Then reference that in the DELETE