绑定变量窥探和直方图
来源:互联网 发布:oracle导出数据库脚本 编辑:程序博客网 时间:2024/05/15 17:52
一、绑定变量的窥探(peek)1、Oracle在处理带有绑定变量的SQL时候,只会在硬解析的时候才会“窥探”一下SQL中绑定变量的值,然后会根据窥探到的值来决定整个SQL的执行计划。参数:_optim_peek_user_binds2、绑定变量窥探的案例create table t8(id int ,name varchar2(100));begin for i in 1 .. 1000 loop insert into t8 values (i, 'a' || i); end loop;end; create index t_idx on t8(id); exec dbms_stats.gather_table_stats(user,'T8',cascade=>true); select id,count(*) from t8 group by id; variable n number; exec :n := 1; select count(*) from t8 where id = :n; select * from table(dbms_xplan.display_cursor);PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------SQL_ID7zy48bjbwdjff, child number 0-------------------------------------select count(*) from t8 where id = :nPlan hash value: 293504097---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | |1 (100)| || 1 | SORT AGGREGATE | |1 |4 | | |PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------|* 2 | INDEX RANGE SCAN| T_IDX |1 |4 |1 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=:N)19 rows selected. --再插入值beginfor i in 1 .. 10000 loop insert into t8 values(1,'a'||i); end loop; commit;end;exec dbms_stats.gather_table_stats(user,'T8',cascade=>true);variable n number;exec :n := 1;select count(*) from t8 where id = :n;SQL> select * from table(dbms_xplan.display_cursor);PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------SQL_ID7zy48bjbwdjff, child number 0-------------------------------------select count(*) from t8 where id = :nPlan hash value: 293504097---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | |1 (100)| || 1 | SORT AGGREGATE | |1 |4 | | |PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------|* 2 | INDEX RANGE SCAN| T_IDX |1 |4 |1 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=:N)19 rows selected.不管以后数据量是什么情况,只按第一次硬解析的执行计划run SQL二、直方图1、作用:当某列数据分布不均衡,为了让CBO能生成最佳的执行计划,我们可能需要对表收集直方图,直方图最大的桶数(Bucket)是254。2、两种直方图 (1)频率直方图,当列中Distinct_keys小于254,Oracle就会自动的创建频率直方图,并且桶数(BUCKET)等于Distinct_Keys (2)高度平衡直方图,当列中Distinct_keys大于254,Oracle就会自动的创建高度平衡直方图 (3)生成直方图BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname=> 'HR', tabname => 'T8', estimate_percent => 100, method_opt => 'for all columns size skewonly', no_invalidate => FALSE, degree => 1, cascade => TRUE);END;method_opt => 'for all columns size skewonly',---正式的生产环境中,最好别用allcolumns方式收集直方图,因为all columns 几乎会对所有列都收集直方图信息,但是有些列并不会出现在where条件中,我们去收集并不会出现在where条件中的列就浪费了资源。三、绑定变量窥视bind peek与直方图相互作用 1、收集直方图并 不绑定变量 采用hard coding 硬编码 select * from v$version; select id,count(*) from t8 group by id;BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname=> 'GYJ', tabname => 'T8', estimate_percent => 100, method_opt => 'for all columns size skewonly', no_invalidate => FALSE, degree => 1, cascade => TRUE);END;/SQL> select count(*) from t8 where id =1;Execution Plan----------------------------------------------------------Plan hash value: 2157757785---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | |1 |4 |9 (0)| 00:00:01 || 1 | SORT AGGREGATE | |1 |4 | | ||* 2 | TABLE ACCESS FULL| T8 | 10004 | 40016 |9 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=1)Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 31 consistent gets 0 physical reads 0 redo size424 bytes sent via SQL*Net to client419 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedSQL> select count(*) from t8 where id =2;Execution Plan----------------------------------------------------------Plan hash value: 293504097---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | |1 |4 |1 (0)| 00:00:01 || 1 | SORT AGGREGATE | |1 |4 | | ||* 2 | INDEX RANGE SCAN| T_IDX |1 |4 |1 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=2)Statistics---------------------------------------------------------- 1 recursive calls 0 db block gets 2 consistent gets 0 physical reads 0 redo size422 bytes sent via SQL*Net to client419 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed不适用用绑定变量硬解析依据直方图可以获得很好的基数(cardinality)
0 0
- 绑定变量窥探和直方图
- 绑定变量窥探和直方图
- 绑定变量窥探和直方图
- 直方图;绑定变量窥探;自适应游标
- Oracle 绑定变量窥探
- 绑定变量窥探
- Oracle 绑定变量窥探
- Oracle_绑定变量窥探测试
- 绑定变量与直方图
- 绑定变量窥探(bind peeking)--什么使执行计划不准
- 《基于ORACLE的SQL优化读书》笔记 绑定变量窥探
- 绑定变量窥探(bind peeking)--什么使执行计划不准
- 直方图对绑定变量sql的影响
- 一个执行计划异常变更的案例 - 外传之绑定变量窥探
- oracle 11.2.0.1告警日志报错ORA-03137与绑定变量窥探BUG9703463
- 变量绑定和sql_trace
- 绑定变量和cursor_sharing
- DEFINES和绑定变量
- 给同校大一IT新生的建议
- IOS 推送消息 php做推送服务端
- 学校公文办公处理系统_基于ASP.NET和Swfupload、FlashPaper2.2、校讯通短信发送的开发
- DevExpress ChartControl 多数据时滚动条处理
- NSString 常用用法
- 绑定变量窥探和直方图
- 淘宝2012笔试
- C# 的Brush 及相关颜色的操作
- 黑马程序员:面向对象设计练习
- string深刻认识
- linux shell编程指南第二十一章------创建屏幕输出
- 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
- 严格模式与混杂模式-如何触发这两种模式,区分它们有何意义
- FreeMarker教程