码迷,mamicode.com
首页 > 其他好文 > 详细

在分区表上创建Unique Index

时间:2016-06-24 12:23:54      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

在创建FullText Index之前,指定的Table上必须存在一个只包含Single column的 unique index。如果在分区表上创建的Unique Index 不包括Partition Column,SQL Server 抛出Error Message,脚本如下

CREATE UNIQUE NONCLUSTERED INDEX UQ_IDX_IndexName
ON Schema_Name.table_name
(
[col1]
)
with(data_compression=row);

Error Description:Column ‘CreatedDateKey‘ is partitioning column of the index ‘UQ_IDX_IndexName‘. Partition columns for a unique index must be a subset of the index key.

 

如果要在Partitioned Table上创建 Aligned Unique index(对齐的唯一索引),那么Partition column必须是Index Key。

CREATE UNIQUE NONCLUSTERED INDEX UQ_IDX_IndexName
ON Schema_Name.table_name
(
[col1],
[CreatedDateKey]
)
with(data_compression=row);

可以使用On 子句指定索引存储的FileGroup,这样,SQL Server在Partitioned Table上创建的Unique Index 是 No-Aligned。

CREATE UNIQUE NONCLUSTERED INDEX UQ_IDX_IndexName
ON Schema_Name.table_name
(
[col1],
)
with(data_compression=row)
on [Primary];


Appendix: Create Index Syntax

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name 
    ON <object> ( column [ ASC | DESC ] [ ,...n ] ) 
    [ INCLUDE ( column_name [ ,...n ] ) ]
    [ WHERE <filter_predicate> ]
    [ WITH ( <relational_index_option> [ ,...n ] ) ]
    [ ON { partition_scheme_name ( column_name ) 
         | filegroup_name 
         | default 
         }
    ]

ON filegroup_name

Creates the specified index on the specified filegroup. If no location is specified and the table or view is not partitioned, the index uses the same filegroup as the underlying table or view. The filegroup must already exist.

 

Partitioned Indexes

Partitioned indexes are created and maintained in a similar manner to partitioned tables, but like ordinary indexes, they are handled as separate database objects. You can have a partitioned index on a table that is not partitioned, and you can have a nonpartitioned index on a table that is partitioned.

If you are creating an index on a partitioned table, and do not specify a filegroup on which to place the index, the index is partitioned in the same manner as the underlying table. This is because indexes, by default, are placed on the same filegroups as their underlying tables, and for a partitioned table in the same partition scheme that uses the same partitioning columns. When the index uses the same partition scheme and partitioning column as the table, the index is aligned with the table.

When partitioning a non-unique, clustered index, the Database Engine by default adds any partitioning columns to the list of clustered index keys, if not already specified.

 

Aligned index              

An index that is built on the same partition scheme as its corresponding table. When a table and its indexes are in alignment, SQL Server can switch partitions quickly and efficiently while maintaining the partition structure of both the table and its indexes. An index does not have to participate in the same named partition function to be aligned with its base table. However, the partition function of the index and the base table must be essentially the same, in that 1) the arguments of the partition functions have the same data type, 2) they define the same number of partitions, and 3) they define the same boundary values for partitions.

Nonaligned index              

An index partitioned independently from its corresponding table. That is, the index has a different partition scheme or is placed on a separate filegroup from the base table. Designing an nonaligned partitioned index can be useful in the following cases:

  • The base table has not been partitioned.

  • The index key is unique and it does not contain the partitioning column of the table.

  • You want the base table to participate in collocated joins with more tables using different join columns.

参考doc:

CREATE INDEX (Transact-SQL)

Partitioned Tables and Indexes

Full-text indexes in partitioned tables

 

If the unique index does not contain the partitioning column then SQL Server would have to check for duplicate in all partitions for each insert or update of the row (e.g. you partition on columns "a" and you have unique index on "b", then (a,b) rows (1,2) and (3,2) could be in different partitions). This would lead to very inefficient maintenance of uniquness and that is the primary reason why SQL Server 2005 enforces the partitioning key to be contained in the unique key (because then the "duplicates" will always be in the same partition).

You can create unique partitioned index by partitioning it on the key, or create non-partitioned unique index regardless if and how the table is partitioned. This is what you see by creating the index in a "separate" filegroup - such index is not partitioned. If you don‘t specify filegroup, the index will be (by default) partitioned exactly as the table.

 

在分区表上创建Unique Index

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5613750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!