如何轻松找到有溢出危险的 IDENTITY 列?
我的数据库越来越旧,最大的 INT IDENTITY
列之一的值约为 13 亿。这将溢出约21亿。我计划增加它的大小,但由于数据库中的记录数量,我不想太快这样做。我可能会在增加列大小之前更换数据库硬件,这可以抵消可能导致的任何性能问题。我还想关注数据库中填充率超过 50% 的所有其他列。有很多表,手动检查每个表是不切实际的。
这就是我现在获取该值的方式(我知道返回的值可能稍微过时,但它足以满足我的目的):
PRINT IDENT_CURRENT('MyDatabase.dbo.MyTable')
我可以使用 INFORMATION_SCHEMA
来获取此信息吗?
My database is getting old, and one of my biggest INT IDENTITY
columns has a value around 1.3 billion. This will overflow around 2.1 billion. I plan on increasing it's size, but I don't want to do it too soon because of the number of records in the database. I may replace my database hardware before I increase the column size, which could offset any performance problems this could cause. I also want to keep an eye on all the other columns in my databases that are more than 50% full. It's a lot of tables, and checking each one manually is not practical.
This is how I am getting the value now (I know the value returned may be slightly out of date, but it's good enough for my purposes):
PRINT IDENT_CURRENT('MyDatabase.dbo.MyTable')
Can I use the INFORMATION_SCHEMA
to get this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以查阅
sys.identity_columns
系统目录视图:这为您提供了每列的名称、种子、增量和最后一个值。该视图还包含数据类型,因此您可以轻松地找出哪些标识列可能很快就会耗尽数字......
You can consult the
sys.identity_columns
system catalog view:This gives you the name, seed, increment and last value for each column. The view also contains the data type, so you can easily figure out which identity columns might be running out of numbers soonish...
我创建了一个存储过程来解决这个问题。它使用
INFORMATION_SCHEMA
查找IDENTITY
列,然后使用IDENT_CURRENT
和列的DATA_TYPE
计算百分比满的。将数据库指定为第一个参数,然后可以选择指定最小百分比和数据类型。输出示例:
我创建了一个提醒,每月检查一次所有数据库,并将此信息记录在电子表格中。
CheckIdentityColumns 过程
I created a stored procedure to solve this problem. It uses the
INFORMATION_SCHEMA
to find theIDENTITY
columns, and then usesIDENT_CURRENT
and the column'sDATA_TYPE
to calculate the percent full. Specify the database as the first parameter, and then optionally the minimum percent and data type.Example output:
I created a reminder to check all my databases once per month, and I log this information in a spreadsheet.
CheckIdentityColumns Procedure
Keith Walton 有一个非常全面的查询,非常好。这是一个更简单的方法,它基于标识列都是整数的假设:
结果将如下所示:
检查整数标识列
Keith Walton has a very comprehensive query that is very good. Here's a little simpler one that is based on the assumption that the identity columns are all integers:
The results will look like this:
Checking Integer Identity Columns
在为这个问题制定解决方案时,我们发现这个线程既内容丰富又有趣(我们还编写了详细的< href="https://blog.garage-coding.com/2016/07/21/check-fields-might-overflow.html" rel="nofollow">关于此的帖子并描述了我们的工具如何作品)。
在我们的解决方案中,我们查询
information_schema
以获取以下列表:所有列。然后我们编写了一个程序来遍历它们中的每一个并计算最大值和最小值(我们考虑了溢出和下溢)。
While crafting a solution for this problem, we found this thread both informative and interesting (we also wrote a detailed post about this and described how our tool works).
In our solution we're querying the
information_schema
to acquire a list ofall columns. Then we wrote a program that would go through each of them and compute the maximum and minimum (we account for both overflow and underflow).