/
02_bind_peeking_nested_loops.sql
126 lines (108 loc) · 4.21 KB
/
02_bind_peeking_nested_loops.sql
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
-- Copyright 2018 Tanel Poder. All rights reserved. More info at http://tanelpoder.com
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.
--------------------------------------------------------------------------------
--
-- File name: demos/bind_peeking_nested_loops.sql
-- Purpose: this script demos how a "wrong" bind variable value
-- can cause an execution plan to be compiled which is
-- very inefficient for the next execution with different bind variable
-- values (with large number of matching rows)
-- the second execution of the query takes very long time to complete
-- despite adaptive bind variable peeking, which would kick in during the
-- next (3rd) execution
--
-- This problem happens even on Oracle 11.2 despite adaptive bind peeking
-- and cardinality feedback (due to design, not a bug)
--
-- Author: Tanel Poder (tanel@e2sn.com)
-- Copyright: (c) http://tech.e2sn.com
--
--------------------------------------------------------------------------------
alter session set optimizer_use_sql_plan_baselines = false;
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
DROP TABLE t4;
DROP TABLE t5;
CREATE TABLE t1 AS SELECT * FROM dba_objects WHERE rownum <= 50000;
CREATE TABLE t2 AS SELECT * FROM dba_objects WHERE rownum <= 10000;
CREATE TABLE t3 AS SELECT * FROM dba_objects WHERE rownum <= 10000;
CREATE TABLE t4 AS SELECT * FROM dba_objects WHERE rownum <= 10000;
CREATE TABLE t5 AS SELECT * FROM dba_objects WHERE rownum <= 10000;
CREATE INDEX i1 ON t1(owner);
CREATE INDEX i2 ON t2(owner);
CREATE INDEX i3 ON t3(owner);
CREATE INDEX i4 ON t4(owner);
CREATE INDEX i5 ON t5(owner);
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T1',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 254');
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T2',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 254');
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T3',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 254');
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T4',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 254');
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T5',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 254');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T1',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 1');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T2',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 1');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T3',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 1');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T4',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 1');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'T5',cascade=>TRUE, method_opt=>'FOR ALL INDEXED COLUMNS SIZE 1');
-- this hack might be needed to "help" this problem to show up sometimes:
-- ALTER SESSION SET OPTIMIZER_INDEX_COST_ADJ=10;
VAR v1 VARCHAR2(100)
VAR v2 VARCHAR2(100)
VAR v3 VARCHAR2(100)
VAR v4 VARCHAR2(100)
VAR v5 VARCHAR2(100)
EXEC :v1:='SCOTT'
EXEC :v2:='SCOTT'
EXEC :v3:='SCOTT'
EXEC :v4:='SCOTT'
EXEC :v5:='SCOTT'
SET TIMING ON
PROMPT Running query first time, this should be fast (and should use nested loops execution plan)
SELECT
MIN(t1.created), MAX(t1.created)
FROM
t1
, t2
, t3
, t4
, t5
WHERE
t1.object_id = t2.object_id
AND t2.object_id = t3.object_id
AND t3.object_id = t4.object_id
AND t4.object_id = t5.object_id
AND t1.owner = :v1
AND t2.owner = :v2
AND t3.owner = :v3
AND t4.owner = :v4
AND t5.owner = :v5
/
SET TIMING OFF
--SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null,'ALLSTATS LAST ADVANCED'));
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null));
EXEC :v1:='SYS'
EXEC :v2:='SYS'
EXEC :v3:='SYS'
EXEC :v4:='SYS'
EXEC :v5:='SYS'
SET TIMING ON
PROMPT Now running the same query with different bind variables (this query should take very long time)
SELECT
MIN(t1.created), MAX(t1.created)
FROM
t1
, t2
, t3
, t4
, t5
WHERE
t1.object_id = t2.object_id
AND t2.object_id = t3.object_id
AND t3.object_id = t4.object_id
AND t4.object_id = t5.object_id
AND t1.owner = :v1
AND t2.owner = :v2
AND t3.owner = :v3
AND t4.owner = :v4
AND t5.owner = :v5
/