oracle每一行的hash值,Hash分区表分区数与数据分布的测试
較早就知道Hash分區(qū)建議分區(qū)數(shù)是2的冪, 只是當(dāng)作經(jīng)驗(yàn)值記錄,一直都沒有測(cè)試過, 今天做了個(gè)簡(jiǎn)單測(cè)試, 供大家參考。 親手做過的實(shí)驗(yàn),
記憶更加深刻一些 。
Oracle 10.2.0.4
1.??建立分區(qū)數(shù)為5的hash分區(qū)表test01:
create table test01
partition by hash(object_id)
(partition p1,
partition p2,
partition p3,
partition p4,
Partition p5)
as select * from sys.dba_objects;
查看各個(gè)分區(qū)的記錄數(shù) (隱約可以看出如果1,5合并的話,數(shù)據(jù)分布會(huì)非常平均):
select count(*) from test01 partition (p1);
6746
select count(*) from test01 partition (p2);
13550
select count(*) from test01 partition (p3);
13764
select count(*) from test01 partition (p4);
13445
select count(*) from test01 partition (p5);
6777
2.??直接建立分區(qū)數(shù)為8 (2的3次方) 的hash分區(qū)表test02:
create table test02
partition by hash(object_id)
(partition p1,
partition p2,
partition p3,
partition p4,
partition p5,
partition p6,
partition p7,
Partition p8)
as select * from sys.dba_objects;
查看各個(gè)分區(qū)的記錄數(shù) (數(shù)據(jù)是平均分布的):
select count(*) from test02 partition (p1);
6750
select count(*) from test02 partition (p2);
6861
select count(*) from test02 partition (p3);
6891
select count(*) from test02 partition (p4);
6682
select count(*) from test02 partition (p5);
6778
select count(*) from test02 partition (p6);
6689
select count(*) from test02 partition (p7);
6874
select count(*) from test02 partition (p8);
6766
3.??在test01上增加hash分區(qū)p6:
alter table test01 add partition p6 ;
這時(shí)候后來看test01的數(shù)據(jù)分布:
select count(*) from test01 partition (p1); -- 沒變
6746
select count(*) from test01 partition (p2); -- 少了6689
6861
select count(*) from test01 partition (p3); -- 沒變
13764
select count(*) from test01 partition (p4); -- 沒變
13445
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 恰好是6689
6689
4.??在test01上增加hash分區(qū)p7:
alter table test01 add partition p7 ;
這時(shí)候后來看test01的數(shù)據(jù)分布(以下比較是相對(duì)于加入p6后):
select count(*) from test01 partition (p1); -- 沒變
6746
select count(*) from test01 partition (p2); -- 沒變
6861
select count(*) from test01 partition (p3); -- 少了6874
6890
select count(*) from test01 partition (p4); -- 沒變
13445
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 沒變
6689
select count(*) from test01 partition (p7); -- 恰好是6874
6874
5.??在test01上增加hash分區(qū)p8:
alter table test01 add partition p8 ;
這時(shí)候后來看test01的數(shù)據(jù)分布(以下比較是相對(duì)于加入p7后):
select count(*) from test01 partition (p1); -- 沒變
6746
select count(*) from test01 partition (p2); -- 沒變
6861
select count(*) from test01 partition (p3); -- 沒變
6890
select count(*) from test01 partition (p4); -- 少了6765
6680
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 沒變
6689
select count(*) from test01 partition (p7); -- 沒變
6874
select count(*) from test01 partition (p7); -- 恰好是6765
6765
大家從上面的數(shù)據(jù)分布拆分情況可以大致看出Oracle是如何將數(shù)據(jù)平均分布
的,也應(yīng)該大致理解了為什么Oracle的HASH分區(qū)數(shù)建議是2個(gè)冪 。
還可以看到加入到8個(gè)分區(qū)(2的3次方)后數(shù)據(jù)都平均分布了,和一次性直接劃分
為8個(gè)分區(qū)數(shù)據(jù)分布比較接近 (但是不相同)。
6.??下面簡(jiǎn)單測(cè)試一下如果從8個(gè)分區(qū)繼續(xù)加入到9,10,11,16
個(gè)分區(qū)又是怎樣的情況呢 ? 這里我們還是以test01表來做測(cè)試。
alter table test01 add partition p9 ;
這時(shí)候后來看test01的數(shù)據(jù)分布(以下比較是相對(duì)于加入p8后):
select count(*) from test01 partition (p1); -- 少了3390
3356
select count(*) from test01 partition (p2); -- 沒變
6861
select count(*) from test01 partition (p3); -- 沒變
6890
select count(*) from test01 partition (p4); -- 沒變
6680
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 沒變
6689
select count(*) from test01 partition (p7); -- 沒變
6874
select count(*) from test01 partition (p8); -- 沒變
6765
select count(*) from test01 partition (p9); -- 恰好是3390
3390
7.??alter table test01 add partition p10 ;
這時(shí)候后來看test01的數(shù)據(jù)分布(以下比較是相對(duì)于加入p9后):
select count(*) from test01 partition (p1); -- 沒變
3356
select count(*) from test01 partition (p2); -- 少了3443
3418
select count(*) from test01 partition (p3); -- 沒變
6890
select count(*) from test01 partition (p4); -- 沒變
6680
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 沒變
6689
select count(*) from test01 partition (p7); -- 沒變
6874
select count(*) from test01 partition (p8); -- 沒變
6765
select count(*) from test01 partition (p9); -- 沒變
3390
select count(*) from test01 partition (p10); -- 恰好是3443
3443
8.??alter table test01 add partition p11 ;
這時(shí)候后來看test01的數(shù)據(jù)分布(以下比較是相對(duì)于加入p10后):
select count(*) from test01 partition (p1); -- 沒變
3356
select count(*) from test01 partition (p2); -- 沒變
3418
select count(*) from test01 partition (p3); -- 少了3444
3446
select count(*) from test01 partition (p4); -- 沒變
6680
select count(*) from test01 partition (p5); -- 沒變
6777
select count(*) from test01 partition (p6); -- 沒變
6689
select count(*) from test01 partition (p7); -- 沒變
6874
select count(*) from test01 partition (p8); -- 沒變
6765
select count(*) from test01 partition (p9); -- 沒變
3390
select count(*) from test01 partition (p10); -- 沒變
3443
select count(*) from test01 partition (p11); -- 恰好是3444
3444
OK, 其實(shí)不用測(cè)試這么多,大家就可以看出規(guī)律了,但是這里之所以測(cè)試
這些, 是為了通過概率的方式統(tǒng)計(jì)一下到底每次在拆分?jǐn)?shù)據(jù)量的時(shí)候有什
么規(guī)律 (雖然大前提是hash算法)。 這里可以粗略知道的是: 假設(shè)一個(gè)
表從8個(gè)分區(qū)增加到16個(gè)分區(qū), partition 1~8 的 hash bucket no 應(yīng)
該和9~16 的對(duì)應(yīng)相等,因?yàn)?~16的數(shù)據(jù)都是分別從1~8 partition中
拆分出來的 。
9. 現(xiàn)在我們一次性將分區(qū)加到16個(gè),看看數(shù)據(jù)分布情況,明顯已經(jīng)均勻分布了。
select count(*) from test01 partition (p1);
3356
select count(*) from test01 partition (p2);
3418
select count(*) from test01 partition (p3);
3446
select count(*) from test01 partition (p4);
3322
select count(*) from test01 partition (p5);
3427
select count(*) from test01 partition (p6);
3367
select count(*) from test01 partition (p7);
3392
select count(*) from test01 partition (p8);
3421
select count(*) from test01 partition (p9);
3390
select count(*) from test01 partition (p10);
3443
select count(*) from test01 partition (p11);
3444
select count(*) from test01 partition (p12);
3358
select count(*) from test01 partition (p13);
3350
select count(*) from test01 partition (p14);
3322
select count(*) from test01 partition (p15);
3482
select count(*) from test01 partition (p16);
3344
[本帖最后由 tolywang 于 2011-1-21 10:01 編輯]
總結(jié)
以上是生活随笔為你收集整理的oracle每一行的hash值,Hash分区表分区数与数据分布的测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wordpress 新建php文件大小,
- 下一篇: oracle按时间导出,如何为Oracl