Oracle 聚集

来源:互联网 发布:知父莫若子txt百度云 编辑:程序博客网 时间:2024/05/01 21:57

http://blog.csdn.net/junmail/archive/2009/07/29/4392264.aspx

 聚集(cluster)是存储表数据的可选择的方法。一个聚集是一组表,将具有同一公共列值的行存储在一起,并且它们经常一起使用。这些公共列构成聚集码。

 

经常被同时访问的表在物理位置上可以存储在一起。为了将它们存储在一起,就要创建一个簇( c l u s t e r )来管理这些表。表中的数据一起存储在簇中,从而最小化必须执行的I / O次数,改善系统性能。

表中相关的列称为簇键(cluster key)。簇键用一个簇索引(cluster index)来进行索引;对于簇中的多个表,簇键值只存储一次。在把任何行插入簇的表中之前,都必须先创建一个簇索引。对于经常频繁一起查询的表说,使用簇比较方便。在簇中,来自不同表的行存储在同一个块中;因此同将表分开存储相比,连接这些表的查询就可能执行更少的I / O。不过,与对非。簇表的相同操作比较,簇表的插入、更新和删除性能要差很多。在聚簇表之前,要判断共同查询这些表的频率。如果这些表总是一起查询,就要考虑把它们合并成一个表而不是聚簇两个表。

Examples
1
Creating a Cluster: Example
The following statement creates a cluster named personnel with the cluster key column department, a cluster size of 512 bytes, and storage parameter values:

CREATE CLUSTER personnel
   (department NUMBER(4))
;

2
Cluster Keys: Example
The following statement creates the cluster index on the cluster key of personnel:

CREATE INDEX idx_personnel ON CLUSTER personnel;


After creating the cluster index, you can add tables to the index and perform DML operations on those tables.

3
Adding Tables to a Cluster: Example
The following statements create some departmental tables from the sample hr.employees table and add them to the personnel cluster created in the earlier example:

CREATE TABLE dept_10
   CLUSTER personnel (department_id)
   AS SELECT * FROM employees WHERE department_id = 10;

CREATE TABLE dept_20
   CLUSTER personnel (department_id)
   AS SELECT * FROM employees WHERE department_id = 20;

 

 

 

试验:创建表testatestb、并设置a01b01共用聚集testclu

     testa--a01
                     /
                       -----testclu------index_test 
                     /
     testb--b01

1、创建聚集
create cluster testclu(coll varchar(20));

 

2、创建表,并设置a01b01共用聚集testclu
create table testa(
a01 varchar(20),
a02 varchar(20)
)
cluster testclu(a01);

create table testb(
b01 varchar(20),
b02 varchar(20)
)
cluster testclu(b01);

 

3、为聚集创建索引
create index index_test on cluster testclu;

 

4、输入测试数据
insert into testa values('01','01');
insert into testa values('02','02');

insert into testb values('01','01');
insert into testb values('02','02');

 

5、通过autotrace 检查两个表是不是都采用索引index_test来检索数据

SQL> set autotrace on
SQL> select * from testa where a01='01';

A01                     A02
-------------------- --------------------
01                      01
执行计划
----------------------------------------------------------
             0
SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=4)
             1                     0
     TABLE ACCESS (CLUSTER) OF 'TESTA' (Cost=2 Card=1 Bytes=4)
             2                     1
       INDEX (UNIQUE SCAN) OF 'INDEX_TEST' (NON-UNIQUE) (Cost=1 Card=3)

SQL> select * from testb where b01='01';

B01                     B02
-------------------- --------------------
01                      01
执行计划
----------------------------------------------------------
             0
SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=4)
             1                     0
     TABLE ACCESS (CLUSTER) OF 'TESTB' (Cost=2 Card=1 Bytes=4)
             2                     1
       INDEX (UNIQUE SCAN) OF 'INDEX_TEST' (NON-UNIQUE) (Cost=1 Card=2)

从上面两个执行计划看来,采用了共同的索引index_test来检索数据。

 

6、删除试验内容 
drop table testa;
drop table testb;
drop index index_test;
drop cluster testcul;

 

原创粉丝点击