【问题标题】:Insert into table that uses Clustered Columnstore index插入到使用聚集列存储索引的表中
【发布时间】:2018-09-13 21:00:57
【问题描述】:

我想插入到使用聚集列存储索引的表中。 我的逻辑如下。首先,我检查表是否具有聚集列存储索引,然后删除索引,插入新数据,最后再次创建聚集列存储索引。

这是我的示例代码。

declare @sql as nvarchar(max)
if exists (select i.name as indexname, 
       t.name as tablename
from   sys.indexes i
  join sys.tables t on i.object_id = t.object_id
where  i.type in (5, 6) and t.name = 'cci_table')
begin
        set @sql = '
                    DROP CLUSTERED COLUMNSTORE INDEX cci ON dbo.cci_table'
        print @sql

        /** insert data to cci_table **/

        set @sql = '
                    CREATE CLUSTERED COLUMNSTORE INDEX cci ON dbo.cci_table'  
        print @sql
end
else
begin
        set @sql = '
                    CREATE CLUSTERED COLUMNSTORE INDEX cci ON dbo.cci_table'  
        print @sql
end

这样做是个好方法吗? 是否有其他方法可以在聚集列存储索引表中插入数据,还是我必须删除当前索引然后再次创建索引?

【问题讨论】:

  • 你为什么要这样做?只有在 SQL Server 2012 中,(非聚集)列存储索引才会以只读方式呈现表,但 SQL Server 2012 不支持 CCI(如果我没记错的话)。换句话说,为什么不简单地进行更新并让引擎来处理呢?
  • @Jeroen Monsert 那么放弃然后创建 CCI 是没有意义的。在那种更新的情况下,查询会是什么样子?只是将数据插入到 dbo.cci_table 中?
  • 嗯,是的。尽管列存储中的插入和更新效率远低于行存储,但引擎可以使用所谓的增量存储来处理它们。如果您可以批量处理行,您应该——参见data loading guidance——但单独的行也可以。完整的细分是 - 2012 年有只读的非集群列存储,2014 年有可更新的集群列存储(但只读非集群列存储),2016 年使非集群列存储也可更新。
  • 无论版本 2012-2016,您还可以使用分区切换,这在性能方面也很可能会好很多。不过,这确实需要做更多的工作。

标签: sql-server tsql clustered-index


【解决方案1】:

不需要删除然后创建列存储索引,因为使用列存储索引的表是可更新的。 这意味着如果索引存在没有问题,我可以将新数据插入到现有的列存储表中。

 declare @sql as nvarchar(max)
    if exists (select i.name as indexname, 
           t.name as tablename
    from   sys.indexes i
      join sys.tables t on i.object_id = t.object_id
    where  i.type in (5, 6) and t.name = 'cci_table')
    begin

            /** insert data to cci_table **/

    end
    else
    begin
            set @sql = '
                        CREATE CLUSTERED COLUMNSTORE INDEX cci ON dbo.cci_table'  
            print @sql

            /** insert data to cci_table **/

    end

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 2017-12-02
    • 2020-09-15
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多