为指定的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的用户,即我们的应用用户。
1.SQL> conn sys/Oracle@orcl as sysdba
2.已连接。
3.
4.--1.为创建outline用户赋权CREATE ANY OUTLINE
5.SQL> grant CREATE ANY OUTLINE to lau;
6.
7.授权成功。
8.
9.SQL> conn lau/lau@orcl
10.已连接。
11.
12.SQL> create table t (id int);
13.
14.表已创建。
15.
16.SQL> insert into t select level from dual connect by level <=10000;
17.
18.已创建10000行。
19.
20.SQL> commit;
21.
22.提交完成。
23.
24.SQL> set autot traceonly
25.SQL> select * from t where id=1;
26.
27.
28.执行计划
29.----------------------------------------------------------
30.Plan hash value: 1601196873
31.
32.--------------------------------------------------------------------------
33.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
34.--------------------------------------------------------------------------
35.| 0 | SELECT STATEMENT | | 1 | 13 | 6 (0)| 00:00:01 |
36.|* 1 | TABLE ACCESS FULL| T | 1 | 13 | 6 (0)| 00:00:01 |
37.--------------------------------------------------------------------------
38.
39.Predicate Information (identified by operation id):
40.---------------------------------------------------
41.
42. 1 - filter("ID"=1)
43.
44.Note
45.-----
46. - dynamic sampling used for this statement
47.
48.
49.统计信息
50.----------------------------------------------------------
51. 5 recursive calls
52. 0 db block gets
53. 48 consistent gets
54. 0 physical reads
55. 0 redo size
56. 402 bytes sent via SQL*Net to client
57. 385 bytes received via SQL*Net from client
58. 2 SQL*Net roundtrips to/from client
59. 0 sorts (memory)
60. 0 sorts (disk)
61. 1 rows processed
62.
63.--2.创建两个测试outline
64.SQL> create or replace outline test_outline1 for category cate_outline
65. 2 on select * from t where id=1;
66.
67.大纲已创建。
68.
69.SQL> create or replace outline test_outline2 for category cate_outline
70. 2 on select * from t where id=2;
71.
72.大纲已创建。
73.
74.--3.查看该用户下创建的outline。
75.SQL> col name for a20
76.SQL> col sql_text for a50
77.SQL> col used for a10
78.SQL> set autot off
79.SQL> set linesize 200
80.SQL> select name, sql_text ,used,category
81. 2 from user_outlines
82. 3 where category=upper('cate_outline');
83.
84.NAME SQL_TEXT USED CATEGORY
85.-------------------- -------------------------------------------------- ---------- ------------------------------
86.TEST_OUTLINE2 select * from t where id=2 UNUSED CATE_OUTLINE
87.TEST_OUTLINE1 select * from t where id=1 UNUSED CATE_OUTLINE
88.
89.--4.使cate_outline下的outline生效
90.SQL> alter system set USE_STORED_OUTLINES =cate_outline;
91.
92.系统已更改。
93.
94.SQL> select name, sql_text ,used,category
95. 2 from user_outlines
96. 3 where category=upper('cate_outline');
97.
98.NAME SQL_TEXT USED CATEGORY
99.-------------------- -------------------------------------------------- ---------- ------------------------------
100.TEST_OUTLINE2 select * from t where id=2 UNUSED CATE_OUTLINE
101.TEST_OUTLINE1 select * from t where id=1 UNUSED CATE_OUTLINE
102.
103.SQL> set autot explain
104.用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
105.SQL> set autot on explain
106.SQL> select * from t where id=1;
107.
108. ID
109.----------
110. 1
111.
112.
113.执行计划
114.----------------------------------------------------------
115.Plan hash value: 1601196873
116.
117.--------------------------------------------------------------------------
118.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
119.--------------------------------------------------------------------------
120.| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
121.|* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
122.--------------------------------------------------------------------------
123.
124.Predicate Information (identified by operation id):
125.---------------------------------------------------
126.
127. 1 - filter("ID"=1)
128.
129.Note
130.-----
131. - outline "TEST_OUTLINE1" used for this statement --说明已经使用了我们创建的outline.
132.
133.SQL> select * from t where id=2;
134.
135. ID
136.----------
137. 2
138.
139.
140.执行计划
141.----------------------------------------------------------
142.Plan hash value: 1601196873
143.
144.--------------------------------------------------------------------------
145.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
146.--------------------------------------------------------------------------
147.| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
148.|* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
149.--------------------------------------------------------------------------
150.
151.Predicate Information (identified by operation id):
152.---------------------------------------------------
153.
154. 1 - filter("ID"=2)
155.
156.Note
157.-----
158. - outline "TEST_OUTLINE2" used for this statement
159.
160.--以下没有使用outline,因为没有绑定变量。
161.SQL> select * from t where id=3;
162.
163. ID
164.----------
165. 3
166.
167.
168.执行计划
169.----------------------------------------------------------
170.Plan hash value: 1601196873
171.
172.--------------------------------------------------------------------------
173.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
174.--------------------------------------------------------------------------
175.| 0 | SELECT STATEMENT | | 1 | 13 | 6 (0)| 00:00:01 |
176.|* 1 | TABLE ACCESS FULL| T | 1 | 13 | 6 (0)| 00:00:01 |
177.--------------------------------------------------------------------------
178.
179.Predicate Information (identified by operation id):
180.---------------------------------------------------
181.
182. 1 - filter("ID"=3)
183.
184.Note
185.-----
186. - dynamic sampling used for this statement
187.
188.--创建索引,验证outline的使用,sql依然使用全表扫描。
189.SQL> create index ind_t_id on t(id);
190.
191.索引已创建。
192.
193.SQL> select * from t where id=1;
194.
195. ID
196.----------
197. 1
198.
199.
200.执行计划
201.----------------------------------------------------------
202.Plan hash value: 1601196873
203.
204.--------------------------------------------------------------------------
205.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
206.--------------------------------------------------------------------------
207.| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
208.|* 1 | TABLE ACCESS FULL| T | 1 | 13 | 2 (0)| 00:00:01 |
209.--------------------------------------------------------------------------
210.
211.Predicate Information (identified by operation id):
212.---------------------------------------------------
213.
214. 1 - filter("ID"=1)
215.
216.Note
217.-----
218. - outline "TEST_OUTLINE1" used for this statement
219.
220.--禁用outline之后,sql使用了索引
221.SQL> alter system set USE_STORED_OUTLINES =false;
222.
223.系统已更改。
224.
225.SQL> select * from t where id=1;
226.
227. ID
228.----------
229. 1
230.
231.
232.执行计划
233.----------------------------------------------------------
234.Plan hash value: 3343177607
235.
236.-----------------------------------------------------------------------------
237.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
238.-----------------------------------------------------------------------------
239.| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
240.|* 1 | INDEX RANGE SCAN| IND_T_ID | 1 | 13 | 1 (0)| 00:00:01 |
241.-----------------------------------------------------------------------------
242.
243.Predicate Information (identified by operation id):
244.---------------------------------------------------
245.
246. 1 - access("ID"=1)
247.
248.Note
249.-----
250. - dynamic sampling used for this statement
本篇文章来源于 Linux公社网站() 原文链接:http:///Linux/2012-07/66708.htm
outline for 绑定变量
1.获取带绑定变量sql的 child_number,hash_value
select child_number,hash_value,address,sql_text from v$sql
where sql_text like 'select * from t where id%';
2.创建outline
begin
dbms_outln.create_outline (
hash_value =>3573770389,
child_number =>0,
category =>'CATE_OUTLINE');
end;
/
1.SQL> var v_id number;
2.SQL> exec :v_id :=5;
3.
4.PL/SQL 过程已成功完成。
5.
6.提交完成。
7.SQL> set autot traceonly
8.SQL> select * from t where id=:v_id;
9.
10.
11.执行计划
12.----------------------------------------------------------
13.Plan hash value: 1601196873
14.
15.--------------------------------------------------------------------------
16.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
17.--------------------------------------------------------------------------
18.| 0 | SELECT STATEMENT | | 100 | 1300 | 7 (15)| 00:00:01 |
19.|* 1 | TABLE ACCESS FULL| T | 100 | 1300 | 7 (15)| 00:00:01 |
20.--------------------------------------------------------------------------
21.
22.Predicate Information (identified by operation id):
23.---------------------------------------------------
24.
25. 1 - filter("ID"=TO_NUMBER(:V_ID))
26.
27.Note
28.-----
29. - dynamic sampling used for this statement
30.
31.
32.统计信息
33.----------------------------------------------------------
34. 0 recursive calls
35. 0 db block gets
36. 0 consistent gets
37. 0 physical reads
38. 0 redo size
39. 0 bytes sent via SQL*Net to client
40. 0 bytes received via SQL*Net from client
41. 0 SQL*Net roundtrips to/from client
42. 0 sorts (memory)
43. 0 sorts (disk)
44. 1 rows processed
45.
46.SQL> col child_number for 999999999999
47.SQL> col hash_values for 999999999999
48.SQL> col hash_value for 999999999999
49.SQL> col address for a20
50.SQL> col sql_text for a50
51.SQL> set linesize 200
52.
53.SQL> select child_number,hash_value,address,sql_text from v$sql
54. 2 where sql_text like 'select * from t where id%';
55.
56. CHILD_NUMBER HASH_VALUE ADDRESS SQL_TEXT
57.------------- ------------- -------------------- --------------------------------------------------
58. 0 3573770389 22E58344 select * from t where id=:v_id
59.
60.SQL> begin
61. 2 dbms_outln.create_outline (
62. 3 hash_value =>3573770389,
63. 4 child_number =>0,
64. 5 category =>'CATE_OUTLINE');
65. 6 end;
66. 7 /
67.
68.PL/SQL 过程已成功完成。
69.
70.提交完成。
71.SQL> select name, sql_text ,used,category
72. 2 from user_outlines
73. 3 where category=upper('cate_outline');
74.
75.NAME SQL_TEXT USED CATEGORY
76.-------------------- -------------------------------------------------- ---------- ------------------------------
77.SYS_OUTLINE_12071817 select * from t where id=:v_id UNUSED CATE_OUTLINE
78.153216201
79.
80.SQL> alter system set USE_STORED_OUTLINES =cate_outline;
81.
82.系统已更改。
83.
84.SQL> set autot traceonly
85.
86.SQL> exec :v_id :=1;
87.
88.PL/SQL 过程已成功完成。
89.
90.提交完成。
91.SQL> select * from t where id=:v_id;
92.
93.
94.执行计划
95.----------------------------------------------------------
96.Plan hash value: 1601196873
97.
98.--------------------------------------------------------------------------
99.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
100.--------------------------------------------------------------------------
101.| 0 | SELECT STATEMENT | | 16 | 208 | 6 (0)| 00:00:01 |
102.|* 1 | TABLE ACCESS FULL| T | 16 | 208 | 6 (0)| 00:00:01 |
103.--------------------------------------------------------------------------
104.
105.Predicate Information (identified by operation id):
106.---------------------------------------------------
107.
108. 1 - filter("ID"=TO_NUMBER(:V_ID))
109.
110.Note
111.-----
112. - outline "SYS_OUTLINE_12071817153216201" used for this statement
113.
114.
115.统计信息
116.----------------------------------------------------------
117. 35 recursive calls
118. 123 db block gets
119. 44 consistent gets
120. 0 physical reads
121. 632 redo size
122. 402 bytes sent via SQL*Net to client
123. 385 bytes received via SQL*Net from client
124. 2 SQL*Net roundtrips to/from client
125. 2 sorts (memory)
126. 0 sorts (disk)
127. 1 rows processed
128.
129.SQL> exec :v_id :=10;
130.
131.PL/SQL 过程已成功完成。
132.
133.提交完成。
134.SQL> select * from t where id=:v_id;
135.
136.
137.执行计划
138.----------------------------------------------------------
139.Plan hash value: 1601196873
140.
141.--------------------------------------------------------------------------
142.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
143.--------------------------------------------------------------------------
144.| 0 | SELECT STATEMENT | | 16 | 208 | 6 (0)| 00:00:01 |
145.|* 1 | TABLE ACCESS FULL| T | 16 | 208 | 6 (0)| 00:00:01 |
146.--------------------------------------------------------------------------
147.
148.Predicate Information (identified by operation id):
149.---------------------------------------------------
150.
151. 1 - filter("ID"=TO_NUMBER(:V_ID))
152.
153.Note
154.-----
155. - outline "SYS_OUTLINE_12071817153216201" used for this statement
156.
157.
158.统计信息
159.----------------------------------------------------------
160. 0 recursive calls
161. 0 db block gets
162. 24 consistent gets
163. 0 physical reads
164. 0 redo size
165. 402 bytes sent via SQL*Net to client
166. 385 bytes received via SQL*Net from client
167. 2 SQL*Net roundtrips to/from client
168. 0 sorts (memory)
169. 0 sorts (disk)
170. 1 rows processed
171.
172.SQL> create index ind_t_id on t(id);
173.
174.索引已创建。
175.
176.SQL> comment on column t.id is 'dddd';
177.
178.注释已创建。
179.
180.SQL> select * from t where id=:v_id;
181.
182.
183.执行计划
184.----------------------------------------------------------
185.Plan hash value: 1601196873
186.
187.--------------------------------------------------------------------------
188.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
189.--------------------------------------------------------------------------
190.| 0 | SELECT STATEMENT | | 16 | 208 | 6 (0)| 00:00:01 |
191.|* 1 | TABLE ACCESS FULL| T | 16 | 208 | 6 (0)| 00:00:01 |
192.--------------------------------------------------------------------------
193.
194.Predicate Information (identified by operation id):
195.---------------------------------------------------
196.
197. 1 - filter("ID"=TO_NUMBER(:V_ID))
198.
199.Note
200.-----
201. - outline "SYS_OUTLINE_12071817153216201" used for this statement
202.
203.
204.统计信息
205.----------------------------------------------------------
206. 1 recursive calls
207. 0 db block gets
208. 24 consistent gets
209. 0 physical reads
210. 0 redo size
211. 402 bytes sent via SQL*Net to client
212. 385 bytes received via SQL*Net from client
213. 2 SQL*Net roundtrips to/from client
214. 0 sorts (memory)
215. 0 sorts (disk)
216. 1 rows processed
217.
218.SQL> alter system set use_stored_outlines=false;
219.
220.系统已更改。
221.
222.SQL> select * from t where id=:v_id;
223.
224.
225.执行计划
226.----------------------------------------------------------
227.Plan hash value: 3343177607
228.
229.-----------------------------------------------------------------------------
230.| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
231.-----------------------------------------------------------------------------
232.| 0 | SELECT STATEMENT | | 100 | 1300 | 1 (0)| 00:00:01 |
233.|* 1 | INDEX RANGE SCAN| IND_T_ID | 100 | 1300 | 1 (0)| 00:00:01 |
234.-----------------------------------------------------------------------------
235.
236.Predicate Information (identified by operation id):
237.---------------------------------------------------
238.
239. 1 - access("ID"=TO_NUMBER(:V_ID))
240.
241.Note
242.-----
243. - dynamic sampling used for this statement
244.
245.
246.统计信息
247.----------------------------------------------------------
248. 13 recursive calls
249. 0 db block gets
250. 30 consistent gets
251. 4 physical reads
252. 0 redo size
253. 402 bytes sent via SQL*Net to client
254. 385 bytes received via SQL*Net from client
255. 2 SQL*Net roundtrips to/from client
256. 0 sorts (memory)
257. 0 sorts (disk)
258. 1 rows processed
本篇文章来源于 Linux公社网站() 原文链接:http:///Linux/2012-07/66708p2.htm
USE_STORED_OUTLINES参数在实例重启后需要重新设置,有两种应对方法
1.使用登录触发器为单独的用户设置会话信息
1.SQL> conn lau/lau@oralife
2.已连接。
3.SQL> create or replace trigger tr_login
4. 2 after logon on database
5. 3 declare
6. 4 v_username varchar2(30);
7. 5 begin
8. 6 select SYS_CONTEXT('USERENV','SESSION_USER') into v_username from dual;
9. 7 if v_username = 'SCOTT' then
10. 8 execute immediate 'alter session set nls_date_format=''yyyy/mm/dd hh24:mi:ss''';
11. 9 end if;
12. 10 exception
13. 11 when others then
14. 12 null;
15. 13 end;
16. 14 /
17.
18.触发器已创建
19.
20.SQL> select sysdate from dual;
21.
22.SYSDATE
23.--------------
24.25-7月 -12
25.
26.SQL> conn scott/tiger@oralife
27.已连接。
28.SQL> select sysdate from dual;
29.
30.SYSDATE
31.-------------------
32.2012/07/25 20:26:10
本篇文章来源于 Linux公社网站() 原文链接:http:///Linux/2012-07/66708p3.htm
在登录触发器中为特定的用户添加
execute immediate 'alter session set use_stored_outlines=cate_outline';
开启类cate_outline。
2.使用启动触发器开启系统级设置
1.SQL> conn sys/Oracle@oralife as sysdba
2.已连接。
3.SQL> create or replace trigger tr_login
4. 2 after STARTUP on database
5. 3 declare
6. 4 begin
7. 5 execute immediate 'alter system set nls_date_format=''yyyy/mm/dd hh24:mi:ss''';
8. 6 exception
9. 7 when others then
10. 8 null;
11. 9 end;
12. 10 /
13.
14.触发器已创建
15.
16.SQL> select sysdate from dual;
17.
18.SYSDATE
19.--------------
20.25-7月 -12
21.
22.--重启实例后,以上的设置并没有生效,但是添加
23.--execute immediate 'alter system set use_stored_outlines=cate_outline';
24.--之后的确会开启类cate_outline,使outline生效。
25.SQL> select sysdate from dual;
26.
27.SYSDATE
28.--------------
29.25-7月 -12 --没有生效
重启实例后,以上的设置并没有生效,但是将其替换为
execute immediate 'alter system set use_stored_outlines=cate_outline';
之后的确会开启类cate_outline,使outline生效。
本篇文章来源于 Linux公社网站() 原文链接:http:///Linux/2012-07/66708p4.htm
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26613085/viewspace-1129169/ ,如需转载,请注明出处,否则将追究法律责任。