ITPub博客

首页 > 数据库 > Oracle > 哈希分区表

哈希分区表

原创 Oracle 作者:goasktom 时间:2014-06-02 15:53:06 0 删除 编辑

哈希分区表中的数据,oracle会尽量让其均匀分布到所有分区上,当向分区表添加新的分区时,数据会自动重新分布,现在通过添加新分区前后数据行rowid的变化来验证这一点

--创建测试表,并插入10000条记录
A>create table t_hash(id int)
  2   partition by hash(id)
  3   (
  4   partition p1
  5   )
  6   /

Table created.

A>insert into t_hash select rownum from dual connect by level<10001;

10000 rows created.

--初始情况下,分区表只有一个分区,所以分区表的记录数和分区P1的记录数相同,都是10000
A>select count(*) from t_hash;

  COUNT(*)
----------
     10000

A>select count(*) from t_hash partition (p1);

  COUNT(*)
----------
     10000

A>commit;

Commit complete.


--现在挑选一条记录,记录下其rowid
A>select * from t_hash where rownum<2;

        ID
----------
      2641

A>select rowid from t_hash where id=2641;

ROWID
------------------
AAAOBjAAEAAAACjAAA

A>@rowid
Enter value for rowid: AAAOBjAAEAAAACjAAA

      file      block        row
---------- ---------- ----------
         4        163          0


--查询视图发现id=2641这条记录,现在位于P1分区上
A>select ID,PARTITION_NAME from t_hash t,user_tab_partitions u where id=2641 and TABLE_NAME='T_HASH' and PARTITION_POSITION = 1;

        ID PARTITION_NAME
---------- ---------------
      2641 P1


--添加新分区P2
A>alter table t_hash add partition p2;

Table altered.

--通过记录数观察到数据被均匀分布到了两个分区上
A>select count(*) from t_hash partition (p1);

  COUNT(*)
----------
      4992

A>select count(*) from t_hash partition (p2);

  COUNT(*)
----------
      5008

--仍然查看id=2641这条记录的rowid,发现变了
A>select rowid from t_hash where id=2641;

ROWID
------------------
AAAOBlAAEAAAADzAAA

A>@rowid
Enter value for rowid: AAAOBlAAEAAAADzAAA

      file      block        row
---------- ---------- ----------
         4        243          0


--rowid变化的原因是,这条记录被重新放到了新分区P2上
A>select ID,PARTITION_NAME from t_hash t,user_tab_partitions u where id=2641 and TABLE_NAME='T_HASH' and PARTITION_POSITION <> 1;

        ID PARTITION_NAME
---------- ---------------
      2641 P2

 
--本来想看看删除刚添加的分区有什么效果的,但未能如愿
A>alter table t_hash drop partition p2;
alter table t_hash drop partition p2
                                  *
ERROR at line 1:
ORA-14255: table is not partitioned by Range, List, Composite Range or Composite List method

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29646619/viewspace-1174141/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-05-07

  • 博文量
    27
  • 访问量
    43325