如何将大量 Varbinary(max) 行插入 Table
How to Insert Large Amount of Varbinary(max) Rows Into Table
我使用本地 SQL 服务器。我有 table 和 250k 张照片 (DropPhotos
)。照片存储在名为 Content
的 Varbinary(max)
列中。我想将所有这些行移动到同一数据库 (BasePhotos
) 中的另一个 table 以及一些额外的列。我这样写 SQL 语句:
INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
FROM dbo.DropPhotos
当我开始执行时一切正常。几分钟(5 - 15 分钟)后,我的屏幕开始随机打开和关闭,然后关闭,又过了几分钟,我的电脑重新启动。我怀疑移动如此多的二进制数据会耗尽所有 RAM 并导致关机。我在网上搜索了解决方案,发现存在某种批量插入。也许它可以帮助我?但据我所知,它用于从文件插入行,而不是从一个 table 从另一个插入行。
所以我的问题很简单:如何将大量重行插入 table。
PhotoId 是唯一标识符
更新:请注意,OP在问题中要求移动记录,后来在评论中更改为复制记录。
上面讨论的批处理方法可以这样实现:
set nocount on
DECLARE @IdTable TABLE (id uniqueidentifier)
insert into @IdTable(id)
select top 100
dropPhotos.PhotoId
from
dbo.DropPhotos dropPhotos
left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
where
basePhotos.PhotoId is null
while exists(select top 1 1 from @IdTable)
begin
INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
FROM dbo.DropPhotos
WHERE PhotoId IN (select id from @IdTable)
delete from @IdTable
insert into @IdTable(id)
select top 100
dropPhotos.PhotoId
from
dbo.DropPhotos dropPhotos
left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
where
basePhotos.PhotoId is null
end
我使用本地 SQL 服务器。我有 table 和 250k 张照片 (DropPhotos
)。照片存储在名为 Content
的 Varbinary(max)
列中。我想将所有这些行移动到同一数据库 (BasePhotos
) 中的另一个 table 以及一些额外的列。我这样写 SQL 语句:
INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
FROM dbo.DropPhotos
当我开始执行时一切正常。几分钟(5 - 15 分钟)后,我的屏幕开始随机打开和关闭,然后关闭,又过了几分钟,我的电脑重新启动。我怀疑移动如此多的二进制数据会耗尽所有 RAM 并导致关机。我在网上搜索了解决方案,发现存在某种批量插入。也许它可以帮助我?但据我所知,它用于从文件插入行,而不是从一个 table 从另一个插入行。
所以我的问题很简单:如何将大量重行插入 table。
PhotoId 是唯一标识符
更新:请注意,OP在问题中要求移动记录,后来在评论中更改为复制记录。
上面讨论的批处理方法可以这样实现:
set nocount on
DECLARE @IdTable TABLE (id uniqueidentifier)
insert into @IdTable(id)
select top 100
dropPhotos.PhotoId
from
dbo.DropPhotos dropPhotos
left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
where
basePhotos.PhotoId is null
while exists(select top 1 1 from @IdTable)
begin
INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
FROM dbo.DropPhotos
WHERE PhotoId IN (select id from @IdTable)
delete from @IdTable
insert into @IdTable(id)
select top 100
dropPhotos.PhotoId
from
dbo.DropPhotos dropPhotos
left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
where
basePhotos.PhotoId is null
end