In this article, we will review PowerShell SQL Server module DBATools to identify IDENTITY columns about to reach the threshold.

在本文中,我们将回顾PowerShell SQL Server模块DBATools,以识别即将达到阈值的IDENTITY列。

总览 (Overview)

In relational database engine, it is best practice to use an IDENTITY column to auto-generate column values. We define an identity column with a seed and step value. Once we insert a row into a SQL table, it automatically increases the value for that column. We can combine identity with a primary key to provide a unique identifier for a row. We can use integers and numeric data type for the identity column. We cannot insert value in the IDENTITY column explicitly.

在关系数据库引擎中,最佳实践是使用IDENTITY列自动生成列值。 我们定义一个带有种子和步长值的标识列。 一旦我们在SQL表中插入一行,它就会自动增加该列的值。 我们可以将身份与主键结合起来,为行提供唯一的标识符。 我们可以为标识列使用整数和数字数据类型。 我们无法在IDENTITY列中明确插入值。

We can have the following data types for identity column along with the maximum values.

标识列可以具有以下数据类型以及最大值。

Data Type

Max Value

Storage

Tinyint

255

1 Byte

SmallInt

32,767

2 Bytes

Int

231-1 (2,147,483,647)

4 Bytes

BigInt

263-1 (9,223,372,036,854,775,807)

8 bytes

数据类型

最大值

存储

Tinyint

255

1字节

SmallInt

32,767

2字节

整数

2 31 -1(2,147,483,647)

4字节

大整数

2 63 -1(9,223,372,036,854,775,807)

8字节

We should use an appropriate data type to have better space utilization in the database. It is a important requirement while designing database tables.

我们应该使用适当的数据类型以更好地利用数据库中的空间。 在设计数据库表时,这是一个重要的要求。

句法 (Syntax)

We need to define identity in a column with the following parameter.

我们需要使用以下参数在列中定义身份。

IDENTITY [(Seed, increment)]

身份[(种子,增量)]

  • Seed: Initial value of IDENTITY 种子:IDENTITY的初始值
  • Increment: Next value in an IDENTITY column is the last identity value plus seed. For example, in a table, the last IDENTITY value is 5, and we have defined seed 1, then the next value will be 6 增量:IDENTITY列中的下一个值是最后一个标识值加种子。 例如,在一个表中,最后一个IDENTITY值是5,我们定义了种子1,那么下一个值将是6

Suppose you are doing an insert for SQL Table. You have defined identity on a column with data type Tinyint. You have reached the maximum limit for the identity column. If you still try to insert a row in that table, the query fails. We cannot insert any data in that situation. We either have to change the data type for that column or delete existing records and reseed IDENTITY.

假设您正在插入SQL表。 您已经在数据类型为Tinyint的列上定义了身份。 您已达到身份列的最大限制。 如果您仍然尝试在该表中插入一行,则查询将失败。 在这种情况下,我们无法插入任何数据。 我们要么必须更改该列的数据类型,要么删除现有记录并重新设定IDENTITY。

  • Note: In this article, I am using DBATools using Azure Data Studio. You can also run these commands in Windows PowerShell.
  • 注意: 在本文中,我将使用Azure Data Studio使用DBATools。 您也可以在Windows PowerShell中运行这些命令。

例 (Example)

Let us view the issue with an IDENTITY column using an example.

让我们使用示例通过IDENTITY列查看问题。

  • Use SQLSHACKDEMO
    GO
    CREATE TABLE [dbo].[DemoIdentityInsert] ([id] [tinyint] IDENTITY(1, 1) PRIMARY KEY NOT NULL,[Name] [nvarchar](20) NULL) ON [PRIMARY]
    GO
    
  • Let us verify the current maximum value in the identity column

    让我们在身份列中验证当前最大值

    select max(id) as MaxIdentityValue from  DemoIdentityInsert
    

In the following screenshot, we can see we have reached to value 255.

在以下屏幕截图中,我们可以看到我们已达到255。

We can have a maximum of 255 values in the identity column for tinyint data type. Let us try to insert one more record in this table, and we get following Arithmetic overflow error. It is because it failed to insert value 256 in the identity column.

对于tinyint数据类型,在identity列中最多可以有255个值。 让我们尝试在此表中再插入一条记录,然后得到以下算术溢出错误 。 这是因为它无法在标识列中插入值256。

We can reproduce this error using int data type as follows. In the following query, we have set an initial value for the identity column as 2147483644. It will help us to reproduce the issue with a minimum number of rows.

我们可以使用int数据类型重现此错误,如下所示。 在以下查询中,我们将身份列的初始值设置为2147483644。这将帮助我们以最少的行数重现该问题。

CREATE TABLE [dbo].[DemoIdentityInsert_1] ([id] [int] IDENTITY(2147483644 , 1) PRIMARY KEY NOT NULL,[Name] [nvarchar](20) NULL) ON [PRIMARY]
GO

We can have maximum 2147483644 values in identity column of the integer data type. Let us try to insert few records in this table. We get the similar message – Arithmetic overflow error converting IDENTITY to data type int.

在整数数据类型的标识列中,我们最多可以有2147483644个值。 让我们尝试在此表中插入一些记录。 我们得到类似的消息– 将IDENTITY转换为int数据类型的算术溢出错误。

If we are in a production environment, it might cause an outage where no users can insert data in an existing table. We should have a mechanism to identify that we are approaching near to maximum value in the identity column. We can take appropriate actions before it becomes an issue for us. We can write t-SQL code to know about identity values in our database. It requires you to be good in writing t-SQL along with knowledge about internal system tables.

如果我们在生产环境中,则可能会导致中断,导致没有用户可以在现有表中插入数据。 我们应该有一种机制来标识我们正在接近标识列中的最大值。 我们可以采取适当的措施,直到它成为我们的问题为止。 我们可以编写t-SQL代码来了解数据库中的标识值。 它要求您擅长编写t-SQL以及有关内部系统表的知识。

PowerShell SQL Server检查身份阈值 (PowerShell SQL Server to check IDENTITY threshold)

We can use PowerShell SQL Module DBATools for tracking identity values in the SQL Server database. You can follow my earlier article (see TOC at the bottom) to install it.

我们可以使用PowerShell SQL模块DBATools来跟踪SQL Server数据库中的标识值。 您可以按照我之前的文章(请参阅底部的目录)进行安装。

Once installed, run the following code to get DBATools functions related to identity keyword.

安装后,运行以下代码以获取与identity关键字相关的DBATools函数。

>Get-Help *identity*

It gives following function in PowerShell SQL Server Module DBATools.

它在PowerShell SQL Server模块DBATools中提供以下功能。

  1. Test-DbaIdentityUsage Test-DbaIdentity用法
  2. Get-DbaDbIdentity Get-DbaDbIdentity
  3. Set-DbaDBIdentity Set-DbaDBIdentity

Let us explore these DBATools(Test-DbaIdentityUsage and Get-DbaDbIdentity) functions in this article.

让我们探索本文中的这些DBATools(Test-DbaIdentityUsage和Get-DbaDbIdentity)函数。

Get-DbaDbIdentity (Get-DbaDbIdentity)

We can get information about current identity value in a table using Get-DbaDbIdentity function.

我们可以使用Get-DbaDbIdentity函数在表中获取有关当前身份值的信息。

First, let us get information about this PowerShell SQL Server module function Get-DbaDbIdentity.

首先,让我们获取有关此PowerShell SQL Server模块函数Get-DbaDbIdentity的信息。

>Get-Help Get-DbaDbIdentity

In the following example, we want to check IDENTITY value in DemoIdentityInsert table.

在下面的示例中,我们要检查DemoIdentityInsert表中的IDENTITY值。

>Get-DbaDbIdentity -SqlInstance Kashish\SQL2019CTP -Database SQLShackDemo -Table DemoIdentityInsert

It performs DBCC CHECKIDENT command with NORESEED option and returns the last value of identity column. By default, it gives output in the following format. You can use Format-Table –AutoSize or Out-GridView parameters to get output in a user-friendly way.

它使用NORESEED选项执行DBCC CHECKIDENT命令,并返回标识列的最后一个值。 默认情况下,它以以下格式输出。 您可以使用Format-Table –AutoSizeOut-GridView参数以用户友好的方式获取输出。

Get-DbaDbIdentity (Get-DbaDbIdentity)

We want to check IDENTITY seed usage as per the maximum value supported in a particular data type. We can use DBATools function Test-DbaIdentityUsage to retrieve useful information. It works on SQL Server 2008 and higher versions.

我们要根据特定数据类型支持的最大值来检查IDENTITY种子使用情况。 我们可以使用DBATools函数Test-DbaIdentityUsage检索有用的信息。 它适用于SQL Server 2008和更高版本。

In the following image, we can see the syntax and description of this. We can take help from the URL in the related links section as well.

在下图中,我们可以看到其语法和描述。 我们也可以从相关链接部分的URL中获取帮助。

We have the following parameters in Test-DbaIdentityUsage.

我们在Test-DbaIdentityUsage中具有以下参数

  • SqlInstance – We need to specify the instance name here. We can also specify multiple instances in this parameter SqlInstance –我们需要在此处指定实例名称。 我们还可以在此参数中指定多个实例
  • SqlCredential – By default, DBATools command connects using Windows authentication. If we want to use SQL authentication, specify SQL login credentials here SqlCredential –默认情况下,DBATools命令使用Windows身份验证进行连接。 如果要使用SQL身份验证,请在此处指定SQL登录凭据
  • Databases –We can also filter results for a particular database using this parameter. We cannot see this parameter in syntax because it is a dynamic parameter 数据库 –我们还可以使用此参数过滤特定数据库的结果。 我们无法在语法中看到此参数,因为它是动态参数
  • Threshold – We can define a percentage threshold for the IDENTITY column 阈值 –我们可以为IDENTITY列定义百分比阈值
  • Excludesystem – usually, we do not create user objects in system databases. We can ignore checking identity for the system databases 排除系统 –通常,我们不在系统数据库中创建用户对象。 我们可以忽略检查系统数据库的身份

Let us perform IDENTITY checks against all online databases in our SQL instance.

让我们对SQL实例中的所有在线数据库执行IDENTITY检查。

We might have a large number of databases and tables with IDENTITY columns. It returns output for each identity column in SQL instance. In the following screenshot, we can see a sample of it.

我们可能有大量带有IDENTITY列的数据库和表。 它为SQL实例中的每个标识列返回输出。 在下面的屏幕截图中,我们可以看到一个示例。

We can get a better representation of this information using Out-GridView format. Execute the following code, and it gives results in another window in a user-friendly way.

我们可以使用Out-GridView格式更好地表示此信息。 执行以下代码,它以用户友好的方式在另一个窗口中提供结果。

We need to look at following columns in the Test-DbaIdentityUsage output.

我们需要查看Test-DbaIdentityUsage输出中的以下列。

  • LastValue: it shows the current maximum value in the IDENTITY column LastValue:在IDENTITY列中显示当前最大值
  • Percentused: it gives percentage usage of identity value against its data type maximum supported value 百分比 :它给出了标识值相对于其数据类型最大支持值的使用百分比
  • Seedvalue: It gives an increment value for an IDENTITY column Seedvalue :它为IDENTITY列提供增量值

Let us filter results using grid view filter. Click on Add Criteria and select a database from the drop-down list.

让我们使用网格视图过滤器过滤结果。 单击添加条件,然后从下拉列表中选择一个数据库。

We can have multiple filters comparison filters to apply.

我们可以有多个过滤器比较过滤器来应用。

  • Equals 等于
  • Contains 包含
  • Does not contain 不含
  • Equals 等于
  • Does not equal 不相等
  • Ends with 以。。结束
  • Is empty 是空的
  • Is not empty 不为空

In the following screenshot, we set the filter for database SQLShackDemo.

在以下屏幕截图中,我们为数据库SQLShackDemo设置了过滤器

In earlier demonstration, we used tables DemoIdentityInsert and DemoIdentityInsert_1 to show identity issues. We can see in Test-DbaIdentityUsage output; the identity column is 100 percent used. We get all IDENTITY columns in the specified database.

在较早的演示中,我们使用表DemoIdentityInsertDemoIdentityInsert_1来显示身份问题。 我们可以在Test-DbaIdentityUsage输出中看到; 标识列已使用100%。 我们获得指定数据库中的所有IDENTITY列。

We can perform these checks regularly and be sure of any identity threshold issues.

我们可以定期执行这些检查,并确保没有任何身份阈值问题。

Let us filter out the result for a particular database using Test-DbaIdentityUsage command.

让我们使用Test-DbaIdentityUsage命令过滤出特定数据库的结果。

>Test-DbaIdentityUsage -SqlInstance Kashish\SQL2019CTP -Database sqLShackDemo | Out-GridView

带有阈值的Get-DbaDbIdentity (Get-DbaDbIdentity with threshold)

We do not want output for all identity tables in our database. Suppose we want to set a warning threshold of 20%. If the percentage used for identity columns moves beyond 20% (80% of maximum data type value as possible identity values), we should get those entries in output. We need to specify -Threshold parameter like the following command.

我们不希望数据库中所有身份表的输出。 假设我们要将警告阈值设置为20%。 如果用于标识列的百分比超过20%(最大数据类型值的80%作为可能的标识值),我们应该在输出中获得这些条目。 我们需要像以下命令一样指定-Threshold参数。

>Test-DbaIdentityUsage -SqlInstance Kashish\SQL2019CTP -Database sqLShackDemo -Threshold 20 | Out-GridView

In the following screenshot, we can see records for those tables exceeding a threshold value ( less than 80% possible identity values).

在下面的屏幕快照中,我们可以看到这些表的记录超过阈值(小于可能的标识值的80%)。

As of now, we performed a test for a single instance having multiple databases. Suppose we want to perform the test against multiple SQL instances. We need to specify multiple instance name separated by a comma.

到目前为止,我们对具有多个数据库的单个实例进行了测试。 假设我们要对多个SQL实例执行测试。 我们需要指定多个实例名称,用逗号分隔。

For example, let us Test-DbaIdentityUsage command for the following instances.

例如,让我们为以下实例使用Test-DbaIdentityUsage命令。

  1. Kashish\SQL2019 Kashish \ SQL2019
  2. Kashish\SQL2019CTP Kashish \ SQL2019CTP

In the following command, you can notice both instance names in SqlInstance parameter.

在以下命令中,您可以在SqlInstance参数中注意到两个实例名称。

>Test-DbaIdentityUsage -SqlInstance Kashish\SQL2019CTP,Kashish\SQL2019 -Threshold 20 | Out-GridView

We can see in the output identity columns for all databases in both instances exceeding a threshold value.

我们可以在两个实例的所有数据库的输出标识列中看到超过阈值的情况。

Let us rerun the command with a modified threshold of 70%. It shows tables having used identity values greater than 70% in both instances.

让我们以修改后的阈值70%重新运行该命令。 它显示了在两种情况下使用的标识值都大于70%的表。

>Test-DbaIdentityUsage -SqlInstance Kashish\SQL2019CTP,Kashish\SQL2019 -Threshold 70 | Out-GridView

结论 (Conclusion)

We can proactively identify identity max out issues in SQL instances using PowerShell SQL Server module DBATools. You should implement regular checks on this to avoid any last minute surprise. We will cover more such validations in my further articles.

我们可以使用PowerShell SQL Server模块DBATools主动识别SQL实例中的最大身份问题。 您应该对此进行定期检查,以免在最后一刻感到意外。 我们将在后续文章中介绍更多此类验证。

目录 (Table of contents)

DBATools PowerShell Module for SQL Server
PowerShell SQL Server Validation Utility – DBAChecks
SQL Database Backups using PowerShell Module – DBATools
IDENTITY columns threshold using PowerShell SQL Server DBATools
DBATools PowerShell SQL Server Database Backups commands
SQL Restore Database using DBATools
Validate backups with SQL restore database operations using DBATools
Fix Orphan users in SQL Server using DBATools PowerShell
Creating a SQL Server Database using DBATools
Get SQL Database details using DBATools
Get-DbaHelpIndex command in DBATools
适用于SQL Server的DBATools PowerShell模块
PowerShell SQL Server验证实用程序– DBAChecks
使用PowerShell模块SQL数据库备份– DBATools
使用PowerShell SQL Server DBATools的IDENTITY列阈值
DBATools PowerShell SQL Server数据库备份命令
使用DBAToolsSQL Restore Database
使用DBATools通过SQL恢复数据库操作验证备份
使用DBATools PowerShell修复SQL Server中的孤立用户
使用DBATools创建SQL Server数据库
使用DBATools获取SQL数据库详细信息
DBATools中的Get-DbaHelpIndex命令

翻译自: https://www.sqlshack.com/identity-columns-threshold-using-powershell-sql-server-dbatools/

使用PowerShell SQL Server DBATools的IDENTITY列阈值相关推荐

  1. DBATools PowerShell SQL Server数据库备份命令

    In my earlier PowerShell SQL Server article, SQL Database Backups using PowerShell Module – DBATools ...

  2. PowerShell SQL Server验证实用程序– DBAChecks

    In this article, we will explore the DBAChecks PowerShell SQL Server Module. We can use the DBACheck ...

  3. SQL Server中的标识列

    一.标识列的定义以及特点 SQL Server中的标识列又称标识符列,习惯上又叫自增列. 该种列具有以下三种特点: 1.列的数据类型为不带小数的数值类型 2.在进行插入(Insert)操作时,该列的值 ...

  4. SQL Server自动化运维系列——监控磁盘剩余空间及SQL Server错误日志(Power Shell)...

    原文:SQL Server自动化运维系列--监控磁盘剩余空间及SQL Server错误日志(Power Shell) 需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的 ...

  5. SQL Server中的计算列概述

    In this article, we will explore computed columns in SQL Server and their configurations. 在本文中,我们将探讨 ...

  6. Sql Server 添加、修改列脚本

    Sql Server 添加.修改列脚本 --添加列 ALTER TABLE StudentsADD Name NVARCHAR(50) NULL --修改列 ALTER TABLE StudentsA ...

  7. SQL SERVER重置自动编号列(标识列)

    为什么80%的码农都做不了架构师?>>>    两种方法: 一种是用Truncate TRUNCATE TABLE name 可以删除表内所有值并重置标识值 二是用DBCC CHEC ...

  8. SQL SERVER PIVOT 行转列、列传行

    在数据库操作中,有些时候我们遇到需要实现"行转列"的需求,例如一下的表为某店铺的一周收入情况表: WEEK_INCOME(WEEK VARCHAR(10),INCOME DECIM ...

  9. SQL Server 数据库调整表中列的顺序操作

    SQL Server 数据库中表一旦创建,我们不建议擅自调整列的顺序,特别是对应的应用系统已经上线,因为部分开发人员,不一定在代码中指明了列名.表是否可以调整列的顺序,其实可以自主设置,我们建议在安装 ...

最新文章

  1. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。...
  2. PHP GD库解析一张简单图片并输出
  3. HDUOJ---1754 I Hate It (线段树之单点更新查区间最大值)
  4. Android 系统(36)---Android O、N版本修改dex2oat编译选项
  5. linux下文件和目录的颜色表示
  6. 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
  7. 计算机usb接口失灵,如何解决电脑USB接口失灵的问题
  8. C# 读写Excel
  9. Winyao 8125-M2-C NGFF KEY A+E RTL8125B 工业2.5G千兆网卡
  10. 面试官:你说说软件测试WHX模型(图解)
  11. 21315陈泽华——个人征信体系的构成
  12. 雨果奖得主刘慈欣(《三体》作者)如何看待人工智能?
  13. ffffffffffffffffffffffffffffff
  14. .Net Core6.0程序发布到IIS支持apk文件下载
  15. 环保在线监测系统,网页版,提供源码
  16. 编译mimikatz
  17. [转载]校园背景音乐
  18. 系统无法停止通用卷解决方法
  19. MySQL--忘记密码,重置密码重启服务密码失效(Windows)
  20. 史蒂夫·鲍尔默- 软件行业

热门文章

  1. mysql5好还是8_定投扣款哪天好?周一还是周五,月末还是月初?(定投知识8)...
  2. 示例 - 10行代码在C#中获取页面元素布局信息
  3. ltp-ddt realtime_cpu_load涉及的cyclictest 交叉编译
  4. 51nod 1091 线段的重叠(贪心)
  5. 集成Glide4.3.1出错!AbstractMethodError: abstract method void com.bumptech.glide.module
  6. 快速划分分栏控制器上的item(自定义)
  7. WEB自定义控件小记
  8. ★LeetCode(371)——两整数之和(JavaScript)
  9. HTML+CSS制作炫彩的数字时钟
  10. 35岁以上的IT人士如果有一天被公司裁员了,该怎么办?