一 建立测试表
create table a nologging as select * from all_objects;
二 准备工作
找到sql_Id='aq03p7muwgvq5'
select * from V$sql where sql_text like '% from a where object_id=3%';
找到全表的outline:
方法一:dba_hist_sql_plan/v$sql_plan都可以
select extractvalue(value(d), '/hint') as outline_hints
from
xmltable('/*/outline_data/hint'
passing (
select
xmltype(other_xml) as xmlval
from
dba_hist_sql_plan
where
sql_id = '&sql_id'
and plan_hash_value=&plan_hash_value
and other_xml is not null)) d
方法二:
select * from dbms_xplan.display_awr('aq03p7muwgvq5',0,'outline');
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
DB_VERSION('11.2.0.4')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "A"@"SEL$1")
END_OUTLINE_DATA
*/
declare v_hints sys.sqlprof_attr;
v_sqltext clob;
begin
select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownumtrue,replace=>true);
end;
建立索引
create index I_ind_object_id_com on a(object_id,object_name) nologging;
查看执行计划,并没有走索引:
Execution Plan
----------------------------------------------------------
Plan hash value: 2248738933
--------------------------------------------------------------------------
|>
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 98 | 177 (1)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| A | 1 | 98 | 177 (1)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation>
---------------------------------------------------
1 - filter("OBJECT_ID"=3)
Note
-----
- SQL profile "sql_full" used for this statement
Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
1254 consistent gets
1246 physical reads
0 redo> 1606 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
删除profile
begin dbms_sqltune.drop_sql_profile('sql_full');
end;
再次执行sql,找到走索引的outline
select * from dbms_xplan.display_awr('aq03p7muwgvq5',1,'outline'); /*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
DB_VERSION('11.2.0.4')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
INDEX_RS_ASC(@"SEL$1" "A"@"SEL$1" ("A"."OBJECT_ID" "A"."OBJECT_NAME"))
END_OUTLINE_DATA
*/
declare v_hints sys.sqlprof_attr;
v_sqltext clob;
begin
select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownumtrue,replace=>true);
end;
建立更优的索引
create index I_ind_object_id on a(object_Id) nologging;
再次查看执行计划,并没有走更优的索引
Execution Plan
----------------------------------------------------------
Plan hash value: 3075844428
---------------------------------------------------------------------------------------------------
|>
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 98 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| A | 1 | 98 | 3 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_IND_OBJECT_ID_COM | 1 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------
Predicate Information (identified by operation>
--------------------------------------------------
2 - access("OBJECT_ID"=3)
Note
-----
- SQL profile "sql_comp_ind" used for this statement
Statistics
7 recursive calls 0 db block gets
10 consistent gets
1 physical reads
0 redo> 1609 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
当然你也可以用SQLT里的coe_xfr_sql_profile.sql或者create_sql_profile.sql生成sql_profile;
这里有一点比较扯的是用完整的outline,写进去不报错,但执行计划不走sql_profile里约定的内容;
declare v_hints sys.sqlprof_attr;
v_sqltext clob;
begin
select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownum
这一段里,
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com