Oracle的簇和簇表

来源:互联网 发布:淘宝排名 编辑:程序博客网 时间:2024/06/08 17:59

          簇其实就是一组表,是一组共享相同数据块的多个表组成(类似于表等值连接的结果)。 将经常一起使用的表组合在一起成簇可以提高处理效率。在一个簇中的表就叫做簇表。建立顺序是:簇→簇表→数据→簇索引


 1、创建簇的 格式

    CREATE CLUSTER cluster_name
    (column date_type [,column datatype]...)
    [PCTUSED 40 | integer] [PCTFREE 10 | integer]
    [SIZE integer]
    [INITRANS 1 | integer] [MAXTRANS 255 | integer]
    [TABLESPACE tablespace]
    [STORAGE storage]  
    SIZE:指定估计平均簇键,以及与其相关的行所需的字节数。 
 2、创建簇 

    create cluster my_clu (deptno number ) 

    pctused 60

    pctfree 10

    size 1024

    tablespace users

    storage (

       initial 128 k

       next 128 k

       minextents 2

       maxextents 20

    );  

 3、创建簇表 

    create table t1_dept(

      deptno number ,

      dname varchar2 ( 20 )

    )   

    cluster my_clu(deptno);  

    create table t1_emp(

      empno number ,

      ename varchar2 ( 20 ),

      birth_date date ,

      deptno number

    )

    cluster my_clu(deptno); 

4、为簇创建索引  
   create index clu_index on cluster my_clu; 
    注:若不创建索引,则在插入数据时报错:ORA-02032: clustered tables cannot be used before the cluster index is built 
管理簇 
使用ALTER修改簇属性(必须拥有ALTER ANY CLUSTER的权限)  
 1、修改簇属性  
    可以修改的簇属性包括:
    * PCTFREE、PCTUSED、INITRANS、MAXTRANS、STORAGE
    * 为了存储簇键值所有行所需空间的平均值SIZE
    * 默认并行度  
    注:
    * 不能修改INITIAL和MINEXTENTS的值
    * PCTFREE、PCTUSED、SIZE参数修改后适用于所有数据块
    * INITRANS、MAXTRANS仅适用于以后分配的数据块
    * STORAGE参数修改后仅影响以后分配给簇的盘区 
    格式:

    alter cluster my_clu

    pctused 40  

  2、删除簇 
    drop cluster my_clu; -- 仅适用于删除空簇  
    drop cluster my_clu including tables ; -- 删除簇和簇表  

    drop cluster my_clu including tables cascade constraints ;

    -- 同时删除外键约束  

    注:簇表可以像普通表一样删除。 
散列聚簇表  
 在簇表中,Oracle使用存储在索引中的键值来定位表中的行,而在散列聚簇表中,使用了散列函数代替了簇索引,先通过内部函数或者自定义的函数进行散列计算,然后再将计算得到的码值用于定位表中的行。创建散列簇需要用到HASHKEYS子句。 
 1、创建散列簇 

    create cluster my_clu_two(empno number(10) )

    pctused 70

    pctfree 10

    tablespace users

    hash is empno

    hashkeys 150 ;  

    说明:
    * hash is 子句指明了进行散列的列,如果列是唯一的标示行,就可以将列指定为散列值
    * hashkeys 指定和限制散列函数可以产生的唯一的散列值的数量 
 2、创建散列表  

    create table t2_emp (

      empno number ( 10 ),

      ename varchar2 ( 20 ),

      birth_date date ,

      deptno number )

    cluster my_clu_two(empno);  

    注意:
    * 必须设置数值的精度(具体原因不详)
    * 散列簇不能也不用创建索引
    * 散列簇不能ALTER:size、hashkeys、hash is参数
原创粉丝点击