Comments on: SQL Server Identity Values Check https://thomaslarock.com/2015/11/sql-server-identity-values-check/ Thomas LaRock is an author, speaker, data expert, and SQLRockstar. He helps people connect, learn, and share. Along the way he solves data problems, too. Wed, 22 Mar 2023 15:02:15 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Doug https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-91966 Wed, 22 Mar 2023 15:02:15 +0000 http://thomaslarock.com/?p=17186#comment-91966 In reply to Doug.

In my particular situation, it is difficult to get a precise % identities used for a specific table that I had to reseed. This is the best script I have seen out there that attempts to take into account many scenarios, but I think that Microsoft simply doesn’t give enough information in the system catalog views to meet all scenarios.

I have an INT identity column. I am 91.6% filled up with positive identities. I then reseeded the table to -2147483648 and it is counting back up to 0 while the dev teams work on a permanent fix.

Using the catalog views in this script, it calculates that I have -99% left in the table. That is obviously wrong, but it is due to the limitation of MS’s catalog views and the information they show.

What is truly needed is a script that goes out and counts the number of identities used on the positive side and add them to the number of identities used on the negative side (including identities deleted from the table but are still used) and then divide that number by 4294967295 and multiple by 100 to get an accurate percent of identities used. However, we know that process would likely take forever and row counts do not necessary equal used identity values.

I have implemented a “fix” on this script (at least for me) that I know is buggy to try and ascertain how many identities I have left by using just the system catalog views.

Going to share it here in case anyone else is interested. I know it is not accurate, but it does get me close to the actual used % of identities.

, CAST(CASE
WHEN (Increment_Value > 0 AND Last_Value > 0) THEN (100)*(1.0-(CAST(Max_Value-Last_Value AS FLOAT)/CAST(Max_Value-Seed_Value + 1.0 AS FLOAT)))
WHEN (Increment_Value > 0 AND Last_Value < 0) THEN ABS((100)*(1.0-(CAST(FLOOR((Max_Value – Min_Value)/ABS(Increment_Value))-((Min_Value-Last_Value)-Row_Count) AS FLOAT)/CAST(FLOOR((Max_Value – Min_Value)/ABS(Increment_Value))-Seed_Value + 1.0 AS FLOAT))))
ELSE (100)*(1.0-(CAST(Min_Value-Last_Value AS FLOAT)/CAST(Min_Value-Seed_Value + 1.0 AS FLOAT)))
END AS DECIMAL(10,8)) AS [Current_Inserts_Used_Pct_New]

]]>
By: Doug https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-91953 Mon, 20 Mar 2023 18:46:33 +0000 http://thomaslarock.com/?p=17186#comment-91953 I know this is a dated article, but I thought you might be interested in a fringe case scenario.

I have a table that is filling up. The original table identity was seeded as (1,1). The table is about full (91.6%) after almost 5 years (only positive identities). In order to give them more time to create a better solution, I am reseeding the table to -2147483648 identity so that it can continue to increment back to 1.

I believe this fringe case breaks the script for that table. Here is the output as taken from excel (sorry about the lack of format, but I am sure you will be able to match up the columns to the values). Note I only made the changes in a non-production table which is barely used, as I await QA testing. But the script says there is -99% of the table used.

Thought you would be interested in know this given that solutions to tables filling up and lack of time to fix them recommend reseeding.

Schema_Name Table_Name Column_Name Row_Count Total_Possible_Inserts Inserts_Remaining Inserts_Possible Current_Inserts_Used_Pct Date_You_Will_Run_Out Is_Unique Increment_Value Last_Value Min_Value Max_Value Seed_Value
dbo CSE_TrinityData TrinDataKey 13126003 4294967295 4294877751 2147483647 -99.99583033 1900-01-01 PK 1 -2147394104 -2147483648 2147483647 1

]]>
By: Doug https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-91951 Mon, 20 Mar 2023 15:17:27 +0000 http://thomaslarock.com/?p=17186#comment-91951 I know this is an old article, but I have a fringe case for you…. something that doesn’t come up very often, but thought you may like to know.

I have a table with a INT primary key and IDENTITY(1,1). Identity usage was at 91.6% of the just the positive identities, and I have reseeded the table to the negative, minimal range of -2147483648 so that ti starts counting up to 1. We will be working on the new solution well before the negative keys are used up, but needed a small buffer to give us more time.

In any case the script comes with a negative percent used. I’m sure because the seed value always remains as 1 even if the table is reseeded is the cause of it, but I have not dug into the calculations to find out why.

See script output below. I know it probably won’t show nicely (I copied right from excel), but you’ll probably be able to figure out that values for the columns

Schema_Name Table_Name Column_Name Row_Count Total_Possible_Inserts Inserts_Remaining Inserts_Possible Current_Inserts_Used_Pct Date_You_Will_Run_Out Is_Unique Increment_Value Last_Value Min_Value Max_Value Seed_Value
dbo CSE_TrinityData TrinDataKey 13119043 4294967295 4294884711 2147483647 -99.99615443 1/1/1900 PK 1 -2147401064 -2147483648 2147483647 1

]]>
By: SQL Server Identity Values Check on GitHub - Thomas LaRock https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-15672 Wed, 21 Dec 2016 14:38:42 +0000 http://thomaslarock.com/?p=17186#comment-15672 […] year I blogged about a script that Karen López (blog | @datachick) and I wrote together to help you determine if you were […]

]]>
By: Use Compression to Combine Data Quality and Performance - SQL Server - SQL Server - Toad World https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-13518 Mon, 21 Dec 2015 13:18:46 +0000 http://thomaslarock.com/?p=17186#comment-13518 […] at your existing databases to see if you’re running out of numbers using Tom’s post SQL Server Identity Values Check.  You might find that you already have a case where there’s a 4-byte column about to hit […]

]]>
By: ThomasLaRock https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-13511 Wed, 09 Dec 2015 15:16:00 +0000 http://thomaslarock.com/?p=17186#comment-13511 In reply to Calvin Jones.

Weird, but I think I’ve fixed this as well, have another try if you can and let me know.

]]>
By: Calvin Jones https://thomaslarock.com/2015/11/sql-server-identity-values-check/#comment-13510 Mon, 07 Dec 2015 16:16:00 +0000 http://thomaslarock.com/?p=17186#comment-13510 In reply to ThomasLaRock.

Here is the column declaration: [Error_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,

]]>