oracle 数据库聚簇表(cluster) 之二 hash cluster

os: centos 7.4
db: oracle 11.2.0.4

oracle数据表三种基本类型:Heap Table、IOT、Cluster。名称分别为 堆表、索引组织表、聚簇表。
通常情况下,堆表是我们的首选项,也是 create table 时的默认选项。
IOT是一种融合数据到索引结构上的数据表类型(另外几篇blog 有介绍)。
聚簇表是最不常用的表类型,cluster 是一种独特的段结构,根据表的关联列,将多个表的关联数据放在一个 cluster 段上,实现快速检索。
Oracle cluster更适合相对静态数据的存储,对于OLTP应用来说,cluster在大部分情况下都不太适用,因为我们都无法预估到数据量的变化,根本无法合理设置cluster的参数。

版本

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
# 
# su - oracle
Last login: Tue Jan 21 03:40:05 CST 2020 on pts/0
$ sqlplus / as sysdba;

SQL*Plus: Release 11.2.0.4.0 Production on Mon Feb 3 10:29:09 2020

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> set lines 300;
SQL> set pages 300;
SQL> 
SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE	11.2.0.4.0	Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

SQL> 

hash cluster

目标用户需要 create cluster 权限
hash cluster 只能存放一个表,无法象 index cluster 一样可以存储多个关联表。

SQL> grant create cluster to scott2;

SQL> create cluster cluster_t0 (c0 varchar2(100)) size 1000 hashkeys 128 tablespace users;

SQL> select cluster_name, tablespace_name, cluster_type, key_size from dba_clusters where owner='SCOTT2';

CLUSTER_NAME		       TABLESPACE_NAME		      CLUST   KEY_SIZE
------------------------------ ------------------------------ ----- ----------
CLUSTER_T0		       USERS			      HASH	   1000

SQL> select segment_name,segment_type, extents, header_file, header_block, bytes, blocks from dba_segments where owner='SCOTT2';

SEGMENT_NAME									  SEGMENT_TYPE		EXTENTS HEADER_FILE HEADER_BLOCK      BYTES	BLOCKS
--------------------------------------------------------------------------------- ------------------ ---------- ----------- ------------ ---------- ----------
CLUSTER_T0									  CLUSTER		      1 	  4	    1378      196608     24

创建 cluster 的 size 1000 是用于指定 cluster 键大致大小,指定之后,就可以实现空间的预留。

hashkeys

此时已经创建了一个 cluster segment,用于存放接下来的聚簇表数据。

SQL> create table clu_t0 (
  c0 varchar2(100),
  c1 varchar2(100),
  c2 varchar2(100),
  c3 varchar2(100),
  constraint pk_clu_t0 primary key (c0)
)
cluster CLUSTER_T0(c0);
;

SQL> insert into clu_t0
select level, dbms_random.string('a',100),dbms_random.string('a',100),dbms_random.string('a',100) from dual connect by level <=10000;

查看一些数据字典

SQL> select cluster_name, tablespace_name, cluster_type, key_size from dba_clusters where owner='SCOTT2';

CLUSTER_NAME		       TABLESPACE_NAME		      CLUST   KEY_SIZE
------------------------------ ------------------------------ ----- ----------
CLUSTER_T0		       USERS			      HASH	   1000

SQL> select segment_name,segment_type, extents, header_file, header_block, bytes, blocks from dba_segments where owner='SCOTT2';

SEGMENT_NAME		       SEGMENT_TYPE	     EXTENTS HEADER_FILE HEADER_BLOCK	   BYTES     BLOCKS
------------------------------ ------------------ ---------- ----------- ------------ ---------- ----------
PK_CLU_T0		       INDEX			   5	       1       186872	  327680	 40
CLUSTER_T0		       CLUSTER			  19	       4	 1378	 4194304	512

查看下这两个表的关联情况

SQL> set autotrace traceonly;

SQL> select t0.* from clu_t0 t0 where t0.c0='998';


Execution Plan
----------------------------------------------------------
Plan hash value: 1371962339

-----------------------------------------------------------------
| Id  | Operation	  | Name   | Rows  | Bytes | Cost (%CPU)|
-----------------------------------------------------------------
|   0 | SELECT STATEMENT  |	   |	17 |  3536 |	 0   (0)|
|*  1 |  TABLE ACCESS HASH| CLU_T0 |	17 |  3536 |		|
-----------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T0"."C0"='998')


Statistics
----------------------------------------------------------
	  2  recursive calls
	  0  db block gets
	 33  consistent gets
	  0  physical reads
	  0  redo size
       1016  bytes sent via SQL*Net to client
	520  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	  1  rows processed

参考:

发布了721 篇原创文章 · 获赞 70 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/ctypyb2002/article/details/104224120
今日推荐