如果有直方图信息,而且绑定变量窥视也开启了,这里我们很好理解,在oracle 9i和oracle 10g时,sql第一次执行会将绑定变量的值带入到sql中,然后根据直方图等统计信息来评估合理的执行计划,在同样的sql下次执行时,会直接使用之前的执行计划,这个也就是我
如果有直方图信息,而且绑定变量窥视也开启了,这里我们很好理解,在oracle 9i和oracle 10g时,sql第一次执行会将绑定变量的值带入到sql中,然后根据直方图等统计信息来评估合理的执行计划,在同样的sql下次执行时,会直接使用之前的执行计划,这个也就是我们经常所接触的绑定变量窥视。
1) 开启了绑定变量窥视,收集该列的直方图:
sql> select * from v$version;
banner
----------------------------------------------------------------
oracle database 10g enterprise edition release 10.2.0.4.0 - 64bi
pl/sql release 10.2.0.4.0 - production
core 10.2.0.4.0 production
tns for 64-bit windows: version 10.2.0.4.0 - production
nlsrtl version 10.2.0.4.0 - production
sql> alter system set _optim_peek_user_binds=true;
system altered.
sql> create table t002 as select * from dba_objects;
table created.
sql> update t002 set object_id=100 where rownum
49999 rows updated.
sql> commit;
commit complete.
sql> execute dbms_stats.gather_table_stats(ownname=>'xiaoyu',tabname=>'t002',met
hod_opt=>'for all columns size 254');
pl/sql procedure successfully completed.
sql> select num_rows,blocks from user_tables where table_name='t002';
num_rows blocks
---------- ----------
50328 712
sql> select num_distinct,density,num_nulls from user_tab_columns where table_nam
e='t002' and column_name='object_id';
num_distinct density num_nulls
------------ ---------- ----------
29 9.8243e-06 0
sql> variable x number;
sql> exec : x:=100;
pl/sql procedure successfully completed.
sql> select count(*) from t002 where object_id=:x;
count(*)
----------
49999
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id 4yqsqnawx85ty, child number 0
-------------------------------------
select count(*) from t002 where object_id=:x
plan hash value: 3014849763
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 158 (100)| |
| 1 | sort aggregate | | 1 | 4 | | |
|* 2 | table access full| t002 | 50065 | 195k| 158 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t002@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:x)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(*)[22]
30 rows selected.
sql> exec : x:=1;
pl/sql procedure successfully completed.
sql> select count(*) from t002 where object_id=:x;
count(*)
----------
0
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id 4yqsqnawx85ty, child number 0
-------------------------------------
select count(*) from t002 where object_id=:x
plan hash value: 3014849763
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 158 (100)| |
| 1 | sort aggregate | | 1 | 4 | | |
|* 2 | table access full| t002 | 50065 | 195k| 158 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t002@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:x)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(*)[22]
30 rows selected.
这里比较好理解,由于开启了绑定变量窥视,然后该列又有直方图信息,所以第一次执行时会把具体值带入,然后根据具体值的直方图信息来评估rows,这里的通过谓词过滤后估算返回的rows是50065,关于有直方图的情况下估算rows的方式,非常复杂,小鱼自己也没有过多的深究,后面有机会整理相应的文章来分享。
2)如果开启了绑定变量窥视,但是没有收集直方图:
sql> alter system set _optim_peek_user_binds=true;
system altered.
sql> execute dbms_stats.gather_table_stats(ownname=>'xiaoyu',tabname=>'t002',met
hod_opt=>'for all columns size 1');
pl/sql procedure successfully completed.
sql>variable y number;
sql>exec : y:=100
pl/sql procedure successfully completed
sql> select count(*) from t002 where object_id=:y;
count(*)
----------
49999
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------
sql_id 86ngbvm962n14, child number 0
-------------------------------------
select count(*) from t002 where object_id=:y
plan hash value: 3014849763
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time
--------------------------------------------------------------------------
| 0 | select statement | | | | 158 (100)|
| 1 | sort aggregate | | 1 | 4 | |
|* 2 | table access full| t002 | 1290 | 5160 | 158 (1)| 00:00:02
--------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t002@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:y)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(*)[22]
30 rows selected.
这里我们来算算这个rows 1290是如何估算出来的
这里介绍最简单的如何计算rows,selectivity、density的公式:(下列计算公式在该列没有直方图前提下)
小鱼之前介绍cbo优化器的基本知识里面提过一个selectivity可选择率这个概念
可选择率selectivity=释放指定谓词条件返回结果集的记录数/未施加任何谓词条件的原始结果集的记录数,可选择率越大,那么cbo估算返回的rows也越大。
那么集的势rows=selectivity*未施加任何谓词条件的原始结果集的记录数
那么这个可选择率selectivity如何计算了,在列的统计信息中num_nulls为0时,selectivity=1/num_distinct
sql> select num_distinct,num_nulls,density,num_buckets from dba_tab_columns wher
e table_name='t002' and column_name='object_id';
num_distinct num_nulls density num_buckets
------------ ---------- ---------- -----------
39 0 .025641026 1
sql> select num_rows from user_tables where table_name='t002';
num_rows
----------
50328
sql> select 1/39 from dual;
1/39
----------
.025641026
sql> select 50328*1/39 from dual;
50328*1/39
----------
1290.46154
这里我们通过统计信息发现计算而来的1290跟执行计划的rows 1290完全一致
列没有直方图,而且num_nulls为0时:
selectivity_without_null=(1/num_distinct),也就是列的统计信息中num_nulls为0时,列的选择率是1/num_distinct,此时density也是等于1/num_distinct
列没有直方图,但是num_nulls又不为0时:
selectivity_with_null=(1/num_distinct)*(num_rows-num_nulls)/(num_rows),而density还是等于1/num_distinct
对于第一点num_nulls为0,列没有直方图,selectivity选择率和density上面已经进行了验证,下面稍微扩展点,来验证下num_nulls不为0,没有直方图时,可选择率selectivity、rows和density关系
sql> update t002 set object_id=null where rownumsql> commit;
sql> execute dbms_stats.gather_table_stats(ownname=>'xiaoyu',tabname=>'t002',method_opt=>'for all columns size 1');
sql> select num_rows, --表中的记录数
2 blocks, --表中数据所占的数据块数
3 empty_blocks, --表中的空块数
4 avg_space, --数据块中平均的使用空间
5 chain_cnt, --表中行连接和行迁移的数量
6 avg_row_len --每条记录的平均长度
7 from dba_tables
8 where owner=&owner and table_name=&tabname;
enter value for owner: 'xiaoyu'
enter value for tabname: 't002'
old 8: where owner=&owner and table_name=&tabname
new 8: where owner='xiaoyu' and table_name='t002'
num_rows blocks empty_blocks avg_space chain_cnt avg_row_len
---------- ---------- ------------ ---------- ---------- -----------
50328 712 0 0 0 91
sql> select column_name,
2 num_distinct,
3 num_nulls,
4 density,
5 num_buckets,
6 low_value,
7 high_value
8 from dba_tab_col_statistics
9 where owner = &owner
10 and table_name = &tabname
11 and column_name in (&col1);
enter value for owner: 'xiaoyu'
old 9: where owner = &owner
new 9: where owner = 'xiaoyu'
enter value for tabname: 't002'
old 10: and table_name = &tabname
new 10: and table_name = 't002'
enter value for col1: 'object_id'
old 11: and column_name in (&col1)
new 11: and column_name in ('object_id')
column_name num_distinct num_nulls density num_buckets low_value high_value
------------------------------ ------------ ---------- ---------- ----------- --
---------------------------- ------------------------------
object_id 44 943 .022727273 1 c2
02 c3064a3f
sql> select count(*) from t002 where object_id=100;
count(*)
----------
49000
sql> select * from table(dbms_xplan.display_cursor(null,null));
plan_table_output
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id 3yaw02xfsf7c8, child number 0
-------------------------------------
select count(*) from t002 where object_id=100
plan hash value: 3014849763
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 158 (100)| |
| 1 | sort aggregate | | 1 | 3 | | |
|* 2 | table access full| t002 | 1122 | 3366 | 158 (1)| 00:00:02 |
---------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=100)
19 rows selected.
来算算选择率selectivity_with_null=(1/num_distinct)*(num_rows-num_nulls)/(num_rows)
sql> select (50328-943)/50328*1/44 from dual;
(50328-943)/50328*1/44
----------------------
.02230143
算算density=1/num_distinct也跟dba_tab_columns中值一样
sql> select 1/44 from dual;
1/44
----------
.022727273
根据选择率selectivity_with_null跟执行计划的预估的rows也是相符的,这个地方要注意rows是严格根据num_rows*selectivity的,而不是num_rows*density,因为在没直方图时density计算方式始终是1/num_distinct
sql> select 0.02230143* 50328 from dual;
0.02230143*50328
----------------
1122.38637
细心点我们发觉上面计算selectivity的方式可以直接简化为:
(1/num_distinct)*(num_rows-num_nulls)/num_rows,如果num_null为0, 此时(1/num_distinct)*(num_rows-num_nulls)/num_rows就直接等于1/num_distinct
3)关闭绑定变量窥视,也不收集直方图:
sql> create table t003 as select * from dba_objects;
table created.
sql> alter system set _optim_peek_user_binds=false;
system altered.
sql> update t003 set object_id=10000 where object_id
48524 rows updated.
sql> commit;
commit complete.
sql> execute dbms_stats.gather_table_stats(ownname=>'sys',tabname=>'t003',method
_opt=>'for all columns size 1');
pl/sql procedure successfully completed.
sql> select num_rows from user_tables where table_name='t003';
num_rows
----------
50325
sql> select num_distinct,density,num_nulls from user_tab_columns where table_nam
e='t003' and column_name='object_id';
num_distinct density num_nulls
------------ ---------- ----------
184 .005434783 0
sql> variable a number;
sql> exec : a:=10000;
pl/sql procedure successfully completed.
sql> select count(object_name) from t003 where object_id=:a;
count(object_name)
------------------
48524
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id dq92pjhyfrg1n, child number 0
-------------------------------------
select count(object_name) from t003 where object_id=:a
plan hash value: 3872854764
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 154 (100)| |
| 1 | sort aggregate | | 1 | 29 | | |
|* 2 | table access full| t003 | 274 | 7946 | 154 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t003@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:a)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(object_name)[22]
2 - object_name[varchar2,128]
31 rows selected.
sql> exec : a:=10;
pl/sql procedure successfully completed.
sql> select count(object_name) from t003 where object_id=:a;
count(object_name)
------------------
0
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id dq92pjhyfrg1n, child number 0
-------------------------------------
select count(object_name) from t003 where object_id=:a
plan hash value: 3872854764
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 154 (100)| |
| 1 | sort aggregate | | 1 | 29 | | |
|* 2 | table access full| t003 | 274 | 7946 | 154 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t003@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:a)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(object_name)[22]
2 - object_name[varchar2,128]
31 rows selected.
关闭绑定变量,没有直方图时:
可选择率selectivity=1/num_distinct*(num_rows-num_nulls)/num_rows
sql> select 50325*1/184 from dual;
50325*1/184
-----------
273.505435
sql> select 50325*0.005434783 from dual;
50325*0.005434783
-----------------
273.505454
特别注意点:
而且我们查看v$sql时发现,关闭绑定变量窥视并不是不共享sql,而是说sql第一次执行时不带入具体值,这个如果大家没有测试过可能会想当然的以为是关闭绑定变量窥视是不共享绑定的sql语句,其实绑定变量窥视真正含义是sql第一次执行时带入绑定具体值,而如果关闭了绑定变量窥视,则不会带入具体值,那么由于不带入具体值,直方图也不会影响selectivity计算
sql> select child_number,sql_id,sql_text from v$sql where sql_text like 'select
count(object_name) from t003 where object_id=:a%';
child_number sql_id
------------ -------------
sql_text
--------------------------------------------------------------------------------
------------------------------------------------------------
0 dq92pjhyfrg1n
select count(object_name) from t003 where object_id=:a
4)关闭绑定变量窥视,收集直方图:
sql> alter system set _optim_peek_user_binds=false;
system altered.
sql> execute dbms_stats.gather_table_stats(ownname=>'sys',tabname=>'t003',method
_opt=>'for all columns size 254');
pl/sql procedure successfully completed.
sql> select num_rows from user_tables where table_name='t003';
num_rows
----------
50325
sql> select num_distinct,density,num_nulls from user_tab_columns where table_nam
e='t003' and column_name='object_id';
num_distinct density num_nulls
------------ ---------- ----------
197 .000010034 0
sql> variable b number;
sql> exec : b:=10000;
pl/sql procedure successfully completed.
sql> select count(object_name) from t003 where object_id=:b;
count(object_name)
------------------
48524
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id 9qunh3ms4kjzw, child number 0
-------------------------------------
select count(object_name) from t003 where object_id=:b
plan hash value: 3872854764
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 154 (100)| |
| 1 | sort aggregate | | 1 | 29 | | |
|* 2 | table access full| t003 | 255 | 7395 | 154 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t003@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:b)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(object_name)[22]
2 - object_name[varchar2,128]
31 rows selected.
sql> exec : b:=10;
pl/sql procedure successfully completed.
sql> select count(object_name) from t003 where object_id=:b;
count(object_name)
------------------
0
sql> select * from table(dbms_xplan.display_cursor(null,null,'all'));
plan_table_output
--------------------------------------------------------------------------------
------------------------------------------------------------
sql_id 9qunh3ms4kjzw, child number 0
-------------------------------------
select count(object_name) from t003 where object_id=:b
plan hash value: 3872854764
---------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
---------------------------------------------------------------------------
| 0 | select statement | | | | 154 (100)| |
| 1 | sort aggregate | | 1 | 29 | | |
|* 2 | table access full| t003 | 255 | 7395 | 154 (1)| 00:00:02 |
---------------------------------------------------------------------------
query block name / object alias (identified by operation id):
-------------------------------------------------------------
1 - sel$1
2 - sel$1 / t003@sel$1
predicate information (identified by operation id):
---------------------------------------------------
2 - filter(object_id=:b)
column projection information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) count(object_name)[22]
2 - object_name[varchar2,128]
31 rows selected.
如果有直方图,但是关闭绑定变量窥视,由于无法把绑定变量的值带入sql语句中,此时selectivity计算方式还是1/num_distinct*(num_rows-num_nulls)/num_rows
sql> select 50325*1/197 from dual;
50325*1/197
-----------
255.456853
在关闭绑定变量窥视后,sql语句还是会软解析,只是绑定变量的sql语句第一次执行时无法带入到sql语句中,selectivity计算无法应用到直方图信息,所以此时有无直方图对于这类绑定变量的sql语句没有影响。
关于有直方图时,字段的可选择、density等如何计算,小鱼后续会整理文档来分享。
原文地址:绑定变量窥视是否开启和直方图关系, 感谢原作者分享。