CREATE COLUMNSTORE INDEX (Transact-SQL)
Creates a columnstore index on a specified table. An xVelocity memory optimized columnstore index, is a type of compressed non-clustered index. There is a limit of one columnstore index per table. An index can be created before there is data in the table. A table with a columnstore index cannot be updated. For information about using columnstore indexes, see Columnstore Indexes.
Note
|
|---|
|
For information about how to create a relational index, see CREATE INDEX (Transact-SQL). For information about how to create an XML index, see CREATE XML INDEX (Transact-SQL). For information about how to create a spatial index, see CREATE SPATIAL INDEX (Transact-SQL). |
CREATE [ NONCLUSTERED ] COLUMNSTORE INDEX index_name
ON <object> ( column [ ,...n ] )
[ WITH ( <column_index_option> [ ,...n ] ) ]
[ ON {
{ partition_scheme_name ( column_name ) }
| filegroup_name
| "default"
}
]
[ ; ]
<object> ::=
{
[database_name. [schema_name ] . | schema_name . ]
table_name
{
<column_index_option> ::=
{
DROP_EXISTING = { ON | OFF }
| MAXDOP = max_degree_of_parallelism
}
Indexes can be created on a temporary table. When the table is dropped or the session ends, the indexes are dropped.
The common business data types can be included in a columnstore index. The following data types can be included in a columnstore index.
-
char and varchar
-
nchar and nvarchar (except varchar(max) and nvarchar(max))
-
decimal (and numeric) (Except with precision greater than 18 digits.)
-
int , bigint, smallint, and tinyint
-
float (and real)
-
bit
-
money and smallmoney
-
All date and time data types (except datetimeoffset with scale greater than 2)
The following data types cannot be included in a columnstore index.
-
binary and varbinary
-
ntext , text, and image
-
varchar(max) and nvarchar(max)
-
uniqueidentifier
-
rowversion (and timestamp)
-
sql_variant
-
decimal (and numeric) with precision greater than 18 digits
-
datetimeoffset with scale greater than 2
-
CLR types (hierarchyid and spatial types)
-
xml
Basic Restrictions
A columnstore index:
-
Cannot have more than 1024 columns.
-
Cannot be clustered. Only nonclustered columnstore indexes are available.
-
Cannot be a unique index.
-
Cannot be created on a view or indexed view.
-
Cannot include a sparse column.
-
Cannot act as a primary key or a foreign key.
-
Cannot be changed using the ALTER INDEX statement. Drop and re-create the columnstore index instead. (You can use ALTER INDEX to disable and rebuild a columnstore index.)
-
Cannot be created by with the INCLUDE keyword.
-
Cannot include the ASC or DESC keywords for sorting the index. Columnstore indexes are ordered according to the compression algorithms. Sorting would eliminate many of the performance benefits.
Columnstore indexes cannot be combined with the following features:
-
Page and row compression, and vardecimal storage format (A columnstore index is already compressed in a different format.)
-
Replication
-
Change tracking
-
Change data capture
-
Filestream
For information about the performance benefits and limitations of columnstore indexes, see Columnstore Indexes.
A. Creating a simple nonclustered index
The following example creates a simple table and clustered index, and then demonstrates the syntax of creating a columnstore index.
CREATE TABLE SimpleTable (ProductKey [int] NOT NULL, OrderDateKey [int] NOT NULL, DueDateKey [int] NOT NULL, ShipDateKey [int] NOT NULL); GO CREATE CLUSTERED INDEX cl_simple ON SimpleTable (ProductKey); GO CREATE NONCLUSTERED COLUMNSTORE INDEX csindx_simple ON SimpleTable (OrderDateKey, DueDateKey, ShipDateKey); GO
B. Creating a simple nonclustered index using all options
The following example creates a simple table and clustered index, and then demonstrates the syntax of creating a columnstore index.
CREATE NONCLUSTERED COLUMNSTORE INDEX csindx_simple ON SimpleTable (OrderDateKey, DueDateKey, ShipDateKey) WITH (DROP_EXISTING = ON, MAXDOP = 2) ON "default" GO
For a more complex example using partitioned tables, see Columnstore Indexes.
Note