首页 > 解决方案 > Create Index using Liquibase (INCLUDE COLUMN)

问题描述

I want to create an INDEX on my MSSQL DB and the index should looks like :

CREATE NONCLUSTERED INDEX [table] ON [status] ([date]) INCLUDE ([id], [status]) WITH (ONLINE = ON)

I found that with liquibase "sql" tag option we can execute the SQL script and i applied the same in my liquibase script:

<changeSet  author="author"  id="15">  
    <sql  dbms="mssql"  schemaName="${SCHEMA_NAME}"
            endDelimiter="\nGO"  
            splitStatements="true"  
            stripComments="true">CREATE NONCLUSTERED INDEX [table] ON [status] ([date]) INCLUDE ([id], [status]) WITH (ONLINE = ON)
     </sql>  
</changeSet> 

But, the problem is the above changeset working only with the enterprise DB.

Reason: liquibase.exception.DatabaseException: Online index operations can only be performed in Enterprise edition of SQL Server.

Anybody, know how i can achieve the same task use liquibase changeset CREATEINDEX option.

标签: sqlsql-serverliquibase

解决方案


正如错误消息所述,

联机索引操作只能在 SQL Server 企业版中执行。

为了使它起作用,您需要做的就是ONLINE从您的语句中删除该子句create index

CREATE NONCLUSTERED INDEX [table] ON [status] ([date]) INCLUDE ([id], [status]);

这适用于任何版本。同时,如果您希望能够在版本允许时使用在线索引创建和重建,您可以检查您的应用程序连接到的数据库引擎的版本,然后可选地添加支持的子句,例如online在这种情况下.

在您的示例中,在线索引重建仅在 Enterprise edition中可用,但是 Development 和 Evaluation 版本在功能上与其相同,因此在线索引操作也可以在它们中使用。

以下是确定 SQL Server 实例功能级别的方法:

select serverproperty('EngineEdition');

这为您提供了实例的实际功能,而与许可无关。通过将此查询的结果与它可以返回的记录值进行比较,您可以决定它支持哪些选项,并在执行查询之前将合适的部分包含在您的查询中。


推荐阅读