【问题标题】:fastest way to insert from table to table从表插入到表的最快方法
【发布时间】:2019-11-09 20:05:13
【问题描述】:

我通常使用以下查询进行插入。 但这次我将使用这个查询来处理 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'

【问题讨论】:

  • 。 .这是插入 1500 万条记录的查询吗?还是您多次调用查询?
  • 这是一个查询。有 15 百万行 "select * from collection where pool='0929B522-AF2A-4B36-xxxx-xxxxxxxxxxxx'"

标签: sql optimization query-optimization


【解决方案1】:

这个查询没问题:

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 ( . . . );

【讨论】:

  • 我可以让它在循环内吗?它会让它更快吗?
  • 在临时表内还有其他池,所以我想我无法删除 where 子句。我只希望转移那个特定的池。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2012-01-31
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多