Oracle SQL调优:使用outline稳固sql执行计划
Oracle SQL调优:使用outline稳固sql执行计划
为指定的sql创建outline
USE_STORED_OUTLINES
Syntax:
USE_STORED_OUTLINES = { TRUE | FALSE | category_name }
this parameters are not initialization parameters, so you cannot set them in a pfile or spfile. However, you can set them using an ALTER SYSTEM statement.重启后需要重新设置。
lau为创建outline的用户,即我们的应用用户。
- SQL> conn sys/Oracle@orcl as sysdba
- 已连接。
- --1.为创建outline用户赋权CREATE ANY OUTLINE
- SQL> grant CREATE ANY OUTLINE to lau;
- 授权成功。
- SQL> conn lau/lau@orcl
- 已连接。
- SQL> create table t (id int);
- 表已创建。
- SQL> insert into t select level from dual connect by level <=10000;
- 已创建10000行。
- SQL> commit;
- 提交完成。
- SQL> set autot traceonly
- SQL> select * from t where id=1;
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 6 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| T | 1 | 13 | 6 (0)| 00:00:01 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=1)
- Note
- -----
- - dynamic sampling used for this statement
- 统计信息
- ----------------------------------------------------------
- 5 recursive calls
- 0 db block gets
- 48 consistent gets
- 0 physical reads
- 0 redo size
- 402 bytes sent via SQL*Net to client
- 385 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
- --2.创建两个测试outline
- SQL> create or replace outline test_outline1 for category cate_outline
- 2 on select * from t where id=1;
- 大纲已创建。
- SQL> create or replace outline test_outline2 for category cate_outline
- 2 on select * from t where id=2;
- 大纲已创建。
- --3.查看该用户下创建的outline。
- SQL> col name for a20
- SQL> col sql_text for a50
- SQL> col used for a10
- SQL> set autot off
- SQL> set linesize 200
- SQL> select name, sql_text ,used,category
- 2 from user_outlines
- 3 where category=upper('cate_outline');
- NAME SQL_TEXT USED CATEGORY
- -------------------- -------------------------------------------------- ---------- ------------------------------
- TEST_OUTLINE2 select * from t where id=2 UNUSED CATE_OUTLINE
- TEST_OUTLINE1 select * from t where id=1 UNUSED CATE_OUTLINE
- --4.使cate_outline下的outline生效
- SQL> alter system set USE_STORED_OUTLINES =cate_outline;
- 系统已更改。
- SQL> select name, sql_text ,used,category
- 2 from user_outlines
- 3 where category=upper('cate_outline');
- NAME SQL_TEXT USED CATEGORY
- -------------------- -------------------------------------------------- ---------- ------------------------------
- TEST_OUTLINE2 select * from t where id=2 UNUSED CATE_OUTLINE
- TEST_OUTLINE1 select * from t where id=1 UNUSED CATE_OUTLINE
- SQL> set autot explain
- 用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
- SQL> set autot on explain
- SQL> select * from t where id=1;
- ID
- ----------
- 1
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=1)
- Note
- -----
- - outline "TEST_OUTLINE1" used for this statement --说明已经使用了我们创建的outline.
- SQL> select * from t where id=2;
- ID
- ----------
- 2
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=2)
- Note
- -----
- - outline "TEST_OUTLINE2" used for this statement
- --以下没有使用outline,因为没有绑定变量。
- SQL> select * from t where id=3;
- ID
- ----------
- 3
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 6 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| T | 1 | 13 | 6 (0)| 00:00:01 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=3)
- Note
- -----
- - dynamic sampling used for this statement
- --创建索引,验证outline的使用,sql依然使用全表扫描。
- SQL> create index ind_t_id on t(id);
- 索引已创建。
- SQL> select * from t where id=1;
- ID
- ----------
- 1
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=1)
- Note
- -----
- - outline "TEST_OUTLINE1" used for this statement
- --禁用outline之后,sql使用了索引
- SQL> alter system set USE_STORED_OUTLINES =false;
- 系统已更改。
- SQL> select * from t where id=1;
- ID
- ----------
- 1
- 执行计划
- ----------------------------------------------------------
- Plan hash value: 3343177607
- -----------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -----------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
- |* 1 | INDEX RANGE SCAN| IND_T_ID | 1 | 13 | 1 (0)| 00:00:01 |
- -----------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - access("ID"=1)
- Note
- -----
- - dynamic sampling used for this statement
|
评论暂时关闭