5

我的數據庫正在變老,我最大的INT IDENTITY列中有一個值約爲13億。這將溢出21億美元左右。我計劃增加它的大小,但由於數據庫中的記錄數量太多,我不想過早這樣做。在增加列大小之前,我可能會更換我的數據庫硬件,這可能會抵消可能導致的任何性能問題。我還希望關注數據庫中超過50%的所有其他列。這是很多表格,並且手動檢查每個表格都是不實際的。如何輕鬆找到有溢出危險的IDENTITY列?

這是我怎麼現在所獲得的價值(我知道返回可能會稍微過時的值,但它的不夠好,我的目的):

PRINT IDENT_CURRENT('MyDatabase.dbo.MyTable') 

我可以使用INFORMATION_SCHEMA得到這個信息?

+11

[這裏](http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer-之前 - 問)和[這裏](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/)是爲什麼。根據SO常見問題解答,這種行爲不僅可以接受,而且是受到鼓勵的。我將繼續這樣做,直到SO社區被認爲是不禮貌的禮節。 – 2012-01-05 16:28:20

+0

這不是一個答案,但你是否從0或1開始了你的身份?如果是,那麼您是否考慮將身份重置爲最小的int,而不是增加列大小?這會給你額外的20億列相同的大小。 – 2012-01-05 16:18:24

+0

@AlexKuznetsov:是的,我考慮過了。我可能會在未來的項目中使用負值,但爲這個數據庫編寫的代碼太多了,我不想冒這個風險。 – 2012-01-05 16:23:21

回答

11

您可以諮詢sys.identity_columns系統目錄視圖:

SELECT  
    name, 
    seed_value, increment_value, last_value 
FROM sys.identity_columns 

這給了你的名字,種子,增量和去年值爲每個柱。該視圖還包含數據類型,因此您可以輕鬆找出哪些標識列可能會盡快用完數字...

+0

請注意,對於跨數據庫視圖,last_value可能會錯誤地報告爲空。 IDENT_CURRENT(object_name(object_id))將返回正確的last_value。 – jmoreno 2017-02-21 13:29:30

5

我創建了一個存儲過程來解決這個問題。它使用INFORMATION_SCHEMA找到IDENTITY列,然後使用IDENT_CURRENT和列的DATA_TYPE來計算完整百分比。指定數據庫作爲第一個參數,然後選擇最小百分比和數據類型。

EXEC master.dbo.CheckIdentityColumns 'MyDatabase' --all 

EXEC master.dbo.CheckIdentityColumns 'MyDatabase', 50 --columns 50% full or greater 

EXEC master.dbo.CheckIdentityColumns 'MyDatabase', 50, 'int' --only int columns 

輸出示例:

Table      Column    Type Percent Full Remaining 
------------------------- ------------------ ------- ------------ --------------- 
MyDatabase.dbo.Table1  Table1ID   int  9   1,937,868,393 
MyDatabase.dbo.Table2  Table2ID   int  5   2,019,944,894 
MyDatabase.dbo.Table3  Table3ID   int  9   1,943,793,775 

我創建了一個提醒,檢查我所有的數據庫,每月一次,我登錄電子表格信息。

IDENTITY tracking spreadsheet

CheckIdentityColumns程序

USE master 
GO 

CREATE PROCEDURE dbo.CheckIdentityColumns 
    (
    @Database  AS NVARCHAR(128), 
    @PercentFull AS TINYINT   = 0, 
    @Type   AS VARCHAR(8)  = NULL 
    ) 

AS 

--this procedure assumes you are not using negative numbers in your identity columns 

DECLARE @Sql NVARCHAR(3000) 

SET @Sql = 
'USE ' + @Database + ' 

SELECT       
    [Column].TABLE_CATALOG + ''.'' + 
    [Column].TABLE_SCHEMA + ''.'' + 
    [Table].TABLE_NAME   AS [Table], 
    [Column].COLUMN_NAME      AS [Column], 
    [Column].DATA_TYPE    AS [Type], 
    CAST((
    CASE LOWER([Column].DATA_TYPE) 
    WHEN ''tinyint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/255) 
    WHEN ''smallint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/32767) 
    WHEN ''int'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/2147483647) 
    WHEN ''bigint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/9223372036854775807) 
    WHEN ''decimal'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/(([Column].NUMERIC_PRECISION * 10) - 1)) 
    END * 100) AS INT)     AS [Percent Full], 
    REPLACE(CONVERT(VARCHAR(19), CAST(
    CASE LOWER([Column].DATA_TYPE) 
    WHEN ''tinyint'' 
    THEN (255 - IDENT_CURRENT([Table].TABLE_NAME)) 
    WHEN ''smallint'' 
    THEN (32767 - IDENT_CURRENT([Table].TABLE_NAME)) 
    WHEN ''int'' 
    THEN (2147483647 - IDENT_CURRENT([Table].TABLE_NAME)) 
    WHEN ''bigint'' 
    THEN (9223372036854775807 - IDENT_CURRENT([Table].TABLE_NAME)) 
    WHEN ''decimal'' 
    THEN ((([Column].NUMERIC_PRECISION * 10) - 1) - IDENT_CURRENT([Table].TABLE_NAME)) 
    END 
    AS MONEY) , 1), ''.00'', '''')    AS Remaining 


FROM      
    INFORMATION_SCHEMA.COLUMNS     AS [Column] 

    INNER JOIN  
    INFORMATION_SCHEMA.TABLES     AS [Table] 
    ON  [Table].TABLE_NAME     = [Column].TABLE_NAME 

WHERE 
    COLUMNPROPERTY(
     OBJECT_ID([Column].TABLE_NAME), 
     [Column].COLUMN_NAME, ''IsIdentity'') = 1 --true 
    AND [Table].TABLE_TYPE      = ''Base Table'' 
    AND [Table].TABLE_NAME      NOT LIKE ''dt%'' 
    AND [Table].TABLE_NAME      NOT LIKE ''MS%'' 
    AND [Table].TABLE_NAME      NOT LIKE ''syncobj_%'' 
    AND CAST(
    (
    CASE LOWER([Column].DATA_TYPE) 
    WHEN ''tinyint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/255) 
    WHEN ''smallint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/32767) 
    WHEN ''int'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/2147483647) 
    WHEN ''bigint'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/9223372036854775807) 
    WHEN ''decimal'' 
    THEN (IDENT_CURRENT([Table].TABLE_NAME)/(([Column].NUMERIC_PRECISION * 10) - 1)) 
    END * 100 
    ) AS INT)         >= ' + CAST(@PercentFull AS VARCHAR(4)) 

IF (@Type IS NOT NULL) 
    SET @Sql = @Sql + 'AND LOWER([Column].DATA_TYPE) = ''' + LOWER(@Type) + '''' 

SET @Sql = @Sql + ' 

ORDER BY 
    [Column].TABLE_CATALOG + ''.'' + 
    [Column].TABLE_SCHEMA + ''.'' + 
    [Table].TABLE_NAME, 
    [Column].COLUMN_NAME' 

EXECUTE sp_executesql @Sql 
GO 
2

Keith Walton有一個非常全面的查詢,非常好。下面是基於這樣的假設標識列都是整數稍微簡單一個:

SELECT sys.tables.name AS [Table Name], 
    last_value AS [Last Value],  
    MAX_LENGTH, 
    CAST(cast(last_value as int)/2147483647.0 * 100.0 AS DECIMAL(5,2)) 
     AS [Percentage of ID's Used], 
    2147483647 - cast(last_value as int) AS Remaining 
FROM sys.identity_columns 
    INNER JOIN sys.tables 
     ON sys.identity_columns.object_id = sys.tables.object_id 
ORDER BY last_value DESC 

結果將是這樣的:

Table Name  Last Value  MAX_LENGTH Percentage of ID's Used Remaining 
My_Table  49181800    4    2.29    2098301847 

Checking Integer Identity Columns

0

雖然這個各具特色a solution問題,我們發現這個線程既豐富又有趣(我們也寫了一個詳細的post about this並描述了我們的工具是如何工作的)。

在我們的解決方案中,我們正在查詢information_schema以獲取 所有列的列表。然後我們編寫了一個程序,通過它們中的每一個計算最大值和最小值(我們計算溢出和下溢)。

SELECT 
    b.COLUMN_NAME, 
    b.COLUMN_TYPE, 
    b.DATA_TYPE, 
    b.signed, 
    a.TABLE_NAME, 
    a.TABLE_SCHEMA 
FROM (
    -- get all tables 
    SELECT 
    TABLE_NAME, TABLE_SCHEMA 
    FROM information_schema.tables 
    WHERE 
    TABLE_TYPE IN ('BASE TABLE', 'VIEW') AND 
    TABLE_SCHEMA NOT IN ('mysql', 'performance_schema') 
) a 
JOIN (
    -- get information about columns types 
    SELECT 
    TABLE_NAME, 
    COLUMN_NAME, 
    COLUMN_TYPE, 
    TABLE_SCHEMA, 
    DATA_TYPE, 
    (!(LOWER(COLUMN_TYPE) REGEXP '.*unsigned.*')) AS signed 
    FROM information_schema.columns 
) b ON a.TABLE_NAME = b.TABLE_NAME AND a.TABLE_SCHEMA = b.TABLE_SCHEMA 
ORDER BY a.TABLE_SCHEMA DESC;