solving sql server problems for millions of dbas and developers since 2006


Identify and resolve SQL Server problems BEFORE they happen with SQL diagnostic manager

SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers


SQL Product Highlight

Idera - SQL diagnostic manager

SQL diagnostic manager is a powerful performance monitoring and diagnostics solution that proactively alerts administrators to health, performance or availability problems within their SQL Server environment via a central console or mobile device. SQL diagnostic manager minimizes costly server downtime by providing agent-less, real-time monitoring and customizable alerting for fast diagnosis and remediation of SQL Server performance and availability problems. SQL diagnostic manager also provides a 'community' environment where, using Idera's IntelliFeed(TM) technology DBAs form a community t

Learn more!








Gaps in SQL Server Identity Columns

By: | Read Comments (1) | Print

Chad is an Architect, Administrator and Developer with technologies such as SQL Server, .NET, and Windows Server.

Related Tips: More

Problem
I have a table that makes use of integer based sequential values (identity based or otherwise) and I need to determine what gaps exist within this table. What is the best way to determine these gaps using a set-based approach (i.e. no cursor operations)?

Solution
A variety of simple queries can be used with any version of SQL Server to determine the first-level gaps within a given integer-based column in a table, some of which are more scalable than others. My favorite, and probably the most scalable solution I've found is a morph of a solution created by Umachandar Jayachandran.

Let's setup an example with this code and then begin to issues the queries below.

SELECT a.pkid + 1 as GapValue
FROM t1 a
WHERE NOT EXISTS(
SELECT * FROM t1 b
WHERE b.pkid = a.pkid + 1 )
AND a.pkid < (select max(pkid) from t1);

The query above will give you the minimum integer value (first-level gap) for every gap in the pkid column from table t1. The query from above will return the following result:

GapValue
-----------
4
8
14
27
31

This is a great start, however, there is at least 1 fairly problematic limitation with the above: you will never get a result set that contains all ID’s in a multiple-value gap range, you will only get the first ID of a gap range (for example, notice in the data that the first gap starts at 4 as output in the result set, however there also is no 5 or 6 in the data, but the results of the query only provide the first value in a gap range). This can pose a significant problem, for example, if you are trying to insert records into the table containing the gaps based of a query from data in another table.  You may still end up getting the data in successfully, but you will not have done so by using up all the gaps, or even the smallest values for the id's in question.

To address this issue, with SQL Server 2000 it would be extremely difficult and would require some iterative techniques. However, with SQL Server 2005 and the advent of Recursion and CTE's (recursive common table expressions), you can easily morph the solution from above to provide you what you're looking for, as follows:

declare @i int;

SELECT @i = MAX(pkid) FROM t1;

WITH tmp (gapId) AS (
SELECT DISTINCT a.pkid + 1
FROM t1 a
WHERE NOT EXISTS( SELECT * FROM t1 b
WHERE b.pkid = a.pkid + 1)
AND a.pkid < @i

UNION ALL

SELECT a.gapId + 1
FROM tmp a
WHERE NOT EXISTS( SELECT * FROM t1 b
WHERE b.pkid = a.gapId + 1)
AND a.gapId < @i
)
SELECT gapId
FROM tmp
ORDER BY gapId;

Not only will this result in a data-set including ALL the id values in all gap ranges, but is also extremely efficient and scalable when compared to other possible solutions using functions and iterative (i.e. cursor based) solutions:

gapId
-----------
4
5
6
8
14
15
16
17
18
19
20
21
22
23
24
25
27
31
32
33
34

Next Steps

  • Be sure to check out the supporting code example to understand the entire process.
  • This code is a prime example of new and enhanced approaches to resolving historical SQL Server problems with SQL Server 2005.
  • As you begin to work with SQL Server 2005, research the new coding options to expand your knowledge and improve the functionality and performance of your code.
  • Check out the following related tips on MSSQLTips.com:
  • Special thanks to Chad Boyd from the MSSQLTips.com community for this tip
     


Related Tips: More | Become a paid author


Last Update: 3/2/2007

Share: Share 






Comments and Feedback:

Tuesday, May 13, 2008 - 3:38:22 AM - ilies cristian read the tip flag as SPAM

hello guys, here is another approach:

--a simple and understandable multi level gap retrieval aproach: - u create a table and populate it will all the numbers to the max of ur queryed table after that u do a left outer join and there u are:

--zzz= ur initial table; @xxx temp table used to resolve the issue:

declare @a int

declare @xxx table (id int)

SET @a=0

DECLARE @max INT

select @max = max(id) from zzz

while @a< @max

BEGIN

INSERT INTO @xxx (id)

SELECT NULL

SET @a = @a + 1

END

SET @a = 0

--update nulls to right values: 1,2,3,4,... max(id) from ur queryed table

UPDATE @xxx

SET @a = id = @a + 1

SELECT xxx.id from

@xxx xxx left outer join zzz on zzz.id = xxx.id

WHERE zzz.id IS NULL

test: lets say zzz contains foolowing ids: 

        1    =>  ur result will be
        2                
        3              4
        5              7
        6              9
        8             10
       11



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Find and fix SQL Server problems before they happen - SQL diagnostic manager now with predictive analysis!

Optimize your SQL Server storage: compress live databases by up to 90%. Download a free trial.

Do you need some help to solve SQL Server problems you are facing?

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Free Web Cast - What Are You Waiting For? Delivered by Jason Strate on Wednesday, March 14 @ 3:00 PM EST


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com