1、表的功能:存储、管理数据的基本单元(二维表:有行和列组成)
2、表的类型:
1)堆表:heap table :数据存储时,行是无序的,对它的访问采用全表扫描。
2)分区表 表>2G
3)索引组织表(IOT)
4)簇表
5)临时表
6)压缩表
7)嵌套表
3、如何将普通表转换为分区表;
11g以前,1)create 分区表, 2)insert into 分区表 select * from 普通表; 3)rename 分区表名; 4)重建约束、索引、触发器。
11g以后,在线重定义分区表
12.1 分区表及其种类(10g)
1)Range Partitioning (范围分区)
scott:
SQL>create table sale(
product_id varchar2(5), sales_count number(10,2)
)
partition by range(sales_count)
(
partition p1 values less than(1000),
partition p2 values less than(2000),
partition p3 values less than(3000)
);
查看信息:
select * from user_tab_partitions where table_name='SALE';
insert into sale values('1',600);
insert into sale values('2',1000);
insert into sale values('3',2300);
insert into sale values('4',6000);
commit;
select * from sale partition(p1);
select * from sale partition(p2);
增加一个分区
alter table sale add partition p4 values less than(maxvalue);
再看一下, 可以插入6000值了
select * from user_tab_partitions where table_name='SALE';
insert into sale values('4',6000);
看一下段的分配
SQL> select segment_name,segment_type,partition_name from user_segments;
12.1.1 默认情况下,如果对分区表的分区字段做超范围(跨段)update操作,会报错——ORA-14402: 。如果一定要改,可以通过打开表的row movement属性来完成。
SQL> select rowid,t1.* from sale partition(p1) t1;
ROWID PRODU SALES_COUNT
------------------ ----- -----------
AAASvUAAEAAAAGVAAA 1 600
SQL> update sale set sales_count=1200 where sales_count=600;
update sale set sales_count=1200 where sales_count=600
*
第 1 行出现错误:
ORA-14402: 更新分区关键字列将导致分区的更改
SQL> alter table sale enable row movement;
SQL> update sale set sales_count=1200 where sales_count=600;
已更新 1 行。
SQL> select rowid,t1.* from sale partition(p2) t1;
ROWID PRODU SALES_COUNT
------------------ ----- -----------
AAASvVAAEAAAAGdAAA 2 1000
AAASvVAAEAAAAGdAAB 1 1200
一般来说范围分区的分区字段使用数字类型或日期类型,使用字符类型的语法是可以的,实际工作中使用较少。这或许跟values less than
子句有关。
12.1.2 关于建立分区索引
一般使用分区都会建立索引,分区索引有local与global之分。
Local Parfixed Index
|-----------------------------
Local Partitioned Index |
|-----------------------------|
Partitioned Index | |Local Nonparfixed Index
|----------------------------------| |------------------------------
| |
| |Global Partitioned Index
| |------------------------------
|
|Nonpartitioned Index
|------------------------
1)local:一个索引分区对应一个表分区,分区key就是索引key,分区边界就是索引边界。更新一个表分区时仅仅影响该分区的索引。
SQL>create index sale_idx on sale(sales_count) local;
SQL>select * from user_ind_partitions;
Local Parfixed Index,所谓前缀索引,是指组合索引中的first column使用的是分区key。
global:全局索引:
2)分区全局索引:索引分区不与表分区对应,分区key是索引key。另外一定要将maxvalue关键字做上限。
create index sale_global_idx on sale(sales_count) global
partition by range (sales_count)
(
partition p1 values less than(1500),
partition p2 values less than(maxvalue)
);
SQL>select * from user_ind_partitions;
12.1.3 删除一个分区,其中的数据全部清除,并且包括相关索引等
SQL> alter table sale drop partition p3;
12.1.4 Hash Partitioning (散列分区,也叫hash分区)
实现均匀的负载值分配,增加HASH分区可以重新分布数据。
create table my_emp(
empno number, ename varchar2(10)
)
partition by hash(empno)
(
partition p1, partition p2
);
select * from user_tab_partitions where table_name='MY_EMP';
插入几个值,看是否均匀插入。
insert into my_emp values(1,'A');
insert into my_emp values(2,'B');
insert into my_emp values(3,'C');
select * from my_emp partition(P1);
select * from my_emp partition(P2);
12.1.5 列表分区(list): 将不相关的数据组织在一起
create table personcity(
id number, name varchar2(10), city varchar2(10)
)
partition