0

I am executing the code below for a table with 2 million plus records but NewID() is taking a long time to run.

Can you suggest an alternate method to make the query faster?

DELETE FROM Table1
WHERE ID NOT IN (SELECT TOP 60000 ID FROM Table1 
WHERE flag <> '1'
ORDER BY NEWID()) 
AND
ID NOT IN(SELECT ID FROM Table1
WHERE flag = '1');
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
Sam
  • 103
  • 7

1 Answers1

0

How about this... should be twice as fast

DELETE FROM Table1
WHERE ID NOT IN (SELECT TOP 60000 ID FROM Table1 
                 WHERE flag <> '1'
                 ORDER BY NEWID()) 
   AND flag != '1';
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I started to suggest the same, but if there are duplicate IDs in the table, with different values for flag, the results will not be the same. – Tab Alleman Jan 09 '15 at 19:03