嗨,我是分区概念的新手
在azure SQL中创建具有列存储索引的表时,我得到的错误就像
消息102,级别15,状态1,行15'CLUSTERED'附近的语法不正确。
请找到我在azure SQL中运行的以下脚本
CREATE TABLE [dbo].[FactInternetSales]
(
[ProductKey] int NOT NULL
,[OrderDateKey] int NOT NULL
, [CustomerKey] int NOT NULL
, [PromotionKey] int NOT NULL
, [SalesOrderNumber] nvarchar(20) NOT NULL
, [OrderQuantity] smallint NOT NULL
, [UnitPrice] money NOT NULL
, [SalesAmount] money NOT NULL
)
WITH
( CLUSTERED COLUMNSTORE INDEX
, DISTRIBUTION = HASH([ProductKey])
, PARTITION ( [OrderDateKey] RANGE RIGHT FOR VALUES
(20000101,20010101,20020101
,20030101,20040101,20050101
)
)
)
;
您的问题中的DDL是SQL数据仓库/ SQL并行数据仓库的语法,而不是Azure SQL数据库(或Azure VM或本地上的SQL Server)的语法。对于后者,不能使用内联语法加上DISTIBUTION
子句在这些情况下不适用。
相反,首先创建分区函数和方案,并使用分区方案指定ON
子句。索引可以使用内联语法(如下例)指定,也可以使用CREATE CLUSTERED COLUMNSTORE INDEX...
单独指定。
CREATE PARTITION FUNCTION PF_FactInternetSales_OrderDateKey(int)
AS RANGE RIGHT FOR VALUES(
20000101
, 20010101
, 20020101
, 20030101
, 20040101
, 20050101
);
CREATE PARTITION SCHEME PS_FactInternetSales_OrderDateKey
AS PARTITION PF_FactInternetSales_OrderDateKey
ALL TO ([PRIMARY]);
CREATE TABLE [dbo].[FactInternetSales]
(
[ProductKey] int NOT NULL
, [OrderDateKey] int NOT NULL
, [CustomerKey] int NOT NULL
, [PromotionKey] int NOT NULL
, [SalesOrderNumber] nvarchar(20) NOT NULL
, [OrderQuantity] smallint NOT NULL
, [UnitPrice] money NOT NULL
, [SalesAmount] money NOT NULL
, INDEX ccix CLUSTERED COLUMNSTORE ON PS_FactInternetSales_OrderDateKey(OrderDateKey)
) ON PS_FactInternetSales_OrderDateKey(OrderDateKey);
请注意,您需要至少使用Standard Edition和S3 Service Objective才能在Azure SQL数据库中使用columnstore。下面的查询将返回您当前的配置。
SELECT
DATABASEPROPERTYEX(N'YourDatabase', 'Edition') AS Edition
, DATABASEPROPERTYEX(N'YourDatabase', 'ServiceObjective') AS ServiceObjective;
如有必要,可以使用门户网站或使用下面的DDL在T-SQL中移动到更高层。这可能需要几分钟时间,您可以运行上述查询以验证它是否已完成。
ALTER DATABASE YourDatabase
MODIFY (SERVICE_OBJECTIVE = 'S3');
我看到你也用SQL Server 2012标记了你的问题。在SQL Server 2014企业版中引入了集群列存储索引,从SQL Server 2016 SP2开始,也可以在其他SQL Server版本中使用。在SQL Server 2012中,仅支持只读非群集列存储索引。
以下内容适用于标准S3和更高版本。我无法设置分发和分区。
CREATE TABLE [dbo].[FactInternetSales]
(
[ProductKey] int NOT NULL
,[OrderDateKey] int NOT NULL
, [CustomerKey] int NOT NULL
, [PromotionKey] int NOT NULL
, [SalesOrderNumber] nvarchar(20) NOT NULL
, [OrderQuantity] smallint NOT NULL
, [UnitPrice] money NOT NULL
, [SalesAmount] money NOT NULL
)
GO
CREATE CLUSTERED COLUMNSTORE index ProductKey on FactInternetSales
要解决此问题,您需要选择Datawarehouse而不是特定数据库。如果您使用的是Azure Data Studio,则“更改连接”旁边会显示一个下拉按钮。确保您选择的是特定的数据仓库。
注意:CLUSTERED COLUMNSTORE INDEX是一个表结构选项,将表存储为聚簇列存储索引,其中所有数据都按列压缩和存储。聚簇列存储索引是SQL数据仓库的默认值,适用于所有表数据。