2.3 cursor_sharing=similar
在这种模式下,对表做统计和没做统计分析是不一样的。有关统计信息,参考
http://www.linuxidc.com/Linux/2011-07/39435.htm
2.3.1 没有统计信息,没有直方图的情况
-- 查询ta的统计信息
SYS@anqing2(rac2)> exec dbms_stats.delete_table_stats('SYS','TA');
PL/SQL procedure successfully completed.
-- 修改cursor_sharing 模式
SYS@anqing2(rac2)> alter session set cursor_sharing='similar';
Session altered.
SYS@anqing2(rac2)> show parameter cursor_sharing
NAME TYPE VALUE
---------------------- ------------------ ----------------
cursor_sharing string similar
在similer模式下,且没有直方图,等于force 模式。 看测试:
-- 查看硬解析次数
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse time cpu 1882222
parse time elapsed 2648406
parse count (total) 12793472
parse count (hard) 9890538
parse count (failures) 74
--做一次select 查询
SYS@anqing2(rac2)> select * from ta where id=238;
ID NAME
---------- ------------------------------
238 dave
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse time cpu 1882222
parse time elapsed 2648406
parse count (total) 12793476
parse count (hard) 9890539 硬解析加一
parse count (failures) 74
-- 在次select
SYS@anqing2(rac2)> select * from ta where id=2397;
ID NAME
---------- ----------
2397 dave
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse time cpu 1882337
parse time elapsed 2648558
parse count (total) 12823605
parse count (hard) 9890751
parse count (failures) 75
-- 这里还是有增加硬解析,有点奇怪
-- 查看对应的SQL
SYS@anqing2(rac2)> select sql_text,child_number from v$sql where sql_text like 'select * from ta where%';
SQL_TEXT CHILD_NUMBER
---------------------------------------- ------------
select * from ta where id=238 0
select * from ta where id=2396 0
select * from ta where id=2391 0
select * from ta where id=2397 0
这里并没有将谓词值转换成变量,也就是说similar 没有生效,没有达到force的效果。
在如下Blog 上有对Force 没有生效的一个讨论:
摘取一段如下:
Interesting. You may need to flush the shared pool "harder"... Try your testcase again, but issue the ALTER SYSTEM FLUSH SHARED_POOL twice in a row before changing the cursor_sharing setting. It looks like that the statement doesn't show up in V$SQLAREA any longer but still seems not to be flushed entirely and I can find some left-overs of it in the underlying X$KGL library cache fixed tables with the same hash value. After the second "flush shared pool" these are gone and then the cursor sharing works as expected.
解决方法就是在指定cursor_sharing 之前,执行2次flush shared_pool. 测试一下看看。
SYS@anqing2(rac2)> alter system flush shared_pool;
System altered.
SYS@anqing2(rac2)> alter system flush shared_pool;
System altered.
SYS@anqing2(rac2)> alter session set cursor_sharing=similar;
Session altered.
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse count (hard) 9890936
SYS@anqing2(rac2)> select * from ta where id=232;
ID NAME
---------- ----------
232 dave
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse count (hard) 9890946
--从硬解析来看,数量有增加
-- 在查询一次
SYS@anqing2(rac2)> select * from ta where id=23218;
ID NAME
---------- ----------
23218 dave
SYS@anqing2(rac2)> select name,value from v$sysstat where name like '%parse%';
NAME VALUE
------------------------------ ----------
parse count (hard) 9890954
--硬解析又增加了,如果说只有这一条SQL执行,不会增加硬解析。 这个是其他的SQL 带来的硬解析,我们可以查看v$sql验证:
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select * from ta where%';
SQL_TEXT CHILD_NUMBER EXECUTIONS
---------------------------------------- ------------ ----------
select * from ta where id=:"SYS_B_0" 0 2
-- 在这里,已经使转成成了变量,并且执行了2次。 这说明,我们的force是生效的。
2.3.2 有统计信息和直方图
当cursor_sharing 设置为similar,并且表上存在histograms时, 这时候的similar 等于 默认值 exact. 下面我们验证一下。
-- 分析表
SYS@anqing2(rac2)> exec dbms_stats.gather_table_stats('SYS','TA', METHOD_OPT =>'FOR COLUMNS SIZE 10 ID');
PL/SQL procedure successfully completed.
--这里我手工的指定了收集的类型。
-- 查看直方图: histograms
这部分内容,具体可以参考: Oracle Statistic 统计信息 小结
http://www.linuxidc.com/Linux/2011-07/39435.htm
SYS@anqing2(rac2)> SELECT column_name, num_distinct, num_buckets, histogram
2 FROM DBA_TAB_COL_STATISTICS WHERE table_name = 'TA' AND column_name = 'ID';
COLUMN_NAME NUM_DISTINCT NUM_BUCKETS HISTOGRAM
------------------------------ ------------ ----------- ---------------
ID 1005510 10 HEIGHT BALANCED
-- 先flush shared_pool 两次
SYS@anqing2(rac2)> alter system flush shared_pool;
System altered.
SYS@anqing2(rac2)> alter system flush shared_pool;
System altered.
SYS@anqing2(rac2)> show parameter cursor_sharing
NAME TYPE VALUE
------------------------ -------------------------------- ----------
cursor_sharing string SIMILAR
-- select 查询和对应的解析
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select count(*) from ta %';
SQL_TEXT CHILD_NUMBER EXECUTIONS
--------------------------------------------- ------------ ----------
select count(*) from ta where id>:"SYS_B_0" 0 3
SYS@anqing2(rac2)> select count(*) from ta where id>10000;
COUNT(*)
----------
990000
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select count(*) from ta %';
SQL_TEXT CHILD_NUMBER EXECUTIONS
--------------------------------------------- ------------ ----------
select count(*) from ta where id>:"SYS_B_0" 0 3
select count(*) from ta where id>:"SYS_B_0" 1 1
--因为ID值和之前的不一样,所以没有重用之前的cursor,而是重新创建了一个
SYS@anqing2(rac2)> select count(*) from ta where id>100;
COUNT(*)
----------
999900
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select count(*) from ta %';
SQL_TEXT CHILD_NUMBER EXECUTIONS
--------------------------------------------- ------------ ----------
select count(*) from ta where id>:"SYS_B_0" 0 3
select count(*) from ta where id>:"SYS_B_0" 1 1
select count(*) from ta where id>:"SYS_B_0" 2 1
-- 这次ID 值又不一样,又重新生成了一个cursor
..... 中间又生成了N 次...
SYS@anqing2(rac2)> select count(*) from ta where id>90808;
COUNT(*)
----------
909192
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select count(*) from ta %';
SQL_TEXT CHILD_NUMBER EXECUTIONS
--------------------------------------------- ------------ ----------
select count(*) from ta where id>:"SYS_B_0" 0 3
select count(*) from ta where id>:"SYS_B_0" 1 1
select count(*) from ta where id>:"SYS_B_0" 2 1
select count(*) from ta where id>:"SYS_B_0" 3 1
select count(*) from ta where id>:"SYS_B_0" 4 1
select count(*) from ta where id>:"SYS_B_0" 5 1
select count(*) from ta where id>:"SYS_B_0" 6 1
select count(*) from ta where id>:"SYS_B_0" 7 1
-- 这个就像我们开始说的,虽然会对变量进行转换,但不会重用,只有谓词值一样的时候才重用之前的cursor。
SYS@anqing2(rac2)> select count(*) from ta where id>90808;
COUNT(*)
----------
909192
SYS@anqing2(rac2)> select sql_text,child_number,executions from v$sql where sql_text like 'select count(*) from ta %';
SQL_TEXT CHILD_NUMBER EXECUTIONS
--------------------------------------------- ------------ ----------
select count(*) from ta where id>:"SYS_B_0" 0 3
select count(*) from ta where id>:"SYS_B_0" 1 1
select count(*) from ta where id>:"SYS_B_0" 2 1
select count(*) from ta where id>:"SYS_B_0" 3 1
select count(*) from ta where id>:"SYS_B_0" 4 1
select count(*) from ta where id>:"SYS_B_0" 5 1
select count(*) from ta where id>:"SYS_B_0" 6 1
select count(*) from ta where id>:"SYS_B_0" 7 2
-- 注意这里,因为我们执行了一个一样的SQL,包括谓词,这里的重用了之前的cursor。
总结:
当cursor_sharing 设置为similar时,Oracle 会对SQL 的位置值转换成常量,这个和force 一样。 但是不同的是:
(1)当字段上有histograms时, 这时等于exact,虽然SQL text 一样,但谓词值不同就不会重用cursor。 只有SQL 语句完全一样,包括谓词值,才会重用cursor。
(2)当没有statstics时,即没有histograms,这时等于force,只要SQL 语句一样,就会重用cursor。
三. similar 模式对expdp 带来的影响
原理链接如下:
http://www.linuxidc.com/Linux/2011-07/39436.htm
3.1按照默认的expdp
SYS@anqing2(rac2)> show parameter cursor_sharing
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
cursor_sharing string EXACT
[oracle@rac2 backup]$ expdp /'/ as sysdba/' directory=backup full=y dumpfile=fullexp.dmp logfile=fullexp.log
Export: Release 10.2.0.4.0 - Production on Friday, 17 June, 2011 13:54:40
...............
Job "SYS"."SYS_EXPORT_FULL_01" successfully completed at 14:05:49
测试库,全备花了11分钟。
3.2 用similar 模式 expdp 备份
cursor_sharing是一个动态参数,可以在执行expdp之前进行修改,然后执行导出, 备份完在改回来。
SYS@anqing2(rac2)> alter system set cursor_sharing=similar scope=memory;
System altered.
SYS@anqing2(rac2)> show parameter cursor_sharing
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
cursor_sharing string SIMILAR
[oracle@rac2 backup]$ expdp /'/ as sysdba/' directory=backup full=y dumpfile=fullexp.dmp logfile=fullexp.log
Export: Release 10.2.0.4.0 - Production on Friday, 17 June,2011 13:40:37
....
Job "SYS"."SYS_EXPORT_FULL_01" successfully completed at 13:52:42
备份了12分钟.
因为库不大,所以差距不明显. 不过如果库大的话,一般也不会用expdp来备份。所以这里作为一个知识点,了解一下。
