首页 > 解决方案 > 从表插入到表的最快方法

问题描述

我通常使用以下查询进行插入。但这次我将使用这个查询来处理 1500 万条记录。这种方式很慢。我怎样才能让它快。

只会传输 Id(Guid)、Code(nvarchar) 和 pool(guid) 值。如您所见,休息是默认设置。

谢谢。

insert into collection
select Id,GETDATE(),GETDATE(),'00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000',Code,pool,1 from collectiontemp
where pool='0929B522-AF2A-4B36-xxxx-xxxxxxxxxxxx'

标签: sqloptimizationquery-optimization

解决方案


这个查询很好:

insert into collection
    select Id, GETDATE(), GETDATE(), '00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000',
           Code, pool, 1
    from collectiontemp
    where pool = '0929B522-AF2A-4B36-xxxx-xxxxxxxxxxxx';

除了在collectiontemp(pool).

然而,如果你用不同的值调用这个查询 1500 万次pool,那是很昂贵的!要加载整个表,请删除where子句:

insert into collection
    select Id, GETDATE(), GETDATE(), '00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000',
           Code, pool, 1
    from collectiontemp;

如果您有少量值,请使用以下命令在一个查询中列出它们IN

insert into collection
    select Id, GETDATE(), GETDATE(), '00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000',
           Code, pool, 1
    from collectiontemp
    where pool in ( . . . );

推荐阅读