[20171211]UNIQUE LOCAL(Partitioned)Index

[20171211]UNIQUE LOCAL (Partitioned) Index.txt

--//如何在分区表中建立local unique index呢?自己对分区表这部分内容了解很少,参考链接:
--//https://hemantoracledba.blogspot.com/2017/11/unique-local-partitioned-index.html
--//重复测试,一些内容直接转抄,不自己写了.

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

2.建立测试环境:

create table users
(region_code varchar2(3),
username varchar2(30),
account_status varchar2(32),
created date,
profile varchar2(128))
partition by range (region_code)
(partition a_m values less than ('N'),
partition n_r values less than ('S'),
partition s_z values less than (MAXVALUE));

insert into users
select substr(username,1,3), username, account_status, created, profile
from dba_users;

exec dbms_stats.gather_table_stats('','USERS');

col partition_name format a30
SCOTT@book> select partition_name, num_rows from user_tab_partitions where table_name = 'USERS' order by partition_position;
PARTITION_NAME NUM_ROWS
-------------- --------
A_M                  15
N_R                  10
S_Z                  13

3.测试1:
--//测试GLOBAL (non-partitioned) Unique Index on USERNAME.

SCOTT@book> create unique index users_username_u1 on users(username) global;
Index created.

SCOTT@book> drop index users_username_u1;
Index dropped.

SCOTT@book> create unique index users_username_u1 on users(username);
Index created.

SCOTT@book> drop index users_username_u1;
Index dropped.
--//OK!!实际上后面的global参数是对于的,缺省就是global索引.

4.测试2:

--//I now verify that I can create a Non-Unique LOCAL Index (being equi-partitioned, the index is partitioned by
--//REGION_CODE).  (Being equi-partitioned, the default behaviour is the Index Partition Names inherit from the Table
--//Partition Names).

SCOTT@book> create index users_username_l1 on users(username) local;
Index created.

select partition_name, num_rows from user_ind_partitions where index_name = 'USERS_USERNAME_L1' order by partition_position;
SCOTT@book> select partition_name, num_rows from user_ind_partitions where index_name = 'USERS_USERNAME_L1' order by partition_position;
PARTITION_NAME NUM_ROWS
-------------- --------
A_M                  15
N_R                  10
S_Z                  13

SCOTT@book> drop index users_username_l1;
Index dropped.

5.测试3:
--//I've proven (with the GLOBAL Index) that USERNAME is Unique across the whole table.  Can I create a Unique LOCAL Index
--//on this column ?

SCOTT@book> create unique index users_username_u_l1 on users(username) local;
create unique index users_username_u_l1 on users(username) local
                                           *
ERROR at line 1:
ORA-14039: partitioning columns must form a subset of key columns of a UNIQUE index

Note the Error Message. The Partition Key must be a subset of the Unique Index columns.  Let me try adding the Partition
Key  (in my example table, the Partition Key is a single column -- it could be a composite of multiple columns.  In that
case all the columns of the Partition Key must for a subset of the Unique Index).

SCOTT@book> create unique index users_rc_un_u_l1 on users(region_code, username) local;
Index created.

SCOTT@book> select partition_name, num_rows from user_ind_partitions where index_name = 'USERS_RC_UN_U_L1' order by partition_position;
PARTITION_NAME NUM_ROWS
-------------- --------
A_M                  15
N_R                  10
S_Z                  13

SCOTT@book> drop index users_rc_un_u_l1;
Index dropped.

SCOTT@book> create unique index users_un_rc_u_l1 on users(username, region_code) local;

Index created.
SCOTT@book> select partition_name, num_rows from user_ind_partitions where index_name = 'USERS_UN_RC_U_L1' order by partition_position;
PARTITION_NAME NUM_ROWS
-------------- --------
A_M                  15
N_R                  10
S_Z                  13

It doesn't matter if I put the Partition Key (REGION_CODE) before or after the target column(s) (USERNAME) when I create
the LOCAL Unique Index.  What is necessary is that the Partition Key be a subset of the Unique Index definition.

--//实际上按照以上观点,就是建立unique local index要包括Partition Key.

时间: 2024-07-29 21:39:38

[20171211]UNIQUE LOCAL(Partitioned)Index的相关文章

mysql中key 、primary key 、unique key 与index区别

问题描述 请详细说明下 mysql中key .primary key .unique key 与index区别 解决方案 参考下这个链接:http://zccst.iteye.com/blog/1697043

mysql中key 、primary key 、unique key 与index区别_Mysql

mysql中索引是非常重要的知识点,相比其他的知识点,索引更难掌握,并且mysql中的索引种类也有很多,比如primary key .unique key 与index等等,本文章向大家介绍mysql中key .primary key .unique key 与index区别.  一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_name varch

concepts阅读总结10——分区

分区概述: 1.范围分区:范围分区(range partitioning)依据用户创建分区时设定的分区键值(partition key value)范围将数据映射到不同分区.范围分区是较常用的分区方式,通常针对日期数据使用.例如,用户可以将销售数据按月存储到相应的分区中. 在采用范围分区时,应注意以下规则: 定义分区时必须使用 VALUES LESS THAN 子句定义分区的开区间上限(noninclusive upper bound).分区键大于等于此修饰符(literal)的数据将被存储到下

Partitioning in SQL Server 2008

By Muhammad Shujaat Siddiqi 原文地址:http://www.sqlservercentral.com/articles/partition/64740/ Introduction Why don't you partition your table if you have millions of rows and get complaints about the degradation of performance? This is the question I as

oracle index unique scan/index range scan和mysql range/const/ref/eq_ref的区别

关于oracle index unique scan/index range scan和mysql range/const/ref/eq_ref type的区别    关于ORACLE index unique scan和index range scan区别在于是否索引是唯一的,如果=操作谓词有唯一索引则使用unique scan否则则使用range scan 但是这种定律视乎在MYSQL中不在成立 如下执行 kkkm2 id为主键 mysql> explain extended select

MySQL创建index的例子

环境:MySQL Sever 5.1 + MySQL命令行工具 问题:创建索引 CREATE INDEX 语法: CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [index_type] index_col_name: col_name [(length)] [ASC| DESC] index_type: USING {BTREE | HASH |RTR

Oracle Index Skip Scans使用场景

INDEX跳跃扫描一般用在WHERE条件里面没有使用到引导列,但是用到了引导列以外的其他列,并且引导列的DISTINCT值较少的情况. 在这种情况下,数据库把这个复合索引逻辑上拆散为多个子索引,依次搜索子索引中非引导列的WHERE条件里面的值. 使用方法如下: /*+ INDEX_SS ( [ @ qb_name ] tablespec [ indexspec [ indexspec ]... ] ) */ The  INDEX_SS hint instructs the optimizer t

ORACLE Index Lookup索引访问路径总结

  在ORACLE中,索引访问/查找(Index Lookup)路径有五种方式,分别为INDEX UNIQUE SCAN.INDEX RANGE SCAN.INDEX FULL SCAN.INDEX FAST FULL SCAN .INDEX SKIP SCAN.下面通过一些案例介绍.总结一下这五种索引访问路径.本文是总结这方面的知识点,所以文中一些地方参考.引用了参考资料中的部分内容.详细.具体资料可以参考官方资料Index Scans         索引唯一扫描(INDEX UNIQUE

MySQL 创建索引(Create Index)的方法和语法结构及例子_Mysql

CREATE INDEX Syntax CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [index_type] index_col_name: col_name [(length)] [ASC | DESC] index_type: USING {BTREE | HASH | RTREE} 复制代码 代码如下: -- 创建无索引的表格 create t