在分区表上创建Unique Index

在创建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.

时间: 2024-11-03 19:58:29

在分区表上创建Unique Index的相关文章

SQL Server -&gt;&gt; 分区表上创建唯一分区索引

今天在读<Oracle高级SQL编程>这本书的时候,在关于Oracle的全局索引的章节里面有一段讲到如果对一张分区表创建一条唯一索引,而索引本身也是分区的,那就必须把分区列也加入到索引列表中去,当然不一定要再第一列.后来自己就去SQL Server上也试了一下.果真和Oracle一样的道理.再看到后面章节算是理解了.因为如果表被分区了,其实就是每个分区等于一个索引树了.这也就是为什么在SQL Server下这张sys.partitions系统视图中每个partition对应了一个hobt_id

因为发现对象名称 &#39;dbo.T3&#39; 和索引名称 &#39;PK__T3__3214EC2706CC942B&#39; 有重复的键,所以 CREATE UNIQUE INDEX 语句终止。重复的键值为 (1)。

sql语句:ALTER TABLE T3 ADD  PRIMARY KEY (ID); 执行报错: 消息 1505,级别 16,状态 1,第 1 行因为发现对象名称 'dbo.T3' 和索引名称 'PK__T3__3214EC270466E04C' 有重复的键,所以 CREATE UNIQUE INDEX 语句终止.重复的键值为 (1).消息 1750,级别 16,状态 0,第 1 行无法创建约束.请参阅前面的错误消息.语句已终止. 出错原因: 要在ID列上建主键,但id列数据有重复,不唯一.

SqlServer在视图上创建索引的条件

在视图上创建索引需要三个条件: 一.视图必须绑定到架构. 要做到这点,在 CREATE VIEW 语句中,必须加上 WITH SCHEMABINDING,如果是使用企业管理器,则在设计界面的空白处点击右键,属性,选中“绑定到架构”. 二.索引必须是唯一索引.  www.2cto.com 要做到这点,在 CREATE INDEX 中必须指定 UNIQUE. 三.索引必须是聚集索引. 要做到这点,在 CREATE INDEX 中必须指定 CLUSTERED. 例: CREATE VIEW viewF

在LINUX上创建GIT服务器【转】

转自:http://blog.csdn.net/xiongmc/article/details/9176785 如果使用git的人数较少,可以使用下面的步骤快速部署一个git服务器环境. 1. Client生成 SSH 公钥,以便Server端识别. 每个需要使用git服务器的工程师,自己需要生成一个ssh公钥 进入自己的~/.ssh目录,看有没有用 文件名 和 文件名.pub 来命名的一对文件,这个 文件名 通常是 id_dsa 或者 id_rsa. .pub 文件是公钥,另一个文件是密钥.假

对于大量left join 的表查询,可以在关键的 连接节点字段上创建索引。

问题: 大量的left join 怎么优化 select a.id,a.num,b.num,b.pcs,c.num, c.pcs,d.num,d.pcs,e.num,e.pcs,a.x, a.y from a left join b.id=a.id and b.time=a.time left join c.id=a.id and b.time=a.time left join d.id=a.id and b.time=a.time left join e.id=a.id and b.time=

Windows azure上创建及绑定自己域名的website

Windows azure已经是一个大家耳熟能详的服务了,IT界目前最多.最受关注的话题是所谓的"云",什么是云,其实真心没有一个合理的答案去解释,对于一个使用者来说,只要功能能满足,那最后的评价肯定是ok的,windows azure的收费确实很高,几乎所有的服务都是收费的.其实说到云服务,也许大家都会说亚马逊.阿里云-但说句实话,亚马逊在云服务器的方面确实做的相当不错,很受关注,但我也基本算是微软的粉丝了吧~,从一开始就基础微软的所有产品,所以还是比较终于微软的产品,加上我们部门的

在Linux上创建webrev(cont)[基于svn]

在前文中,基于git介绍了webrev工具.实际上,webrev工具还支持hg和svn.最近的工作中不可避免地要使用svn,故在此总结一下如何基于svn在Linux上创建webrev.顺便吐个槽,没有网页版的代码比对,用svn diff简直就是刀耕火种茹毛饮血啊!技术再娴熟的老司机,也架不住让你在高速公路上开拖来机Orz! 以前工作上一直用版本管理工具Mercurial (命令为hg), 个人学习的话用Git, 但从来没用过Subversion (命令为svn等) .所以,下面的先简单介绍一下如

在web上创建可视化的交互式数据javascript框架-JavaScript InfoVis Toolkit

原文:在web上创建可视化的交互式数据javascript框架-JavaScript InfoVis Toolkit 源代码下载地址:http://www.zuidaima.com/share/1554696025099264.htm 官方站点:http://philogb.github.io/jit/index.html 适应各种结构表现(机关.人际关系.文件路径...) 适应客户需求,找的表现机关级别关系图. 如: 这一款JS绘图框架,非常小,压缩后 只有150kb.目前只用到它的这一种表现

Oracle 12C -- 在相同的列的集合上创建多个索引

在12C中,可以在相同的列的集合上创建多个索引,但是多个索引的类型要不同.同一时刻,只有一个是可见的. SQL> create table emp_tab as select * from emp; SQL> create index emp_tab_ix_01 on emp_tab(job,hiredate); --在创建另一个索引之前,要将上一步创建的索引变成invisible状态 SQL> alter index emp_tab_ix_01 invisible; SQL> c