April 25, 2012 at 8:07 pm
Comments posted to this topic are about the item Swiss Cheese!
Cheers,
John Esraelo
May 22, 2012 at 3:18 am
The Cursor has massive overhead.
Have you considered CTE and Ranking functions in a set based operation:
Ideally have an Index:
CREATE CLUSTERED INDEX idx_ID ON MyDB.dbo.Employees(ID);
Create a CTE that assigns row numbers to rows based on seqval ordering. The
outer query then joins two instances, matching current and next values based on an offset of
1 between their row numbers.
WITH CTE AS
(
SELECT ID, ROW_NUMBER() OVER(ORDER BY ID) AS rownum
FROM MyDB.dbo.Employees
)
SELECT Cur.ID + 1 AS start_range, Nxt.ID- 1 AS end_range
FROM CTE AS Cur
JOIN CTE AS Nxt
ON Nxt.rownum = Cur.rownum + 1
WHERE Nxt.ID - Cur.ID > 1;
or if your ID List has duplicates :
WITH CTE AS
(
SELECT ID, ROW_NUMBER() OVER(ORDER BY ID) AS rownum
FROM (SELECT DISTINCT ID FROM MyDB.dbo.Employees) AS D
)
SELECT Cur.ID + 1 AS start_range, Nxt.ID - 1 AS end_range
FROM CTE AS Cur
JOIN CTE AS Nxt
ON Nxt.rownum = Cur.rownum + 1
WHERE Nxt.ID - Cur.ID > 1;
This will run about 10 times faster than a Cursor, Tested on 10000000 Rows with 9,999 Gaps (Ran on my system in around 23 seconds).
If you need to return the list of individual missing values as opposed to missing ranges, using a Nums table (Every database NEEDS a Nums table) makes the task is very simple:
SELECT n FROM dbo.Nums
WHERE n BETWEEN (SELECT MIN(ID) FROM MyDB.dbo.Employees)
AND (SELECT MAX(ID) FROM MyDB.dbo.Employees)
AND n NOT IN(SELECT ID FROM MyDB.dbo.Employees);
May 10, 2016 at 1:56 pm
Thanks for the script.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply