插入到使用Clustered Columnstore索引的表中

时间:2018-04-04 10:24:05

标签: sql-server tsql clustered-index

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

这是我的示例代码。

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

这样做是一种好方法吗? 是否有不同的方法在聚集的列存储索引表中插入数据,或者我是否必须删除当前索引然后再次创建索引?

1 个答案:

答案 0 :(得分:0)

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

 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