x
login about faq Site discussion (meta-askssc)

Monitoring identity columns

Hey all

Just want to run something past you all...

The other day we had an issue with an application that basically maxed out the 2.1 billion+ limit in just a few hours on a identity (int) column.

I was lucky enough that this table was a staging table can could be truncated. Problem solve and application fixed.

My question is: Do you monitor on a regular basis the current identity field for any given table. I so how do you achieve this. As when I ran a DBCC CHECKIDENT on this particual column it took over 5 minutes.

Thanks

JL

more ▼

asked Jul 13 '10 at 01:19 PM in Default

sp_lock gravatar image

sp_lock
8.2k 20 26 29

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

IDENT_CURRENT is your friend :)

Edit -> Wow, that was really embarrasing. I wrote a proc a few years back to gather those stats, and it was huge. Now it's eminently simple. I've just written it here as a multi-statement function, but it could just as easily be an in-line function.

I really hope no-one saw this post before I edited it!!

CREATE FUNCTION [dbo].[fn_GetIdentityColumnValues]()
  RETURNS @Results TABLE (TableName sysname, IdentityColumnName sysname, CurrentIdentityValue [numeric](38,0))
AS
BEGIN

INSERT INTO @Results (TableName, IdentityColumnName, CurrentIdentityValue)
SELECT QUOTENAME(OBJECT_SCHEMA_NAME([object_id])) + '.' + QUOTENAME(OBJECT_NAME([object_id])), 
       QUOTENAME([name]), 
       IDENT_CURRENT(QUOTENAME(OBJECT_SCHEMA_NAME([object_id])) + '.' + QUOTENAME(OBJECT_NAME([object_id]))) FROM [sys].[identity_columns]

RETURN
END

Ok, here's another one (just a plain SELECT), that gives you the maximum value for integer and numeric / decimal columns. Note that it doesn't go all the way up to the limit on numeric and decimal columns, because POWER() epic fails when raising 10 to the power of 29 or more, and gets the wrong value on 10 ^ 28.

SELECT [table_name], [identity_column_name], [next_value], [max_value], [max_value] - [next_value] AS [values_left] FROM (
    SELECT QUOTENAME(OBJECT_SCHEMA_NAME([ic].[object_id])) + '.' + QUOTENAME(OBJECT_NAME([ic].[object_id])) AS table_name,
           QUOTENAME([ic].[name]) AS identity_column_name,
           ISNULL(CONVERT(NUMERIC (38, 0), [ic].[last_value]) + CONVERT(NUMERIC (38, 0), [ic].[increment_value]), 
                  CONVERT(NUMERIC (38, 0), [ic].[seed_value]) + CONVERT(NUMERIC (38, 0), [ic].[increment_value])) AS next_value,
           CASE WHEN system_type_id IN (48, 52, 56, 127) THEN POWER(CONVERT(NUMERIC(38, 0), 2), (max_length * 8) - 1) - 1 
                WHEN system_type_id IN (106, 108) THEN POWER(CONVERT(NUMERIC(38, 0), 10), CASE WHEN [precision] - [scale] < 29 THEN ([precision] - [scale]) - 1 ELSE 27 END) - 1 ELSE 0 END AS max_value
    FROM   [sys].[identity_columns] AS [ic]
           INNER JOIN
           [sys].[objects] AS [o]
           ON [o].[object_id] = [ic].[object_id]
    WHERE  [o].[type] = 'U'
) tableData
more ▼

answered Jul 13 '10 at 01:33 PM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.2k 56 63 87

Thanks Matt... I can post you first question if you like? In your option you do this?

Jul 13 '10 at 01:45 PM sp_lock

I do something similar, but manually. One extension I would make is to modify it so that it works out what the full range of the column is based on it's data type, and shows a percentage of how far the table is through it's identity range, and then introduce a parameter to the function to show identity rows above that threshold, or something...

Jul 13 '10 at 01:49 PM Matt Whitfield ♦♦

@Matt Whitfield This is a very useful script, I am so glad that I ran into reading this post! I also have a question which I wanted to ask for a long time: what is the difference between ident_current('the_table') and the value in the last_value column of the sys.identity_columns view corresponding to the same table?

Jul 13 '10 at 01:52 PM Oleg

@Oleg - IDENT_CURRENT will return the IDENT_SEED if there is no last value... to be honest, using last_value would probably be a better plan...

Jul 13 '10 at 02:03 PM Matt Whitfield ♦♦

@Matt Whitfield Thank you, it is good to know about ident_seed in place of lacking last value. Before, I played with inserts/rollbacks trying to find a situation when they are out of sync (so I could figure out what is the difference), but it never happened.

Jul 13 '10 at 02:29 PM Oleg
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1843
x32
x2

asked: Jul 13 '10 at 01:19 PM

Seen: 621 times

Last Updated: Jul 13 '10 at 01:19 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.