您的位置:首页 > 数据库 > Oracle

Oracle用HASH簇表优化latch hit过低

2017-09-02 11:00 169 查看

部署测试环境

以SYS用户在HR中建立与DBA_OBJECTS相同的表,重复插入得到100万行,并且添加一列ID,为这列添加连续的值,并建立唯一索引。

将AWR快照的产生时间设为10分钟。

在五个窗口并行执行:

建立簇表所需信息

HASHKEYS=distinct values

SIZE=(avg_row_len*total_rows/HashKeys)*1.1 1.2可能更保险。

HR@ prod> select num_distinct from user_tab_columns where table_name = 'TEST_LARGE' and column_name = 'ID' ;

NUM_DISTINCT
------------
1164720

HR@ prod> select avg_row_len , num_rows from user_tables where table_name = 'TEST_LARGE' ;

AVG_ROW_LEN   NUM_ROWS
----------- ----------
102    1164720

HR@ prod> select 102*1.2 "size" from dual ;

size
----------
122.4


建立簇表

将原表重命名:

alter table test_large rename to test_large1 ;


建立簇(注意,簇会分配空间,大小为 SIZE*HASHKEYS),相当于建立好了框架,加了single table可以控制本簇只能放一个表:

create cluster cluster_bl(id number(7))
hashkeys 1164720 single table hash is id
size 150 ;


将原表移至簇中(注意:这个表没有段,是放在簇中的):

create table test_large  cluster cluster_bl ( id ) as select * from test_large1 ;


为了支持范围查询,可以为这个表添加主键。这样就可以范围查询了。

当以簇键为条件访问表时,不会使用索引。

进行性能测试

以下两个脚本同时运行,一个访问的是HASH聚簇表,一个访问的是加了主键索引的堆表,比较哪个执行更快:

[oracle@dbsvr1 ~]$ cat test_latch_hit.sh
#!/bin/bash
start=0
while true
do
sqlplus hr/hr << eof
declare
rec hr.test_large1%rowtype ;
i number ;
j number ;
begin
j := mod(abs(dbms_random.random()) , 1140000 ) ;
for i in 1..3000 loop
select * into rec from hr.test_large1 where id = i + j ;
end loop ;
end ;
/
quit
eof
let start=start+1
echo $start
done

[oracle@dbsvr1 ~]$ cat test_latch_hit1.sh
#!/bin/bash
start=0
while true
do
sqlplus hr/hr << eof
declare
rec hr.test_large%rowtype ;
i number ;
j number ;
begin
j := mod(abs(dbms_random.random()) , 1140000 ) ;
for i in 1..3000 loop
select * into rec from hr.test_large where id = i + j ;
end loop ;
end ;
/
quit
eof
let start=start+1
echo $start
done


实验结果表明,访问HASH聚簇表比用索引访问堆表快1.1倍左右。这是在没有latch竞争的情况下做的测试。

性能测试二(并发性能)

为了探索latch对索引访问的影响,采用8个窗口并发的方式进行了测试。

在6分钟的时间里,索引访问方式第一个窗口的执行数是285最后一个窗口的执行数是134。

同样的时间里,散列聚簇表的访问方式第一个是308最后一个是277,各个窗口间的差异不大,这应该并行上的差距。

所以用HASH聚簇表来优化某些静态表(不常更新的表)是可行的。可以访问性能以及并发性能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle 优化