1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
| SQL> @/tmp/setup.sql
Connected.
SQL> DROP TABLE tab1 purge;
TABLE dropped.
SQL> CREATE TABLE tab1 (a INT,
2 b varchar2(20),
3 padding varchar2(200))
4 tablespace USERS;
TABLE created.
SQL>
SQL> DROP SEQUENCE myid;
SEQUENCE dropped.
SQL> CREATE SEQUENCE myid cache 1000;
SEQUENCE created.
SQL>
SQL> BEGIN
2 FOR i IN 1..100000
3 loop
4 INSERT INTO tab1 VALUES (myid.nextval,
5 'AAAAAAAAAA',
6 rpad('*',200,'*'));
7 END loop;
8
9 FOR i IN 1..1000
10 loop
11 INSERT INTO tab1 VALUES (myid.nextval,
12 'BBBBBBBBBB',
13 rpad('*',200,'*'));
14 END loop;
15
16 FOR i IN 1..1000
17 loop
18 INSERT INTO tab1 VALUES (myid.nextval,
19 'CCCCCCCCCC',
20 rpad('*',200,'*'));
21 END loop;
22
23 FOR i IN 1..100
24 loop
25 INSERT INTO tab1 VALUES (myid.nextval,
26 'DDDDDDDDDD',
27 rpad('*',200,'*'));
28 END loop;
29
30 FOR i IN 1..10
31 loop
32 INSERT INTO tab1 VALUES (myid.nextval,
33 'EEEEEEEEEE',
34 rpad('*',200,'*'));
35 END loop;
36
37 INSERT INTO tab1 VALUES (myid.nextval,
38 'FFFFFFFFFF',
39 rpad('*',200,'*'));
40
41 commit;
42 END;
43 /
PL/SQL PROCEDURE successfully completed.
SQL>
SQL> CREATE INDEX tab1_idx1 ON tab1 (b);
INDEX created.
SQL>
SQL> BEGIN dbms_stats.gather_table_stats(ownname => 'MDECKER',
2 tabname => 'TAB1',
3 method_opt =>'FOR ALL COLUMNS SIZE 254',
4 cascade=> TRUE,
5 estimate_percent => 100,
6 no_invalidate => FALSE);
7 END;
8 /
PL/SQL PROCEDURE successfully completed.
SQL>
SQL> SET LINES 300
SQL> col TABLE_NAME format a10
SQL> col owner format a8
SQL> SELECT owner, TABLE_NAME,num_rows, blocks, round((blocks*8192)/1024/1024) AS MB
2 FROM dba_tables WHERE TABLE_NAME = 'TAB1' AND owner = 'MDECKER';
OWNER TABLE_NAME NUM_ROWS BLOCKS MB
-------- ---------- ---------- ---------- ----------
MDECKER TAB1 102111 3268 26
SQL>
SQL> prompt "Press Enter to continue"
"Press Enter to continue"
SQL> pause
SQL>
SQL> /*
SQL> OWNER TABLE_NAME NUM_ROWS BLOCKS MB
SQL> -------- ---------- ---------- ---------- ----------
SQL> MDECKER TAB1 102111 3268 26
SQL>
SQL> */
SQL>
SQL>
SQL>
SQL> SELECT b , COUNT(*) FROM mdecker.tab1 GROUP BY b ORDER BY 1 DESC;
B COUNT(*)
-------------------- ----------
FFFFFFFFFF 1
EEEEEEEEEE 10
DDDDDDDDDD 100
CCCCCCCCCC 1000
BBBBBBBBBB 1000
AAAAAAAAAA 100000
6 ROWS selected.
SQL>
SQL> /*
SQL> B COUNT(*)
SQL> -------------------- ----------
SQL> FFFFFFFFFF 1
SQL> EEEEEEEEEE 10
SQL> DDDDDDDDDD 100
SQL> CCCCCCCCCC 1000
SQL> BBBBBBBBBB 1000
SQL> AAAAAAAAAA 100000
SQL>
SQL> 6 rows selected.
SQL>
SQL> */
SQL>
SQL> prompt "Press Enter to continue"
"Press Enter to continue"
SQL> pause
SQL> SELECT num_buckets, histogram FROM dba_tab_col_statistics WHERE TABLE_NAME = 'TAB1' AND column_name = 'B';
NUM_BUCKETS HISTOGRAM
----------- ---------------
6 FREQUENCY
SQL>
SQL> /*
SQL> NUM_BUCKETS HISTOGRAM
SQL> ----------- ---------------
SQL> 6 FREQUENCY
SQL> */
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> SHOW USER
USER IS "MDECKER"
SQL> @/tmp/sql1.txt
SQL> SET LINES 300
SQL> SET echo ON
SQL> SET pages 10000
SQL> col bind format a120
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL>
SQL> variable myvar varchar2(20)
SQL> EXEC :myvar := 'AAAAAAAAAA';
PL/SQL PROCEDURE successfully completed.
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 0
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 2211052296
--------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 889 (100)| |
|* 1 | TABLE ACCESS FULL| TAB1 | 100K| 20M| 889 (1)| 00:00:11 |
--------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
1 - FILTER("B"=:MYVAR)
18 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
SQL>
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL> EXEC :myvar := 'BBBBBBBBBB';
PL/SQL PROCEDURE successfully completed.
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 0
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 2211052296
--------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 889 (100)| |
|* 1 | TABLE ACCESS FULL| TAB1 | 100K| 20M| 889 (1)| 00:00:11 |
--------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
1 - FILTER("B"=:MYVAR)
18 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
SQL>
SQL> EXEC :myvar := 'CCCCCCCCCC';
PL/SQL PROCEDURE successfully completed.
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 1
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 1918680838
-----------------------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 36 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TAB1 | 1000 | 210K| 36 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TAB1_IDX1 | 1000 | | 4 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
2 - access("B"=:MYVAR)
19 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
fhtgpcv2m2j4s N Y Y Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'CCCCCCCCCC', ANYDATA())) 1918680838 1
SQL>
SQL> EXEC :myvar := 'DDDDDDDDDD';
PL/SQL PROCEDURE successfully completed.
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 2
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 1918680838
-----------------------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 5 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TAB1 | 100 | 21600 | 5 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TAB1_IDX1 | 100 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
2 - access("B"=:MYVAR)
19 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'CCCCCCCCCC', ANYDATA())) 1918680838 1
fhtgpcv2m2j4s N Y Y Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'DDDDDDDDDD', ANYDATA())) 1918680838 2
SQL>
SQL> EXEC :myvar := 'EEEEEEEEEE';
PL/SQL PROCEDURE successfully completed.
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 3
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 1918680838
-----------------------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TAB1 | 10 | 2160 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TAB1_IDX1 | 10 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
2 - access("B"=:MYVAR)
19 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'CCCCCCCCCC', ANYDATA())) 1918680838 1
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'DDDDDDDDDD', ANYDATA())) 1918680838 2
fhtgpcv2m2j4s N Y Y Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'EEEEEEEEEE', ANYDATA())) 1918680838 3
SQL>
SQL> EXEC :myvar := 'FFFFFFFFFF';
PL/SQL PROCEDURE successfully completed.
SQL> prompt "Press enter to run"
"Press enter to run"
SQL> pause
SQL> SET termout off
SQL> SELECT * FROM TABLE(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
SQL_ID fhtgpcv2m2j4s, child NUMBER 4
-------------------------------------
SELECT /* MD_TESTCASE */ * FROM tab1 WHERE b = :myvar
Plan hash VALUE: 1918680838
-----------------------------------------------------------------------------------------
| Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TAB1 | 1 | 216 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TAB1_IDX1 | 1 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (IDENTIFIED BY operation id):
---------------------------------------------------
2 - access("B"=:MYVAR)
19 ROWS selected.
SQL> SELECT sql_id, is_obsolete, is_bind_sensitive, is_bind_aware, is_shareable, dbms_sqltune.extract_binds(bind_data) bind, plan_hash_value, child_number FROM
2 v$sql WHERE sql_id = 'fhtgpcv2m2j4s';
SQL_ID I I I I BIND(NAME, POSITION, DUP_POSITION, DATATYPE, DATATYPE_STRING, CHARACTER_SID, PRECISION, SCALE, MAX_LENGTH, LAST_CAPTURED PLAN_HASH_VALUE CHILD_NUMBER
------------- - - - - ------------------------------------------------------------------------------------------------------------------------ --------------- ------------
fhtgpcv2m2j4s N Y N Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'AAAAAAAAAA', ANYDATA())) 2211052296 0
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'CCCCCCCCCC', ANYDATA())) 1918680838 1
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'DDDDDDDDDD', ANYDATA())) 1918680838 2
fhtgpcv2m2j4s N Y Y N SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'EEEEEEEEEE', ANYDATA())) 1918680838 3
fhtgpcv2m2j4s N Y Y Y SQL_BIND_SET(SQL_BIND(NULL, 1, NULL, 1, 'VARCHAR2(32)', 178, NULL, NULL, 32, '12-MAR-09', 'FFFFFFFFFF', ANYDATA())) 1918680838 4 |