向具有超过 1500 万条记录的 SQL 列添加自动编号

发布于 2024-09-25 06:52:55 字数 79 浏览 3 评论 0原文

我需要向 SQL 2005 中包含大约 1500 万条记录的现有表添加一个自动编号列。

您认为这需要多长时间?更好的方法是什么?

I need to add a autonumber column to an existing table which has about 15 million records in SQL 2005.

Do you think how much time it'll take? What's the better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吹梦到西洲 2024-10-02 06:52:56

确实很难说要花多长时间。

在我看来,最好的选择是带回生产数据库的副本,在开发环境中恢复它,并在那里应用您的更改,看看需要多长时间。

从那里,您可以协调站点停机时间,或安排更新在用户未连接时运行。

It's really difficult to say how long it will take.

In my opinion, your best bet would be to bring back a copy of the production database, restore it in a development environment, and apply your changes there to see how long it takes.

From there, you can coordinate site downtime, or schedule the update to run when users aren't connected.

情绪 2024-10-02 06:52:56

除非是紧急情况:

  1. 不要对实时数据库进行更改。
  2. 不要对实时数据库进行更改。

要了解需要多少停机时间,请还原到新数据库并在其中进行更改。

它不应该很长:它不仅取决于有多少行,更取决于每行中有多少数据。 (SQL Server 将复制整个表。)

Unless it's an emergency:

  1. Don't make changes to a live database.
  2. Don't make changes to a live database.

To find out how much downtime you'll need, do a restore to a new DB and make the change there.

It shouldn't be very long: it depends not only on how many rows, but even more on how much data there is in each row. (SQL Server is going to copy the entire table over.)

总以为 2024-10-02 06:52:56

您是否可以选择备份生产数据库、在另一台服务器上应用更改并更改连接字符串?您甚至可以将其恢复到与原始服务器相同的服务器上,更改连接字符串并删除旧数据库。

如果磁盘空间有限,则可能不可行。

Do you have the option of backing up the production database, applying the changes on another server and changing connection strings? You could even restore it on the same server as the original, change connection strings and get rid of the old database.

May not be feasible if disk space is limited.

内心荒芜 2024-10-02 06:52:55

为了最大限度地减少影响,我将创建一个添加了标识列的新表,通过从旧表中进行选择来插入到新表中,然后删除旧表并重命名新表。下面我将给出一个基本的轮廓。可能需要额外的步骤来处理外键等。

create table NewTable (
   NewID int identity(1,1),
   Column1 ...
)
go

insert into NewTable
   (Column1, ...)
   select Column1, ...
       from OldTable
go

drop table OldTable
go

exec sp_rename 'NewTable', 'OldTable'
go

To minimize impact, I would create a new table with the identity column added, insert into the new table by selecting from the old table, then drop the old table and rename the new. I'll give a basic outline below. Extra steps may be needed to handle foreign keys, etc.

create table NewTable (
   NewID int identity(1,1),
   Column1 ...
)
go

insert into NewTable
   (Column1, ...)
   select Column1, ...
       from OldTable
go

drop table OldTable
go

exec sp_rename 'NewTable', 'OldTable'
go
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文