创建测试表
create table test as select * from dba_objects;
Table created.
创建索引
SQL&get; create index t_idx on test(object_id);
Index created.
收集统计信息
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(ownname =&get; 'SCOTT',
tabname =&get; 'TEST',
estimate_percent =&get; 100,
method_opt =&get; 'for all columns size skewonly',
no_invalidate =&get; FALSE,
degree =&get; 8,
cascade =&get; TRUE);
END;
/
explain plan for select * from test t where object_id=1000; ----本来应该走索引
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ADVANCED -PROJECTION'));
explain plan for select /*+ full(t)*/ * from test t where object_id=1000; ----加个HINT走全表
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ADVANCED -PROJECTION'));
SQL&get; SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ADVANCED -PROJECTION'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 97 | 290 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 97 | 290 (1)| 00:00:04 |
--------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
FULL(@"SEL$1" "T"@"SEL$1") ----这个就是HINT部分
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
DB_VERSION('11.2.0.1')
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=1000)
32 rows selected.
declare
v_hints sys.sqlprof_attr;
begin
v_hints := sys.sqlprof_attr('FULL(@"SEL$1" "T"@"SEL$1")'); ---输入HINT部分
dbms_sqltune.import_sql_profile('select * from test t where object_id=1000', ---输入SQL
v_hints,
'XX', ---SQL PROFILE 名字,随便取
force_match =&get; true);
end;
/
exec dbms_sqltune.drop_sql_profile('XX'); ----删除SQL PROFILE
验证SQL PROFILE是否成功
SQL&get; select * from test t where object_id=1000;
Execution Plan
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 97 | 290 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 97 | 290 (1)| 00:00:04 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=1000)
Note
-----
- SQL profile "XX" used for this statement
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1039 consistent gets
0 physical reads
0 redo size
1607 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed