总结:
1 对于存在索引的表,自动采用索引模式,未能使用inmemory特性。
2 对于使用全表扫描模式,对于单实例不需要设置parallel_DEGREE_POLICY,对于集群环境需要设置参数为auto。参数主要租用与inmemory表。
1 查询INMEMORY表分布;
SELECT * FROM GV$IM_SEGMENTS;
2 查询INMEMORY列
SELECT * FROM GV$IM_COLUMN_LEVEL
一个查询测试引发的思考:关于参数parallel_DEGREE_POLICY
2 从查询中我们发现节点2 v$im_segments为空。则全部缓存在节点1上。(节点1能够查询到),则节点2 需要开启parallel_degree_policy。
Oracle 12C In-Memory特性研究
一、Oracle In-Memory
1.1 In-Memory 开启方法
1.2 开启与关闭IM column store
1.3 inmemory优先级调整
1.4 加载对象到IM
二、In-Memory测试
2.1 全字段查询
2.2 索引字段比较测试
2.3 批量update测试
2.4 大批量insert测试
2.5 大量delete测试
2.6 压缩方式测试
2.7 查询大量列性能测试
三、评估对象在IMO大小
四、参考文献
一、Oracle In-Memory
IM特性是需要不定期维护的,不仅需要关注IM中表是否加载成功,是否出现In-Memory Area不足的情况,在内存资源有限的情况下,还要优化IM中存放的表,及时添加需要的表,删除不需要的表。此外,还可以添加或删除IM表中的部分字段或分区,达到优化内存使用的目的。
1.IM添加和删除表
-
当IM特性开启后,往in memory列式存储中加表,命令如下:Alter table tab_name inmemory;
注意,当执行以上命令时,Oracle并不会将表马上存入in memory中,而是需要通过一次查询操作触发。
删除表的操作为:Alter table tab_name no inmemory;
-
假如一张表有许多字段,但查询的时候只用到其中的某些字段,那么,就可以只存储相关字段:Alter table im_table inmemory no inmemory(object_name); --除去表的object_name字段
-
假如一张表有多个分区,可以除去其中的某些分区:Alter table im_table modify partition P_201601 no inmemory; --除去表的P_201601分区
2.维护常用视图
v$inmemory_area
In-Memory Area 分两个子池,1MB POOL 用来存放列式数据,64KB POOL用来存放元数据和事务信息。POPULATE_STATUS表明当前子池加载状态,DONE表示所有数据已经加载完毕,POPULATING表示正在加载,而OUT OF MEMORY表示分配的空间不足,需要删表或增大In-Memory Area。
v$im_segments
可以用来查看inmemory中添加了多少表,占用多大内存空间等。
v$im_column_level
该试图用来查看IM中存放的字段情况:
可以看到object_name字段的inmemory_compression 显示为no inmemory,表明该字段并未加入IM。
3.其他注意事项
由于inmemory特性的开启是以实例为单位的,在RAC环境中,可以在不同的节点设置不同的inmemory_size, 加载不同的表,以适应不同节点访问不同数据的业务分配机制。
Inmemory_size参数可以在线修改,但是必须重启实例后才能生效,所以在启用初期就应该规划好,避免设置后出现内存不足、无法加表的情况。
Inmemory area是SGA中的一个静态子池,占用SGA,所以在加inmemory_size 的时候应该相应调大SGA,以免挤掉其他子池的空间。
行格式与列格式:
Oracle 数据库传统上以行格式存储数据。在行格式数据库中,数据库中存储的每个新事务或新记录都表示为表中的一个新行,而在查询数据时是利用传统BUFFER CACHE。
列格式数据库将表以单独的列结构存储到内存中。列格式适用于报表类,分析,选择少量列但是查询要访问大部分的数据的场景。
1.1 In-Memory 开启方法
启用IMO非常简单,12.1.0.2及之后版本下,设置INMEMORY_SIZE 为非0值便可启用IM column store特性。
INMEMORY_SIZE 是个实例级参数,默认为0,设置一个非0值时,最小值为100M。
通常情况下,sys用户下的对象及SYSTEM、SYSAUX表空间上的对象无法使用IMO特性,但通过设置“_enable_imc_sys”隐含参数也可以使用
开启DB In-Memory过程如下:
1、修改INMEMORY_SIZE参数:
SQL> ALTER SYSTEM SET INMEMORY_SIZE=1G SCOPE=SPFILE;
2、检查sga参数的设置,确保在设置完inmemroy_size参数之后数据库实例还可以正常启动。如果数据库使用了ASMM,则需要检查sga_target参数。如果使用了AMM,则需要检查MEMORY_TARGET参数,同时也需要检查SGA_MAX_TARGET(或MEMORY_MAX_TARGET)。
备注:从 12.2 开始,可以动态增加 In-Memory 区域的大小,为此,只需 通过 ALTER SYSTEM 命令增加 INMEMORY_SIZE 参数值即可
3、重启数据库实例
4、查看IM特性是否开启
SQL> SHOW PARAMETER inmemory;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
inmemory_adg_enabled boolean TRUE
inmemory_clause_default string
inmemory_expressions_usage string ENABLE
inmemory_force string DEFAULT
inmemory_max_populate_servers integer 2
inmemory_query string ENABLE
inmemory_size big integer 1G
inmemory_trickle_repopulate_servers_ integer 1
percent
inmemory_virtual_columns string MANUAL
optimizer_inmemory_aware boolean TRUE
1.2 开启与关闭IM column store
1、TABLE 级启用:
可以通过如下初始建表或后续修改表 inmemory 属性的方式进行启用:
create table test (id number) inmemory;
alter table test inmemory;
2、COLUMN 级启用:
仅启用表中某列前,该表必须先设置为 inmemory 模式:
alter table imo_t1 inmemory (id) no inmemory (name,type); alter table imo_t2 inmemory (name) no inmemory (id,type);
SELECT table_name, segment_column_id seg_col_id, column_name, inmemory_compression FROM v$im_column_level WHERE owner = 'IMOTEST' and table_name in ('IMO_T1','IMO_T2') ORDER BY 1,3;
TABLE_NAME SEG_COL_ID COLUMN_NAME INMEMORY_COMPRESSION
---------- ---------- --------------- -----------------------
IMO_T1 1 ID DEFAULT
IMO_T1 2 NAME NO INMEMORY
IMO_T1 3 TYPE NO INMEMORY
IMO_T2 1 ID NO INMEMORY
IMO_T2 2 NAME DEFAULT
IMO_T2 3 TYPE NO INMEMORY
3、表空间级启用:
可以通过如下初始创建表空间或后续修改表空间 inmemory 属性的方式进行启用,在属性为 inmemory 的表空间中创建的对象自动加载 inmemory 属性,除非显示设置对象为 no inmemory:
create tablespace imotest datafile '/u01/app/oracle/oradata/orcl/imotest01.dbf' size 100M default inmemory;
或
alter tablespace imotest default inmemory;
select tablespace_name, def_inmemory from dba_tablespaces where tablespace in ('IMOTEST');
TABLESPACE_NAME DEF_INMEMORY
------------------------------ ---------------
IMOTEST ENABLED
4、如果需要知道具体哪些列开启了IM column store则需要到V$IM_COLUMN_LEVEL中进行查看
SQL> SELECT TABLE_NAME, COLUMN_NAME, INMEMORY_COMPRESSION FROM V$IM_COLUMN_LEVEL WHERE TABLE_NAME = 'TBNAME';
1
5、查询有关IM列表属性
SQL> SELECT OWNER, SEGMENT_NAME,bytes,INMEMORY_SIZE,POPULATE_STATUS, BYTES_NOT_POPULATED FROM V$IM_SEGMENTS;
OWNER SEGMENT_NA BYTES INMEMORY_SIZE POPULATE_STAT BYTES_NOT_POPULATED
---------- ---------- ---------- ------------- ------------- -------------------
CS MEM 391880704 52428800 COMPLETED 0
CS TEST 99426304 15138816 COMPLETED 0
CS DUP 5095424 3407872 COMPLETED 0
6、关闭inmemory
alter table test no inmemory;
1
1.3 inmemory优先级调整
启用了 IMO 的对象,会按照一定的优先级进入 SGA 中配置好的 IN-MEMORY 区域,同时,在 IN-MEMORY 区域用满后,依次置换出优先级较低的对象。下表为关于 IMO 对象优先级说明:
优先级描述
PRIORITY NONE 缺省级别;执行 SQL 引起对象扫描后,触发进入 IN-MEMORY
PRIORITY CRITICAL 最高优先级;数据库启动后立即进入 IN-MEMORY
PRIORITY HIGH 在具有 CRITICAL 优先级的对象之后进入 IN-MEMORY
PRIORITY MEDIUM 在具有 CRITICAL、HIGH 优先级的对象之后进入 IN-MEMORY
PRIORITY LOW 在具有 CRITICAL、HIGH、MEDIUM 优先级的对象之后进入 IN-MEMORY
修改示例:
SQL> alter table test inmemory priority high;
SQL> col owner format a30
SQL> col segment_name format a30
SQL> SELECT INMEMORY,INMEMORY_PRIORITY,INMEMORY_COMPRESSION,INMEMORY_DISTRIBUTE, INMEMORY_DUPLICATE
FROM USER_TABLES WHERE TABLE_NAME = 'TEST';
INMEMORY INMEMORY INMEMORY_COMPRESS INMEMORY_DISTRI INMEMORY_DUPL
-------- -------- ----------------- --------------- -------------
ENABLED HIGH FOR QUERY LOW AUTO NO DUPLICATE
1.4 加载对象到IM
1、通过全表扫描objects加载数据到IM
SQL> SELECT /*+ FULL (s) */ COUNT(*) FROM table s;
查询V$IM_SEGMENTS中的数据
SQL> col owner for a10
SQL> col SEGMENT_NAME for a10
SQL> SELECT OWNER, SEGMENT_NAME, POPULATE_STATUS, BYTES_NOT_POPULATED FROM V$IM_SEGMENTS;
OWNER SEGMENT_NA POPULATE_ BYTES_NOT_POPULATED
---------- ---------- --------- -------------------
CS TEST COMPLETED 0
2、DBMS_INMEMORY来加载数据
DBMS_INMEMORY包提供了两个PROCEDURE来手工加载数据到IM中:
POPULATE:强制加载给定的表
REPOPULATE:强制重新加载给定的表,在该表已经至少加载过一次之后才可使用。
手工执行DBMS_INMEMORY.POPULATE procedure来加载TEST表到IM中
SQL> EXEC DBMS_INMEMORY.POPULATE('CS','TEST');
1
2
通过DBMS_INMEMORY.REPOPULATE prcedure及FORCE=>TRUE选项来加载TEST表到IM中。FORCE=>TRUE选项强制进行一次完整重新载入,类似于full refresh
SQL> EXEC DBMS_INMEMORY.REPOPULATE('CS','TEST', FORCE=>TRUE);
1
3、设置PRIORITY clause自动加载数据
将测试表TEST的PRIORITY级别改为HIGH。
SQL> ALTER TABLE test INMEMORY PRIORITY HIGH|MEDIUM|LOW;
1
二、In-Memory测试
2.1 全字段查询
通过CTAS方式创建表TEST,普通行式扫描与开启inmemory特性分别测试,比对执行计划。
create table test as select * from dba_objects;
1
no inmemory查询测试
set timing on
set autotrace traceonly
set pagesize 200 linesize 200
col TABLE_NAME for a20
col INMEMORY_PRIORITY for a20
col INMEMORY_DISTRIBUTE for a20
col INMEMORY_COMPRESSION for a20
全字段查询,执行计划
SQL> select count(*) from test;
COUNT(*)
----------
74329
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 403 (1)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| TEST | 74329 | 403 (1)| 00:00:01 |
-------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
5 db block gets
1460 consistent gets
0 physical reads
0 redo size
544 bytes sent via SQL*Net to client
608 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
开启in memory查询测试
SQL> alter table test inmemory;
1
SQL> select TABLE_NAME,INMEMORY_PRIORITY,INMEMORY_DISTRIBUTE,INMEMORY_COMPRESSION from user_tables;
TABLE_NAME INMEMORY_PRIORITY INMEMORY_DISTRIBUTE INMEMORY_COMPRESSION
-------------------- -------------------- -------------------- --------------------
TEST NONE AUTO FOR QUERY LOW
已开启inmemory
SQL> select pool,ALLOC_BYTES/1024/1024,USED_BYTES/1024/1024,POPULATE_STATUS,con_id
from V$INMEMORY_AREA;
POOL ALLOC_BYTES/1024/1024 USED_BYTES/1024/1024 POPULATE_STATUS CON_ID
------------ ------------ --------------------- ---------- ------------------
1MB POOL 815 0 DONE 3
64KB POOL 192 0 DONE 3
--因为只是把该表设置了INMEMORY,但是未查询过,所以查询V$INMEMORY_AREA中未使用相关内存--
全字段查询,执行计划
第一次执行:
SQL> select count(*) from test;
COUNT(*)
----------
74329
Elapsed: 00:00:00.05
Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 403 (1)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS INMEMORY FULL| TEST | 74329 | 403 (1)| 00:00:01 |
----------------------------------------------------------------------------
Statistics
----------------------------------------------------------
5 recursive calls
5 db block gets
1467 consistent gets
0 physical reads
0 redo size
544 bytes sent via SQL*Net to client
608 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rows processed
第二次执行:
Elapsed: 00:00:00.03
Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 16 (7)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS INMEMORY FULL| TEST | 74329 | 16 (7)| 00:00:01 |
----------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
3 db block gets
9 consistent gets
0 physical reads
0 redo size
544 bytes sent via SQL*Net to client
608 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
查询inmemory内存使用:
SQL> select pool,ALLOC_BYTES/1024/1024,USED_BYTES/1024/1024,POPULATE_STATUS,con_id
from V$INMEMORY_AREA;
POOL ALLOC_BYTES/1024/1024 USED_BYTES/1024/1024 POPULATE_STATUS CON_ID
-------------------------- --------------------- -------------------- -------------------------- ----------
1MB POOL 815 4 DONE 3
64KB POOL 192 .25 DONE 3
--再次查看,已经使用了分配的In-Memory中内存
性能比对:
sql plan no-inmemory inmemory
consistent gets 1460 9
physical reads 0 0
Cost 403 16
结果:
开启inmemory之后性能提升162倍
2.2 索引字段比较测试
在已开启IMO特性条件下,通过给表test增加列索引,比较IM与索引执行计划
1、在object_name上创建索引
SQL> create index idx_test_OBname on test(OBJECT_NAME);
1
2、查看表TEST是否加载到IM中
col owner for a20
col SEGMENT_NAME for a20
col POPULATE_STATUS for a20
col BYTES_NOT_POPULATED for 99999
SELECT OWNER, SEGMENT_NAME, POPULATE_STATUS, BYTES_NOT_POPULATED FROM V$IM_SEGMENTS;
OWNER SEGMENT_NAME POPULATE_STATUS BYTES_NOT_POPULATED
-------------------- -------------------- -------------------- -------------------
CS TEST COMPLETED 0
表已被加入到IM中
3、使用索引简单列查询
SQL> set autotrace traceonly
SQL> select count(*) from test where object_name='TEST';
Execution Plan
----------------------------------------------------------
Plan hash value: 3197243274
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 35 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 35 | | |
|* 2 | INDEX RANGE SCAN| IDX_TEST_OBNAME | 10 | 350 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_NAME"='TEST')
Statistics
----------------------------------------------------------
2 recursive calls
0 db block gets
6 consistent gets
2 physical reads
0 redo size
542 bytes sent via SQL*Net to client
607 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
逻辑读:6
物理:2
4、强制全表扫描查询
SQL> select /*+full(s)*/count(*) from test s where object_name='test';
Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 35 | 124 (1)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 35 | | |
|* 2 | TABLE ACCESS INMEMORY FULL| TEST | 9 | 315 | 124 (1)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - inmemory("OBJECT_NAME"='test')
filter("OBJECT_NAME"='test')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
541 bytes sent via SQL*Net to client
607 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
逻辑读:10
物理读:0
总结:在数据离散度较高,且通过索引条件过滤的扫描场景中,IM特性对性能并没有提升,传统的索引+行式存储的执行计划已经足够,在默认情况下还是会根据查询索引返回rowid的方式查找数据。
2.3 批量update测试
1、创建测试表disk、emp,分别为im表以及rows表,总数相同
SQL> create table disk as select * from dba_objects;
SQL> insert into disk select * from disk;
74360 rows created.
SQL> /
148720 rows created.
SQL> /
297440 rows created.
SQL> /
594880 rows created.
SQL> /
1189760 rows created.
SQL> commit;
Commit complete.
SQL> select count(*) from disk;
COUNT(*)
----------
2379520
SQL> create table mem as select * from disk inmemory;
更新磁盘表
update disk set owner=‘tom’;
更新内存表
update mem set owner=‘tom’;
普通表:
时间:14.49
逻辑读:3202294+894128
物理读:5602
cost:403
IM表:
时间:18.94
逻辑读:3231179+254
物理读:92476
cost:483
总结:IM特性在update性能有所下降
2.4 大批量insert测试
从磁盘插到磁盘
insert into disk select * from cs;
从磁盘插到内存
insert into mem select * from cs;
普通表:
时间:0.13
逻辑读:9332+2903
物理读:2895
cost:404
IM表:
时间:0.17
逻辑读:3231179+254
物理读:92476
cost:483
2.5 大量delete测试
delete from disk where rownum <10000;
delete from mem where rownum <10000;
普通表:
时间:0.08
逻辑读:11470+202
物理读:253
cost:12986
IM表:
时间:0.07
逻辑读:11472+9
物理读:0
cost:493
2.6 压缩方式测试
压缩方式 描述
NO MEMCOMPRESS IMO 中存储无压缩
MEMCOMPRESS FOR DML 最小化压缩,优化 DML 操作
MEMCOMPRESS FOR QUERY LOW 缺省方式:查询性能最优、空间压缩效果好于DML方式
MEMCOMPRESS FOR QUERY HIGH 查询性能次优(excellent)、空间压缩效果好于 QUERY LOW
MEMCOMPRESS FOR CAPACITY LOW 查询性能良好(good)、空间压缩效果好于 QUERY HIGH
MEMCOMPRESS FOR CAPACITY HIGH 缺省设置、空间压缩效果最优
查看压缩比
col owner for a5
col SEGMENT_NAME for a10
col POPULATE_STATUS for a10
col INMEMORY_COMPRESSION for a10
SQL> SELECT V.OWNER, V.SEGMENT_NAME,V.BYTES/1024/1024 ORIG_SIZE_MB,V.INMEMORY_SIZE/1024/1024 IN_MEM_SIZE_MB,BYTES_NOT_POPULATED,POPULATE_STATUS status,INMEMORY_COMPRESSION,V.BYTES/V.INMEMORY_SIZE COMP_RATIO FROM V$IM_SEGMENTS V WHERE SEGMENT_NAME = 'TEST';
OWNER SEGMENT_NAME ORIG_SIZE_MB IN_MEM_SIZE_MB BYTES_NOT_POPULATED STATUS INMEMORY_COMPRESS COMP_RATIO
----- -------------------- ------------ -------------- ------------------- -------------------- ----------------- ----------
CS TEST 1466.78125 110.625 0 COMPLETED FOR QUERY LOW 13.2590395
表TEST在磁盘上占用1466MB,采用默认压缩方式,内存中占用110.625MB,压缩比为13.26:1
压缩比测试:
表TEST大小1466.78125MB,数据行9514112
1、NO MEMCOMPRESS:
alter table test inmemory MEMCOMPRESS NO MEMCOMPRESS;
逻辑读:14
物理读:0
COST: 126
时间: 0.01
压缩比:1.19
2、MEMCOMPRESS FOR DML:
alter table test inmemory MEMCOMPRESS FOR DML;
逻辑读:14
物理读:0
COST: 126
时间:0.01
压缩比:1.22
3、MEMCOMPRESS FOR QUERY LOW:
alter table test inmemory MEMCOMPRESS FOR QUERY LOW;
逻辑读:14
物理读:0
COST:125
时间:0.01
压缩比:7.20
4、MEMCOMPRESS FOR QUERY HIGH:
alter table test inmemory MEMCOMPRESS FOR QUERY high;
逻辑读:14
物理读:0
COST: 125
时间:0.01
压缩比:9.67
5、MEMCOMPRESS FOR CAPACITY LOW:
alter table test inmemory MEMCOMPRESS FOR CAPACITY LOW;
逻辑读:14
物理读:0
COST: 125
时间:0.01
压缩比:12.91
6、MEMCOMPRESS FOR CAPACITY HIGH:
alter table test inmemory MEMCOMPRESS FOR CAPACITY HIGH;
逻辑读:14
物理读:0
COST: 25
时间:0.01
压缩比:19.14
测试结果汇总:
压缩方式 压缩比
NO MEMCOMPRESS 1.19
MEMCOMPRESS FOR DML 1.22
MEMCOMPRESS FOR QUERY LOW 7.20
MEMCOMPRESS FOR QUERY HIGH 9.67
MEMCOMPRESS FOR CAPACITY LOW 12.91
MEMCOMPRESS FOR CAPACITY HIGH 19.14
总结:压缩方式不同,表加载到IM中的时间也会不一样。压缩比越大加载到内存中的时间越长。而对于select查询消耗时间不影响。
2.7 查询大量列性能测试
三、评估对象在IMO大小
DBMS_COMPRESSION.GET_COMPRESSION_RATIO
在对一张表使用COMPRESSION clause进行IM压缩级别设置之前,我们可以通过Oracle的COMPRESSION ADVISOR对表放入到IM中的大小进行提前计算。
SET SERVEROUTPUT ON
DECLARE
l_blkcnt_cmp PLS_INTEGER;
l_blkcnt_uncmp PLS_INTEGER;
l_row_cmp PLS_INTEGER;
l_row_uncmp PLS_INTEGER;
l_cmp_ratio PLS_INTEGER;
l_comptype_str VARCHAR2(100);
BEGIN
dbms_compression.get_compression_ratio (
-- Input parameters
scratchtbsname => 'CS',
ownname => 'CS',
objname => 'TEST',
subobjname => NULL,
comptype => dbms_compression.comp_inmemory_QUERY_LOW,
-- Output parameter
blkcnt_cmp => l_blkcnt_cmp,
blkcnt_uncmp => l_blkcnt_uncmp,
row_cmp => l_row_cmp,
row_uncmp => l_row_uncmp,
cmp_ratio => l_cmp_ratio,
comptype_str => l_comptype_str,
subset_numrows => dbms_compression.comp_ratio_allrows);
dbms_output.put_line('Comp. ratio (QUERY LOW):'||l_cmp_ratio);
END;
/
Comp. ratio (QUERY LOW):7
估计结果压缩比7,实际压缩比为7.20
#五、RAC环境测试
rac环境独有参数DUPLICATE clause、DISTRIBUTE clause:
DUPLICATE clause:
此参数为EXADATA一体机专用,在RAC环境中,每个节点拥有自己的IM Area。一个objects根据DUPLICATE clause的设置将一样的数据加载到多个IM Area中。
默认是NO DUPLICATE设置,表示在数据库的IM中对一个objects在所有节点中合起来只保存一份。举例说明,比如三节点的RAC中,对于分区表SALES来讲可能2012年份的数据在1节点,2013年份的数据在2节点,2014年份的数据在3节点,每个分区只保存在一个节点上。
为了提升可用性,也可以设置为DUPLICAET ALL,在每个节点上都保存一份。举例说明,还是刚才那个SALES表的请款下,1,2,3三个节点各保存一份完整sales表数据到各自的IM中。在任意一个节点上都可以获取查询需要的数据。
在设置为DUPLICATE ALL的情况下
DISTRIBUTE clause:
如果一个objects因为太大无法被加载到一个IM Area中,还可以通过DISTRIBUTE clause的设置将它分成几个数据片分别加载到不同的节点中。
默认情况下DISTRIBUTE clause的默认值为AUTO-DISTRIBUTE,这时候是否将objects分布式分布在不同的节点上由Oracle内部算法决定。这个参数对于单实例没有影响,在RAC环境中,默认存在IM中的表会分布在各个节点之中。
RAC环境并不像单实例一样只需修改表的IM属性即可启用,如果要使用IMO,必须在系统层面修改“并行度策略”为自动。下面对参数“parallel_degree_policy”,分别测试2个场景
1、先查看要测试的表TEST是否已加载到IM STORE
col SEGMENT_NAME for a5
col POPULATE_STATUS for a10
col INMEMORY_DISTRIBUTE for a10
col INMEMORY_DUPLICATE for a20
SQL> select INST_ID,SEGMENT_NAME,INMEMORY_SIZE/1024/1024,BYTES/1024/1024,BYTES_NOT_POPULATED/1024/1024,INMEMORY_DISTRIBUTE,INMEMORY_DUPLICATE,POPULATE_STATUS from gv$im_segments;
INST_ID SEGME INMEMORY_SIZE/1024/1024 BYTES/1024/1024 BYTES_NOT_POPULATED/1024/1024 INMEMORY_D INMEMORY_DUPLICATE POPULATE_S
---------- ----- ----------------------- --------------- ----------------------------- ---------- -------------------- ----------
2 TEST 9.125 192 123.53125 AUTO NO DUPLICATE COMPLETED
1 TEST 16.1875 192 66.9140625 AUTO NO DUPLICATE COMPLETED
1、开启AUTO DOP
alter system set parallel_degree_policy=AUTO sid='*';
alter system set parallel_force_local=false sid='*';
alter system flush buffer_cache;
alter system flush shared_pool;
2、查看执行计划
SQL> select /*+ parallel */count(*) from test;
COUNT(*)
----------
1455168
Execution Plan
----------------------------------------------------------
Plan hash value: 2661943167
-----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
-----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 (0)| 00:00:01 | | | |
| 1 | SORT AGGREGATE | | 1 | | | | | |
| 2 | PX COORDINATOR | | | | | | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | | | Q1,00 | P->S | QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | | | Q1,00 | PCWP | |
| 5 | PX BLOCK ITERATOR | | 90948 | 9 (0)| 00:00:01 | Q1,00 | PCWC | |
| 6 | TABLE ACCESS INMEMORY FULL| TEST | 90948 | 9 (0)| 00:00:01 | Q1,00 | PCWP | |
-----------------------------------------------------------------------------------------------------------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 2
- parallel scans affinitized for inmemory
Statistics
----------------------------------------------------------
232 recursive calls
4 db block gets
163 consistent gets
15 physical reads
0 redo size
545 bytes sent via SQL*Net to client
551 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
16 sorts (memory)
0 sorts (disk)
1 rows processed
逻辑读:163
物理读:15
3、不使用并行查询查询
SQL> select count(*) from test;
COUNT(*)
----------
1455168
Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 16 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS INMEMORY FULL| TEST | 90948 | 16 (0)| 00:00:01 |
----------------------------------------------------------------------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 1 because of no expensive parallel operation
Statistics
----------------------------------------------------------
3 recursive calls
0 db block gets
9081 consistent gets
9077 physical reads
0 redo size
545 bytes sent via SQL*Net to client
551 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
逻辑读:9081
物理读:9077
4、关闭AUTO DOP
alter system set parallel_degree_policy=MANUAL sid='*';
alter system set parallel_force_local=false sid='*';
alter system flush buffer_cache;
alter system flush shared_pool;
5、查看执行计划
SQL> select /*+ parallel */count(*) from test;
COUNT(*)
----------
1455168
Execution Plan
----------------------------------------------------------
Plan hash value: 2661943167
-----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
-----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 (0)| 00:00:01 | | | |
| 1 | SORT AGGREGATE | | 1 | | | | | |
| 2 | PX COORDINATOR | | | | | | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | | | Q1,00 | P->S | QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | | | Q1,00 | PCWP | |
| 5 | PX BLOCK ITERATOR | | 90948 | 9 (0)| 00:00:01 | Q1,00 | PCWC | |
| 6 | TABLE ACCESS INMEMORY FULL| TEST | 90948 | 9 (0)| 00:00:01 | Q1,00 | PCWP | |
-----------------------------------------------------------------------------------------------------------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 2
- parallel scans affinitized for inmemory
Statistics
----------------------------------------------------------
209 recursive calls
4 db block gets
17909 consistent gets
17766 physical reads
0 redo size
545 bytes sent via SQL*Net to client
551 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
16 sorts (memory)
0 sorts (disk)
1 rows processed
逻辑读:17909
物理读:17766
总结:
1、调整系统参数(parallel_degree_policy)之后,rac环境下的IM列查询大幅降低了逻辑读与物理读。
2、在多实例的并发查询中实例之间传输的并不是IMCU,而是每个节点都会对本节点的数据运行相同的sql语句,之后把自己的结果集发送给发起sql语句的实例,组成最终的结果返回给用户。
parallel_degree_policy AUTO MANUAL
逻辑读 163 17909
物理读 15 17766
备注:
在没有显式使用并行sql时,rac环境im全表扫描并没有使用并行。oracle的优化器会通过一系列的计算比较cost。
SQL> select count(*) from test;
执行计划Note
-----
automatic DOP:
Computed Degree of Parallelism is 1 because of no expensive parallel operation
1
2
3
4
5
In some cases, even with Auto DOP set correctly, the optimizer may calculate the cost of a serial access to be less than the cost of a parallel access. This has been identified as bug 18960760, and will usually only happen when very smalltables in the IM column store are involved in the query.
————————————————
[oracle@gzrac01 ~]$ sqlplus / as sysdba
SQL> set autotrace traceonly
SQL> select count(1) from nmosdb.CLR; ------------没有自动选择,还是应用了索引扫描,TAF IN MEMORY路径。
Execution Plan
----------------------------------------------------------
Plan hash value: 2541445809
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time
| Pstart| Pstop |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 579K (1)| 00:00:
23 | | |
| 1 | SORT AGGREGATE | | 1 | |
| | |
| 2 | PARTITION RANGE ALL | | 322M| 579K (1)| 00:00:
23 | 1 | 14 |
| 3 | INDEX FAST FULL SCAN| IX3_CLR | 322M| 579K (1)| 00:00:
23 | 1 | 14 |
--------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
128980 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
403 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> alter session set parallel_degree_policy=auto; ---如果在system级别设置,即使设置了也没有走TAF IN MEMORY路径。
Session altered.
SQL> select count(1) from nmosdb.CLR;
Execution Plan
----------------------------------------------------------
Plan hash value: 1616656644
--------------------------------------------------------------------------------
----------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Tim
e | Pstart| Pstop | TQ |IN-OUT| PQ Distrib |
--------------------------------------------------------------------------------
----------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4126 (1)| 00:
00:01 | | | | | |
| 1 | SORT AGGREGATE | | 1 | |
| | | | | |
| 2 | PX COORDINATOR | | | |
| | | | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | |
| | | Q1,00 | P->S | QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | |
| | | Q1,00 | PCWP | |
| 5 | PX BLOCK ITERATOR | | 322M| 4126 (1)| 00:
00:01 | 1 | 14 | Q1,00 | PCWC | |
| 6 | INDEX FAST FULL SCAN| IX3_CLR | 322M| 4126 (1)| 00:
00:01 | 1 | 14 | Q1,00 | PCWP | |
Note
-----
- automatic DOP: Computed Degree of Parallelism is 156
Statistics
----------------------------------------------------------
612 recursive calls
8 db block gets
239642 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
403 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select /*+full(t)*/count(1) from nmosdb.CLR t;
Execution Plan
----------------------------------------------------------
Plan hash value: 3243308815
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| T
ime | Pstart| Pstop | TQ |IN-OUT| PQ Distrib |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4365 (3)| 0
0:00:01 | | | | | |
| 1 | SORT AGGREGATE | | 1 | |
| | | | | |
| 2 | PX COORDINATOR | | | |
| | | | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | |
| | | Q1,00 | P->S | QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | |
| | | Q1,00 | PCWP | |
| 5 | PX BLOCK ITERATOR | | 322M| 4365 (3)| 0
0:00:01 | 1 | 14 | Q1,00 | PCWC | |
| 6 | TABLE ACCESS INMEMORY FULL| CLR | 322M| 4365 (3)| 0
0:00:01 | 1 | 14 | Q1,00 | PCWP | |
--------------------------------------------------------------------------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 298
- parallel scans affinitized for inmemory
Statistics
----------------------------------------------------------
1040 recursive calls
8 db block gets
1215 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
417 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> alter session set parallel_degree_policy=manual;
Session altered.
SQL> select /*+full(t)*/count(1) from nmosdb.CLR t;
Execution Plan
----------------------------------------------------------
Plan hash value: 3743645769
--------------------------------------------------------------------------------
----------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time
| Pstart| Pstop |
--------------------------------------------------------------------------------
----------------------
| 0 | SELECT STATEMENT | | 1 | 1170K (3)| 00:0
0:46 | | |
| 1 | SORT AGGREGATE | | 1 | |
| | |
| 2 | PARTITION RANGE ALL | | 322M| 1170K (3)| 00:0
0:46 | 1 | 14 |
| 3 | TABLE ACCESS INMEMORY FULL| CLR | 322M| 1170K (3)| 00:0
0:46 | 1 | 14 |
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
417 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
节点2 执行依然缓慢。session级别开启parallel_degree_policy=auto,全表扫描变快。
SQL> select /*+full(t)*/count(1) from nmosdb.CLR t;
^Cselect /*+full(t)*/count(1) from nmosdb.CLR t
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> alter session set parallel_degree_policy=auto;
Session altered.
SQL> select /*+full(t)*/count(1) from nmosdb.CLR t;
COUNT(1)
----------
31323666
SQL> set lines 120
SQL> set autotrace traceonly
SQL> select /*+full(t)*/count(1) from nmosdb.CLR t; --0.281s
Execution Plan
----------------------------------------------------------
Plan hash value: 3243308815
------------------------------------------------------------------------------------------------------------------------
--------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time | Pstart| Pstop | TQ |IN-OUT
| PQ Distrib |
------------------------------------------------------------------------------------------------------------------------
--------------
| 0 | SELECT STATEMENT | | 1 | 4171 (3)| 00:00:01 | | | |
| |
| 1 | SORT AGGREGATE | | 1 | | | | | |
| |
| 2 | PX COORDINATOR | | | | | | | |
| |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | | | | | Q1,00 | P->S
| QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | | | | | Q1,00 | PCWP
| |
| 5 | PX BLOCK ITERATOR | | 322M| 4171 (3)| 00:00:01 | 1 | 14 | Q1,00 | PCWC
| |
| 6 | TABLE ACCESS INMEMORY FULL| CLR | 322M| 4171 (3)| 00:00:01 | 1 | 14 | Q1,00 | PCWP
| |
------------------------------------------------------------------------------------------------------------------------
--------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 320 because of degree limit
- parallel scans affinitized for inmemory
Statistics
----------------------------------------------------------
960 recursive calls
0 db block gets
1215 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
417 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(1) from nmosdb.CLR t; ---1s
Execution Plan
----------------------------------------------------------
Plan hash value: 1616656644
------------------------------------------------------------------------------------------------------------------------
------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time | Pstart| Pstop | TQ |IN-OUT|
PQ Distrib |
------------------------------------------------------------------------------------------------------------------------
------------
| 0 | SELECT STATEMENT | | 1 | 4126 (1)| 00:00:01 | | | | |
|
| 1 | SORT AGGREGATE | | 1 | | | | | | |
|
| 2 | PX COORDINATOR | | | | | | | | |
|
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | | | | | Q1,00 | P->S |
QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | | | | | Q1,00 | PCWP |
|
| 5 | PX BLOCK ITERATOR | | 322M| 4126 (1)| 00:00:01 | 1 | 14 | Q1,00 | PCWC |
|
| 6 | INDEX FAST FULL SCAN| IX3_CLR | 322M| 4126 (1)| 00:00:01 | 1 | 14 | Q1,00 | PCWP |
|
Note
-----
- automatic DOP: Computed Degree of Parallelism is 156
Statistics
----------------------------------------------------------
472 recursive calls
8 db block gets
239642 consistent gets
0 physical reads
0 redo size
553 bytes sent via SQL*Net to client
405 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
parallel_DEGREE_POLICY参数测试
这个参数似乎只是针对inmemory特性的对象。
SQL> alter session set parallel_degree_policy=auto;
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 15 (7)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T1 | 73611 | 15 (7)| 00:00:01 |
----------------------------------------------------------------------------
SQL> alter session set parallel_degree_policy=manual;
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 15 (7)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T1 | 73611 | 15 (7)| 00:00:01 |
----------------------------------------------------------------------------
alter table nmosdb.t1 inmemory
SQL> alter session set parallel_degree_policy=manual;
SQL> set autotrace traceonly
SQL> select count(1) from nmosdb.t1;
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 15 (7)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS INMEMORY FULL| T1 | 73611 | 15 (7)| 00:00:01 |
----------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
1370 consistent gets
0 physical reads
0 redo size
552 bytes sent via SQL*Net to client
392 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> alter session set parallel_degree_policy=auto;
Session altered.
SQL> select count(1) from nmosdb.t1;
Execution Plan
----------------------------------------------------------
Plan hash value: 3110199320
--------------------------------------------------------------------------------
---------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time
| TQ |IN-OUT| PQ Distrib |
--------------------------------------------------------------------------------
---------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 (0)| 00:00:
01 | | | |
| 1 | SORT AGGREGATE | | 1 | |
| | | |
| 2 | PX COORDINATOR | | | |
| | | |
| 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 | |
| Q1,00 | P->S | QC (RAND) |
| 4 | SORT AGGREGATE | | 1 | |
| Q1,00 | PCWP | |
| 5 | PX BLOCK ITERATOR | | 73611 | 8 (0)| 00:00:
01 | Q1,00 | PCWC | |
| 6 | TABLE ACCESS INMEMORY FULL| T1 | 73611 | 8 (0)| 00:00:
01 | Q1,00 | PCWP | |
--------------------------------------------------------------------------------
---------------------------------
Note
-----
- automatic DOP: Computed Degree of Parallelism is 2
- parallel scans affinitized for inmemory
Statistics
----------------------------------------------------------
6 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
552 bytes sent via SQL*Net to client
392 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed