简体   繁体   English

选择发行SQL Server

[英]select into issue of SQL Server

I am using SQL Server 2008 and I need to select all data from one table of one DB into another table of another DB on the same SQL Server instance. 我正在使用SQL Server 2008,并且需要在同一SQL Server实例上从一个数据库的一个表中选择所有数据到另一个数据库的另一个表中。

Here is my script using. 这是我使用的脚本。 The server will run out of memory. 服务器将耗尽内存。 The data is big -- table is about 50G size on disk. 数据量很大-磁盘上的表大小约为50G。 Any easy alternative solution or any solution to lower memory consumption is fine. 任何简单的替代解决方案或任何降低内存消耗的解决方案都可以。 The server has 16G physical RAM and is x64. 该服务器具有16G物理RAM,并且为x64。

Here is the statement I am using, 这是我正在使用的语句,

insert into [TargetDB].[dbo].[Orders]
select *
from [SourceDB].[dbo].[Orders];

Any quick and simple solutions? 任何快速简单的解决方案?

thanks in advance, George 预先感谢乔治

Add some partitioning so that you don't have so take it all at once. 添加一些分区,这样您就不必立即进行全部操作。 Get data for one month at a time, or all ID:s ending with a specific number. 一次获取一个月的数据,或以特定数字结尾的所有ID :。

That way each batch gets a bit smaller. 这样,每批都变得更小。

Copy in batches 分批复制

INSERT INTO [TargetDB].[dbo].[Orders]
SELECT TOP 100 *
FROM [SourceDB].[dbo].[Orders] S
WHERE NOT EXISTS
(
 SELECT 1 FROM [TargetDB].[dbo].[Orders] T1
 WHERE T1.OrderId = S.orderID
)

That should do it in batches of 100, which you could tweak to suit the number of records you need to process. 这应该以100为批次进行,您可以调整它以适合需要处理的记录数。 This code does assume that you have some form of Unique value, such as OrderId to key off in the data copy process 此代码确实假定您具有某种形式的“唯一值”,例如要在数据复制过程中键入的OrderId

Copy in ranges 复制范围

If you have a field you can use to choose "ranges" such as an OrderDate, start off by running a query like 如果您有一个字段可以用来选择“范围”(例如OrderDate),请先运行类似以下的查询

SELECT OrderDate, COUNT(1)
FROM [SourceDB].[dbo].[Orders]

to see how many distinct values there are and how many records there are per distinct value. 看看有多少个不同的值,以及每个不同的值有多少条记录。 That should allow you to choose some ranges (eg. 2009-01-01 -> 2009-01-31) and then use ranged queries to copy the data across: 那应该允许您选择一些范围(例如2009-01-01-> 2009-01-31),然后使用范围查询在以下范围内复制数据:

INSERT INTO [TargetDB].[dbo].[Orders]
SELECT *
FROM [SourceDB].[dbo].[Orders] 
WHERE OrderDate BETWEEN '2009-01-01 00:00:00.000' AND '2009-01-31 23:59:59.997'

您可能要考虑使用BCP批量复制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM