SlideShare a Scribd company logo
24/06/2019
1
a good background in ...
1
2
24/06/2019
2
... SQL processing, aka
... successful application development
3
4
24/06/2019
3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Connor McDonald
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
6
5
6
24/06/2019
4
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
7
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
8
7
8
24/06/2019
5
Getting in touch is easy...
connor-mcdonald.com
https://linktr.ee/connor
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
10https://asktom.oracle.com
9
10
24/06/2019
6
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
1
https://asktom.oracle.com/officehours
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
200 hours free access so far
12
11
12
24/06/2019
7
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
assumptions
1. You roughly know databases (tables and columns)
2. You can write a SQL statement
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
before we begin
13
14
24/06/2019
8
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
recommended reading
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
docs.oracle.com
15
16
24/06/2019
9
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
its free !
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Concepts guide
http://bit.ly/oraconcepts
17
18
24/06/2019
10
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better than lots of current people
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Application Developers guide
http://bit.ly/oradevdoc
19
20
24/06/2019
11
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better than lots of current people
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL is like any language
22
21
22
24/06/2019
12
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile it
23
on the fly
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
run it
24
23
24
24/06/2019
13
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
get | store results
25
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
for successful applications...
26
25
26
24/06/2019
14
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile it ... fast
27
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
run it ... fast
28
27
28
24/06/2019
15
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
get | store results ... fast
29
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
that's it!
30
29
30
24/06/2019
16
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
this session
31
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile
execute
store/fetch results
32
31
32
24/06/2019
17
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
before we begin ...
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
some controversy to start
33
34
24/06/2019
18
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
here's the problem...
35
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... it's us
35
36
24/06/2019
19
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a small digression
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a little bit of history on servers ...
37
38
24/06/2019
20
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(not too) long ago
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
39
40
24/06/2019
21
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
FSB = "front side bus"
41
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
any access to ... anything
41
42
24/06/2019
22
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
CPU capped
43
44
24/06/2019
23
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
hypertransport
45
46
24/06/2019
24
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
memory access direct from CPU
47
47
48
24/06/2019
25
~2007
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"why do I care?"
49
50
24/06/2019
26
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
you can't blame the server (anymore)
51
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
if you can't get that performance ...
51
52
24/06/2019
27
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... it's you
53
54
24/06/2019
28
55
55
56
24/06/2019
29
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all have their place...
57
58
24/06/2019
30
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"The sooner your company admits that you are not
Google, the sooner you can get down to some real work"
- Ted Dziuba
59
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
so what's holding you back ?
59
60
24/06/2019
31
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
It all comes down to...
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
too much work or ...
61
62
24/06/2019
32
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... not being able to do work
63
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
PART #1
64
compiling SQL
63
64
24/06/2019
33
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... how to avoid work when processing SQL
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
terminology
66
65
66
24/06/2019
34
cursors
declare
cursor C(p number) is
select * from DEPT
where DEPTNO = p;
begin
for rec in C loop
…
end loop;
end;
select *
from EMPLOYEE
where EMPNO > 1234;
delete from MY_TABLE;
drop table MY_TABLE;
begin
MY_PROCEDURE(1,2,3);
end;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all of them !
67
68
24/06/2019
35
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
3 phases
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
open
process
close
gimme some memory
do some stuff using that memory,
maybe access other memory
here's your memory back
70
69
70
24/06/2019
36
memory access = controlled access
a quick primer on (database) memory
71
72
24/06/2019
37
74
metaphor
73
74
24/06/2019
38
75
75
76
24/06/2019
39
77
78
24/06/2019
40
79
limited resource
lots of people want it
concurrent access causes problems
it's a complex system
79
80
24/06/2019
41
81
same with memory
SGA
81
82
24/06/2019
42
SGA
protected by
SGA
protected by
1) get latch
83
84
24/06/2019
43
SGA
protected by
2) access memory
SGA
protected by
3) release latch
85
86
24/06/2019
44
latch contention
SGA
protected by
someone must wait ...
87
88
24/06/2019
45
active wait
spinning
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
can I have the latch ?
90
89
90
24/06/2019
46
91
92
24/06/2019
47
latch contention....
hurts CPU...
94
93
94
24/06/2019
48
hurts concurrency
YOU
95
96
24/06/2019
49
GET
NOTHING
97
98
24/06/2019
50
DONE
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
its importance to Oracle
99
100
24/06/2019
51
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
early Oracle
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
2000 times !
(_spin_count)
102
101
102
24/06/2019
52
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Zzzzzzzzzz....
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
nowadays...
yield
mutex
posting
Good reading: http://andreynikolaev.wordpress.com/
CAS
103
104
24/06/2019
53
"Errrr.... weren't we talking SQL?"
to run a SQL statement
105
106
24/06/2019
54
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
SQL> select *
2 frmo emp;
frmo emp
*
ERROR at line 2:
ORA-00923: FROM keyword not found where expected
107
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
SQL> select empnoo
2 from emp;
select empnoo
*
ERROR at line 1:
ORA-00904: invalid column name
107
108
24/06/2019
55
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
PLAN
-------------------------------------
SELECT STATEMENT
TABLE ACCESS BY INDEX ROWID EMP
INDEX RANGE SCAN EMP_PK
EMP_PK EMP
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
109
110
24/06/2019
56
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
111
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
111
112
24/06/2019
57
lots of preliminaries
parsing
114
"compile"
113
114
24/06/2019
58
lots of memory access
115
lots of latching !
116
115
116
24/06/2019
59
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
library cache
117
118
24/06/2019
60
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
previously executed statements
119
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement
already in library cache ?
reuse optimizer info
reuse row source info
select * from emp where empno = 123
syntactically, semantics OK
119
120
24/06/2019
61
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
select surname, firstname from emp where empno = 123
select * from dept where deptno = 4567
select * from customer where locality = 17
select prno from property where reference = 'X123G'
select * from dept where deptno = 23
select surname, firstname from emp where empno = 123
select * from customer where locality = 256
select * from organisation where officer = 'OFF876'
select surname, firstname from emp where empno = 7632
select * from dept where deptno = 4567
select offender from crimes where crno = 247462
Two full parses avoided
library
cache
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
select surname, firstname from emp where empno = 123
select * from dept where dname = 'SALES'
probability of reuse low ?
121
122
24/06/2019
62
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
some queries are “nearly the same”
123
select surname, firstname from emp where empno = 123
select surname, firstname from emp where empno = 456
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
binding
parse this...
now run it with ? = 123
now run it with ? = 456
select surname, firstname from emp where empno = ?
123
124
24/06/2019
63
select surname, firstname from emp where empno = ?
select * from dept where deptno = ?
select * from customer where locality = ?
select prno from property where reference = ?
select * from dept where deptno = ?
select surname, firstname from emp where empno = ?
select * from customer where locality = ?
select * from organisation where officer = ?
select surname, firstname from emp where empno = ?
select * from dept where deptno = ?
select offender from crimes where crno = ?
Five full parses avoided
library
cache
demo
ParseDemo
ParseDemoBind
125
126
24/06/2019
64
"performance still looks ok"
let's make it real
ParseDemo2 nn
ParseDemo2Bind nn
127
128
24/06/2019
65
much more serious
select pk from parse_demo where pk = 17
select pk from parse_demo where pk = 123
select pk from parse_demo where pk = 43
select pk from parse_demo where pk = 33
select pk from parse_demo where pk = 127
select pk from parse_demo where pk = 5432
select pk from parse_demo where pk = 12
select pk from parse_demo where pk = 876
select pk from parse_demo where pk = 76
select pk from parse_demo where pk = 19
select pk from parse_demo where pk = 213
select pk from parse_demo where pk = 543
select pk from parse_demo where pk = 4532
select pk from parse_demo where pk = 7544
select pk from parse_demo where pk = 6543
select pk from parse_demo where pk = 452
129
130
24/06/2019
66
building SQL by concatenation
you'll get hacked
131
132
24/06/2019
67
select ename
from emp
where empno = 6543
select ename
from emp
where empno = 6543
and 1=0
union all
select table_name
from all_tables
where table_name like '%SECURITY%'
select ename
from emp
where empno = 6543
and 1=0
union all
select username
from app_security
where ...
134
133
134
24/06/2019
68
135
136
24/06/2019
69
it takes 5 minutes to hack you
for fast, secure SQL ...
137
138
24/06/2019
70
... always bind user input
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
while we are talking binding
139
140
24/06/2019
71
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement
already in library cache ?
reuse optimizer info
reuse row source info
select * from emp where empno = 123
syntactically, semantics OK
Yes!
Yes!
???
???
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
still some parsing to do
142
141
142
24/06/2019
72
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
Hard
Parse
Soft
Parse
foundnot found
in lib cache
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
scott@mydb> select *
2 from EMP
3 where EMPNO = :b1
mike@mydb> select *
2 from EMP
3 where EMPNO = :b1
SQL> desc mike.emp
Name Null? Type
----------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
143
144
24/06/2019
73
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(even soft) parsing =
memory access =
latching
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
can we do better ?
145
146
24/06/2019
74
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
recall
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
open
process
close
gimme some memory
do some stuff using that memory,
probably access other memory
here's your memory back
148
147
148
24/06/2019
75
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your SQL is always shareable
library cache
select *
from emp
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your cursor points to it
library cache
select *
from emp
cursor
150
149
150
24/06/2019
76
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
close cursor = lose pointer
library cache
select *
from emp
cursor
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
next time we parse
soft parse
151
152
24/06/2019
77
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
153
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
what if we don't close the cursor
153
154
24/06/2019
78
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
emplist(String[] args) {
if "first time I'm using this" {
else
"reset the pointer, re-execute"
}
library cache
select *
from emp
cursor 1
select *
from dept
cursor 2
select *
from emp
where ...
cursor 3
pstmt_all_emp =
con.prepareStatement("select * from emp");
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
don’t close the cursorhang on to the memoryremember what cursors we’ve usedreuse whenever possibledon’t exhaust memorydon’t allow invalid cursor reuse
155
156
24/06/2019
79
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
sounds complicated ...
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a free solution exists
157
158
24/06/2019
80
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
plsql
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
create procedure MY_PROC is
begin
select empno, ename, ...
into …
from emp
where empno = my_plsql_variable
...
end;
159
160
24/06/2019
81
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
static SQL in stored PLSQL
automatically uses bind variables
hold’s cursors open
(even if you close)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better cursor caching
161
162
24/06/2019
82
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
procedure MY_PROC is
cursor c_emp is
select * from emp;
begin
open c_emp;
fetch c_emp
into …;
close c_emp;
end;
select * from emp
procedure MY_PROC is
begin
select *
into …
from dept
where …
end;
select * from dept …
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
164
PART #2
get | store results
we'll come back to running SQL
163
164
24/06/2019
83
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
165
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
start with reading data
166
165
166
24/06/2019
84
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
querying data
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
data is stored in blocks | pages
167
168
24/06/2019
85
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
to read data, you read blocks...
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> select *
2 from EMP
3 where ...
/u01/my_data1.dat
memory
169
170
24/06/2019
86
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
but (database) memory access is complex
171
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> select *
2 from EMP
3 where …
SQL> update EMP
2 set …
3 where …
SQL> select *
2 from EMP
3 where …
SQL> insert into
2 EMP
3 values (…)
SQL> select *
2 from CUSTOMER
3 /
SQL> select max(EMP)
2 from DEPT
SQL> delete
2 from DEPT
3 /
171
172
24/06/2019
87
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
we have to latch !
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
• Get latch
• Search along list of blocks in memory
• Read block
• Extract row of interest
• Release latch
• Give row back to client
"fetch a row"
174
173
174
24/06/2019
88
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
lots of rows
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
lots of latching
175
176
24/06/2019
89
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
typical program
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
rs = stmt.executeQuery("...");
while(rs.next()) {
v1 = rs.getInt(1);
v2 = rs.getString(2);
...
}
• Get latch
• Walk along list
• Get block
• Extract single row
• Release latch
• Give row back to client
• Get latch (again)
• Walk along list (again)
• Get block (again)
• Extract single row
• Release latch (again)
• Give row back to client (again)
Row #1
Row #2
• Get latch (again)
• Walk along list (again)
• Get block (again)
• Extract single row
• Release latch (again)
• Give row back to client (again)
Row #3
178
177
178
24/06/2019
90
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a better strategy.... “pinning”
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"fetch a row"
• Get latch
• Walk along list
• Get block
• Pin the block
• Release latch
• Give row 1 back to client
• Give row 2 back to client
…
• Give row n to client
• Get latch
• Walk along list
• Remove my pin on the block
• Release latch
“and .. I’ll will need
several rows from
this block”
179
180
24/06/2019
91
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
how do you say
“I may want many rows” ?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
responsibility of the client
181
182
24/06/2019
92
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
183
FetchDemo nn
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
focus on blocks
184
183
184
24/06/2019
93
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
focus on roundtrips
185
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all that data goes somewhere
186
185
186
24/06/2019
94
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> declare
. . .
9 begin
10 open c;
11 loop
12 fetch c
13 into r;
14 exit when c%notfound;
. . .
19 /
Elapsed: 00:01:11.72
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> declare
. . .
9 begin
10 open c;
11
12 fetch c
13 bulk collect
14 into r;
15
. . .
19 /
Elapsed: 00:00:08.93
187
188
24/06/2019
95
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> select * from v$mystats
2 where name like '%pga%'
3 /
NAME VALUE
------------------------------ ----------
session pga memory 501534672
session pga memory max 501534672
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
500m of memory !
189
190
24/06/2019
96
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
law of diminishing returns
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
pin rows on a block (8k)
192
191
192
24/06/2019
97
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> declare
. . .
9 begin
10 open c;
11 loop
12 fetch c
13 bulk collect
14 into r limit 100;
15 exit when c%notfound;
. . .
19 /
Elapsed: 00:00:08.17
NAME VALUE
------------------------------ ----------
session pga memory 625912
session pga memory max 625912
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> declare
. . .
9 begin
10 for i in c loop
12 ...
16 end loop;
17 end;
18 /
Elapsed: 00:00:08.21
193
194
24/06/2019
98
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
PART #2(a)
storing data
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
the same rules apply
196
195
196
24/06/2019
99
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
think blocks not rows
197
when appropriate
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
commit when it makes sense to
198
197
198
24/06/2019
100
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
demo
199
InsertDemo3
InsertDemo4 nn
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
200
PART #3
running SQL
199
200
24/06/2019
101
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
so you've written your SQL ...
201
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
202
PreparedStatement stmt =
conn.prepareStatement(
"select count(*)
from vehicles
where make = ?
and model = ?");
stmt.setString(1, "HONDA");
stmt.setString(2, "ACCORD");
stmt.execute();
201
202
24/06/2019
102
203
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
1) what is the real query
204
203
204
24/06/2019
103
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
205
?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
206
SQL> set autotrace traceonly stat
SQL> select *
2 from LOOKS_SO_INNOCENT
3 where CREATED > sysdate
Statistics
------------------------------------------
651 recursive calls
0 db block gets
253243 consistent gets
24 physical reads
205
206
24/06/2019
104
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
207
SQL> variable c clob
SQL> begin
2 dbms_utility.expand_sql_text
3 ( 'select * from LOOKS_SO_INNOCENT '||
4 'where created > sysdate',:c);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> print c
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
208
SQL> create or replace
2 view LOOKS_SO_INNOCENT as
3 select
4 o.owner,
5 o.created,
6 s.bytes,
7 s.tablespace_name
8 from
9 dba_segments s,
10 all_objects o
11 where o.owner = s.owner
12 and o.object_name = s.segment_name;
View created.
207
208
24/06/2019
105
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
209
SELECT "A1"."OWNER" "OWNER",
"A1"."NAME" "NAME",
"A1"."CREATED" "CREATED",
"A1"."BYTES" "BYTES",
"A1"."TABLESPACE_NAME" "TABLESPACE_NAME"
FROM (SELECT "A2"."OWNER" "OWNER",
"A2"."OBJECT_NAME" "NAME",
"A2"."CREATED" "CREATED",
"A3"."BYTES" "BYTES",
"A3"."TABLESPACE_NAME" "TABLESPACE_NAME" FROM (SELECT "A4"
."OWNER" "OWNER",
"A4"."SEGMENT_NAME" "SEGMENT_NAME",
"A4"."PARTITION_NAME" "PARTITION_NAME",
"A4"."SEGMENT_TYPE" "SEGMENT_TYPE",
"A4"."SEGMENT_SUBTYPE" "SEGMENT_SUBTYPE",
"A4"."TABLESPACE_NAME" "TABLESPACE_NAME",
"A4"."HEADER_FILE" "HEADER_FILE",
"A4"."HEADER_BLOCK" "HEADER_BLOCK",
DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072,
"A4"."BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1,
"SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_BLOCKS"("A4"."TABLESPACE_ID",
"A4"."RELATIVE_FNO",
"A4"."HEADER_BLOCK",
"A4"."SEGMENT_TYPE_ID",
"A4"."BUFFER_POOL_ID",
"A4"."SEGMENT_FLAGS",
"A4"."SEGMENT_OBJD",
"A4"."BLOCKS"),
"A4"."BLOCKS"))*"A4"."BLOCKSIZE" "BYTES",
DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072,
"A4"."BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1,
"SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_BLOCKS"("A4"."TABLESPACE_ID",
"A4"."RELATIVE_FNO",
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
210
"A4"."HEADER_BLOCK",
"A4"."SEGMENT_TYPE_ID",
"A4"."BUFFER_POOL_ID",
"A4"."SEGMENT_FLAGS",
"A4"."SEGMENT_OBJD",
"A4"."BLOCKS"),
"A4"."BLOCKS")) "BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072,
"A4"."EXTENTS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1,
"SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_EXTENTS"("A4"."TABLESPACE_ID",
"A4"."RELATIVE_FNO",
"A4"."HEADER_BLOCK",
"A4"."SEGMENT_TYPE_ID",
"A4"."BUFFER_POOL_ID",
"A4"."SEGMENT_FLAGS",
"A4"."SEGMENT_OBJD",
"A4"."EXTENTS"),
"A4"."EXTENTS")) "EXTENTS",
"A4"."INITIAL_EXTENT" "INITIAL_EXTENT",
"A4"."NEXT_EXTENT" "NEXT_EXTENT",
"A4"."MIN_EXTENTS" "MIN_EXTENTS",
"A4"."MAX_EXTENTS" "MAX_EXTENTS",
"A4"."MAX_SIZE" "MAX_SIZE",
"A4"."RETENTION" "RETENTION",
"A4"."MINRETENTION" "MINRETENTION",
"A4"."PCT_INCREASE" "PCT_INCREASE",
"A4"."FREELISTS" "FREELISTS",
"A4"."FREELIST_GROUPS" "FREELIST_GROUPS",
"A4"."RELATIVE_FNO" "RELATIVE_FNO",
DECODE("A4"."BUFFER_POOL_ID",1,'KEEP',2,'RECYCLE','DEFAULT') "BUFFER_POOL",
DECODE("A4"."FLASH_CACHE",1,'KEEP',2,
'NONE','DEFAULT')
"FLASH_CACHE",DECODE("A4"."CELL_FLASH_CACHE",1,'KEEP',2,'NONE','DEFAULT')
"CELL_FLASH_CACHE"
FROM ( (SELECT NVL("A199"."NAME",'SYS') "OWNER",
"A198"."NAME" "SEGMENT_NAME",
209
210
24/06/2019
106
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
211
"A198"."SUBNAME" "PARTITION_NAME",
"A196"."OBJECT_TYPE" "SEGMENT_TYPE",
"A195"."TYPE#" "SEGMENT_TYPE_ID",DECODE(BIT
AND("A195"."SPARE1",2097408),2097152,'SECUREFILE',256,'ASSM','MSSM') "SEGMENT_SUBTYPE",
"A197"."TS#" "TABLESPACE_ID",
"A197"."NAME" "TABLESPACE_NAME",
"A197"."BLOCKSIZE" "BLOCKSIZE",
"A194"."FILE#" "HEADER_FILE",
"A195"."BLOCK#" "HEADER_BLOCK",
"A195"."BLOCKS"*"A197"."BLOCKSIZE" "BYTES",
"A195"."BLOCKS" "BLOCKS",
"A195"."EXTENTS" "EXTENTS",
"A195"."INIEXTS"*"A197"."BLOCKSIZE" "INITIAL_EXTENT",
"A195"."EXTSIZE"*"A197"."BLOCKSIZE" "NEXT_EXTENT",
"A195"."MINEXTS" "MIN_EXTENTS",
"A195"."MAXEXTS" "MAX_EXTENTS",DECODE(BITAND("A195"."SPARE1",4194304),4194304,
"A195"."BITMAPRANGES",NULL) "MAX_SIZE",TO_CHAR(DECODE(
BITAND("A195"."SPARE1",2097152),2097152,
DECODE("A195"."LISTS",0,'NONE',1,'AUTO',2,'MIN',3,'MAX',4,'DEFAULT','INVALID'),NULL))
"RETENTION",DECODE(BITAND("A195"."SPARE1",2097152),2097152,
"A195"."GROUPS",NULL) "MINRETENTION",DECODE(BITAND("A197"."FLAGS",3),1,TO_NUMBER(NULL),
"A195"."EXTPCT") "PCT_INCREASE",DECODE(BITAND("A197"."FLAGS",32),32,
TO_NUMBER(NULL),DECODE("A195"."LISTS",0,1,
"A195"."LISTS"))
"FREELISTS",DECODE(BITAND("A197"."FLAGS",32),32,TO_NUMBER(NULL),DECODE("A195"."GROUPS",0,1,
"A195"."GROUPS")) "FREELIST_GROUPS",
"A195"."FILE#" "RELATIVE_FNO",BITAND("A195"."CACHEHINT",3) "BUFFER_POOL_ID",
BITAND("A195"."CACHEHINT",12)/4 "FLASH_CACHE",BITAND("A195"."CACHEHINT",48)/16 "CELL_FLASH_CACHE",
NVL("A195"."SPARE1",0)
"SEGMENT_FLAGS",DECODE(BITAND("A195"."SPARE1",1),1,
"A195"."HWMINCR",
"A198"."DATAOBJ#") "SEGMENT_OBJD" FROM "SYS"."USER$" "A199",
"SYS"."OBJ$" "A198",
"SYS"."TS$" "A197", ( (SELECT
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
212
DECODE(BITAND("A209"."PROPERTY",8192),8192,'NESTED TABLE','TABLE') "OBJECT_TYPE",
2 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID",
"A209"."OBJ#" "OBJECT_ID",
"A209"."FILE#" "HEADER_FILE",
"A209"."BLOCK#" "HEADER_BLOCK",
"A209"."TS#" "TS_NUMBER" FROM "SYS"."TAB$" "A209" WHERE BITAND("A209"."PROPERTY",1024)=0) UNI
ON ALL (SELECT 'TABLE PARTITION' "OBJECT_TYPE",19 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID",
"A208"."OBJ#" "OBJECT_ID",
"A208"."FILE#" "HEADER_FILE",
"A208"."BLOCK#" "HEADER_BLOCK",
"A208"."TS#" "TS_NUMBER" FROM "SYS"."TABPART$" "A208") UNION ALL
(SELECT 'CLUSTER' "OBJECT_TYPE",3 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID",
"A207"."OBJ#" "OBJECT_ID",
"A207"."FILE#" "HEADER_FILE",
"A207"."BLOCK#" "HEADER_BLOCK",
"A207"."TS#" "TS_NUMBER" FROM "SYS"."CLU$" "A207") UNION ALL
(SELECT DECODE("A206"."TYPE#",8,'LOBINDEX','INDEX') "OBJECT_TYPE",1 "OBJECT_TYPE_ID",6
"SEGMENT_TYPE_ID",
"A206"."OBJ#" "OBJECT_ID",
"A206"."FILE#" "HEADER_FILE",
"A206"."BLOCK#" "HEADER_BLOCK",
"A206"."TS#" "TS_NUMBER" FROM "SYS"."IND$" "A206" WHERE "A206"."TYPE#"=1 OR
"A206"."TYPE#"=2 OR
"A206"."TYPE#"=3 OR
"A206"."TYPE#"=4 OR
"A206"."TYPE#"=6 OR
"A206"."TYPE#"=7 OR
"A206"."TYPE#"=8 OR
"A206"."TYPE#"=9) UNION ALL
(SELECT 'INDEX PARTITION'
"OBJECT_TYPE",20 "OBJECT_TYPE_ID",6 "SEGMENT_TYPE_ID",
"A205"."OBJ#" "OBJECT_ID",
"A205"."FILE#" "HEADER_FILE",
"A205"."BLOCK#" "HEADER_BLOCK",
211
212
24/06/2019
107
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
213
"A205"."TS#" "TS_NUMBER" FROM "SYS"."INDPART$" "A205") UNION ALL
(SELECT 'LOBSEGMENT' "OBJECT_TYPE",21 "OBJECT_TYPE_ID",8 "SEGMENT_TYPE_ID",
"A204"."LOBJ#" "OBJECT_ID",
"A204"."FILE#" "HEADER_FILE",
"A204"."BLOCK#" "HEADER_BLOCK",
"A204"."TS#" "TS_NUMBER" FROM "SYS"."LOB$" "A204" WHERE BITAND("A204"."PROPERTY",64)=0 OR
BITAND("A204"."PROPERTY",128)=128) UNION ALL
(SELECT 'TABLE SUBPARTITION' "OBJECT_TYPE",34 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID",
"A203"."OBJ#" "OBJECT_ID",
"A203"."FILE#" "HEADER_FILE",
"A203"."BLOCK#" "HEADER_BLOCK",
"A203"."TS#" "TS_NUMBER" FROM "SYS"."TABSUBPART$" "A203") UNION ALL
(SELECT 'INDEX SUBPARTITION' "OBJECT_TYPE",35 "OBJECT_TYPE_ID",6 "SEGMENT_TYPE_ID",
"A202"."OBJ#" "OBJECT_ID",
"A202"."FILE#" "HEADER_FILE",
"A202"."BLOCK#" "HEADER_BLOCK",
"A202"."TS#" "TS_NUMBER" FROM "SYS"."INDSUBPART$" "A202") UNION ALL
(SELECT DECODE("A201"."FRAGTYPE$",'P','LOB PARTITION','LOB SUBPARTITION')
"OBJECT_TYPE",DECODE("A201"."FRAGTYPE$",'P',40,41) "OBJECT_TYPE_ID",8 "SEGMENT_TYPE_ID",
"A201"."FRAGOBJ#" "OBJECT_ID",
"A201"."FILE#" "HEADER_FILE",
"A201"."BLOCK#" "HEADER_BLOCK",
"A201"."TS#" "TS_NUMBER" FROM "SYS"."LOBFRAG$" "A201")) "A196",
"SYS"."SEG$" "A195",
"SYS"."FILE$" "A194" WHERE "A195"."FILE#"="A196"."HEADER_FILE" AND
"A195"."BLOCK#"="A196"."HEADER_BLOCK" AND
"A195"."TS#"="A196"."TS_NUMBER" AND
"A195"."TS#"="A197"."TS#" AND
"A198"."OBJ#"="A196"."OBJECT_ID" AND
"A198"."OWNER#"="A199"."USER#"(+) AND
"A195"."TYPE#"="A196"."SEGMENT_TYPE_ID" AND
"A198"."TYPE#"="A196"."OBJECT_TYPE_ID" AND
"A195"."TS#"="A194"."TS#" AND
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
21 ...
214
more ...
pages !
213
214
24/06/2019
108
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
215
back to our problem SQL
PreparedStatement stmt =
conn.prepareStatement(
"select count(*)
from vehicles
where make = ?
and model = ?");
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
216
2) is it sensible/correct ?
215
216
24/06/2019
109
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
217
SQL> select ...
2 from EMP e,
3 DEPT d
4 where e.JOB = 'SALES'
5 and d.LOC = 'NORTH';
6 and d.DEPTNO = e.DEPTNO
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
218
SQL> select REGION, min(AMOUNT)
2 from EMP e,
3 SALES_TRANSACTIONS s
4 where e.JOB = 'SALES'
5 and s.EMPNO = e.EMPNO
6 and s.REGION = 'CA'
7 and s.TAX_AMT > 10
8 or s.SUBSIDY > 0
9 group by REGION
7 and s.TAX_AMT > 10
8 or s.SUBSIDY > 0
9 group by REGION
(
)
217
218
24/06/2019
110
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
219
SQL> select *
2 from sales
3 where region in
4 ( select region
5 from promoted_locations
6 where campaign = 'BLACK FRIDAY');
SQL> desc PROMOTED_LOCATIONS
Name Null? Type
------------------------ -------- -------------
ID NOT NULL NUMBER
CAMPAIGN NOT NULL VARCHAR2(128)
PROMOTED_REGION NOT NULL VARCHAR2(24)
...
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
220
3) the execution plan
219
220
24/06/2019
111
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
optimizer got it wrong...
221
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
or
222
221
222
24/06/2019
112
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... it cannot run better
223
legitimate response
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1| 5|
| 1 | SORT AGGREGATE | | 1| 5|
| 2 | TABLE ACCESS BY INDEX ROWID | VEHICLES | 20K| 687K|
| 3 | INDEX RANGE SCAN | MAKE_IX | 45K| 592K|
------------------------------------------------------------------
224
PreparedStatement stmt =
conn.prepareStatement(
"select count(*)
from vehicles
where make = ?
and model = ?");
223
224
24/06/2019
113
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
225
now what ?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
226
for human beings ...
225
226
24/06/2019
114
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
227
... cardinality is everything
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
228
227
228
24/06/2019
115
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
230
same with the optimizer
229
230
24/06/2019
116
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
understanding cardinality and cost crucial
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
summary
231
232
24/06/2019
117
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
avoid parsing costs
for high frequency SQL
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
use binding and don't get hacked !
233
234
24/06/2019
118
Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
using PLSQL is golden
SQL - the features
235
236
24/06/2019
119
Thanks for coming!
connor-mcdonald.com
https://linktr.ee/connor
237
238

More Related Content

PDF
APEX Connect 2019 - SQL Tuning 101
PDF
APEX Connect 2019 - successful application development
PDF
Latin America Tour 2019 - slow data and sql processing
PPTX
Sangam 18 - Database Development: Return of the SQL Jedi
PDF
OOW19 - Slower and less secure applications
PDF
APEX tour 2019 - successful development with autonomous
PDF
ITOUG 2019 - 18c, 19c features
PDF
ILOUG 2019 - Autonomous, what does it mean for DBAs
APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - successful application development
Latin America Tour 2019 - slow data and sql processing
Sangam 18 - Database Development: Return of the SQL Jedi
OOW19 - Slower and less secure applications
APEX tour 2019 - successful development with autonomous
ITOUG 2019 - 18c, 19c features
ILOUG 2019 - Autonomous, what does it mean for DBAs

Similar to Kscope19 - Understanding the basics of SQL processing (20)

PDF
APEX Connect 2019 - array/bulk processing in PLSQL
PDF
Latin America Tour 2019 - 18c and 19c featues
PPTX
Perth APAC Groundbreakers tour - The Autonomous Database
PDF
ILOUG 2019 - 18c/19c features
PDF
Sangam 19 - Successful Applications on Autonomous
PDF
18c and 19c features for DBAs
PDF
Hyderabad Mar 2019 - Database 18c / 19c
PDF
Developer day v2
PPTX
Hyderabad Mar 2019 - Autonomous Database
PPTX
Tokyo APAC Groundbreakers tour - The Complete Java Developer
PPTX
Perth APAC Groundbreakers tour - 18c features
PDF
APAC Groundbreakers 2019 - Perth/Melbourne
PPTX
The Oracle Autonomous Database
PPTX
OpenWorld 2018 - SQL Tuning in 20 mins
PPTX
OpenWorld 2018 - 20 years of hints and tips
PDF
ITOUG 2019 - 25 years of hints and tips
PPTX
Melbourne Groundbreakers Tour - Hints and Tips
PDF
Feb14 successful development
PPTX
12 Things About 12c Release 2 for Developers
PDF
Java Programming With Oracle Jdbc 1st Ed Donald Bales
APEX Connect 2019 - array/bulk processing in PLSQL
Latin America Tour 2019 - 18c and 19c featues
Perth APAC Groundbreakers tour - The Autonomous Database
ILOUG 2019 - 18c/19c features
Sangam 19 - Successful Applications on Autonomous
18c and 19c features for DBAs
Hyderabad Mar 2019 - Database 18c / 19c
Developer day v2
Hyderabad Mar 2019 - Autonomous Database
Tokyo APAC Groundbreakers tour - The Complete Java Developer
Perth APAC Groundbreakers tour - 18c features
APAC Groundbreakers 2019 - Perth/Melbourne
The Oracle Autonomous Database
OpenWorld 2018 - SQL Tuning in 20 mins
OpenWorld 2018 - 20 years of hints and tips
ITOUG 2019 - 25 years of hints and tips
Melbourne Groundbreakers Tour - Hints and Tips
Feb14 successful development
12 Things About 12c Release 2 for Developers
Java Programming With Oracle Jdbc 1st Ed Donald Bales
Ad

More from Connor McDonald (20)

PDF
Flashback ITOUG
PDF
Sangam 19 - PLSQL still the coolest
PDF
Sangam 19 - Analytic SQL
PDF
UKOUG - 25 years of hints and tips
PDF
Sangam 2019 - The Latest Features
PDF
UKOUG 2019 - SQL features
PDF
OOW19 - Flashback, not just for DBAs
PDF
OOW19 - Read consistency
PDF
OOW19 - Killing database sessions
PDF
OOW19 - Ten Amazing SQL features
PDF
Latin America tour 2019 - Flashback
PDF
Latin America Tour 2019 - 10 great sql features
PDF
Latin America Tour 2019 - pattern matching
PDF
ANSI vs Oracle language
PDF
OG Yatra - upgrading to the new 12c+ optimizer
PDF
OG Yatra - 25 years of hints and tips
PDF
OG Yatra - Flashback, not just for developers
PDF
Kscope19 - Flashback: Good for Developers as well as DBAs
PDF
KScope19 - SQL Features
PDF
Pattern Matching with SQL - APEX World Rotterdam 2019
Flashback ITOUG
Sangam 19 - PLSQL still the coolest
Sangam 19 - Analytic SQL
UKOUG - 25 years of hints and tips
Sangam 2019 - The Latest Features
UKOUG 2019 - SQL features
OOW19 - Flashback, not just for DBAs
OOW19 - Read consistency
OOW19 - Killing database sessions
OOW19 - Ten Amazing SQL features
Latin America tour 2019 - Flashback
Latin America Tour 2019 - 10 great sql features
Latin America Tour 2019 - pattern matching
ANSI vs Oracle language
OG Yatra - upgrading to the new 12c+ optimizer
OG Yatra - 25 years of hints and tips
OG Yatra - Flashback, not just for developers
Kscope19 - Flashback: Good for Developers as well as DBAs
KScope19 - SQL Features
Pattern Matching with SQL - APEX World Rotterdam 2019
Ad

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KodekX | Application Modernization Development
Cloud computing and distributed systems.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
sap open course for s4hana steps from ECC to s4
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Weekly Chronicles - August'25 Week I
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction

Kscope19 - Understanding the basics of SQL processing

  • 2. 24/06/2019 2 ... SQL processing, aka ... successful application development 3 4
  • 3. 24/06/2019 3 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Connor McDonald Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6 5 6
  • 4. 24/06/2019 4 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8 7 8
  • 5. 24/06/2019 5 Getting in touch is easy... connor-mcdonald.com https://linktr.ee/connor Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 10https://asktom.oracle.com 9 10
  • 6. 24/06/2019 6 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 1 https://asktom.oracle.com/officehours Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 200 hours free access so far 12 11 12
  • 7. 24/06/2019 7 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. assumptions 1. You roughly know databases (tables and columns) 2. You can write a SQL statement Copyright © 2018, Oracle and/or its affiliates. All rights reserved. before we begin 13 14
  • 8. 24/06/2019 8 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. recommended reading Copyright © 2018, Oracle and/or its affiliates. All rights reserved. docs.oracle.com 15 16
  • 9. 24/06/2019 9 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. its free ! Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Concepts guide http://bit.ly/oraconcepts 17 18
  • 10. 24/06/2019 10 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. better than lots of current people Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Application Developers guide http://bit.ly/oradevdoc 19 20
  • 11. 24/06/2019 11 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. better than lots of current people Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL is like any language 22 21 22
  • 12. 24/06/2019 12 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. compile it 23 on the fly Copyright © 2018, Oracle and/or its affiliates. All rights reserved. run it 24 23 24
  • 13. 24/06/2019 13 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. get | store results 25 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. for successful applications... 26 25 26
  • 14. 24/06/2019 14 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. compile it ... fast 27 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. run it ... fast 28 27 28
  • 15. 24/06/2019 15 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. get | store results ... fast 29 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. that's it! 30 29 30
  • 16. 24/06/2019 16 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. this session 31 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. compile execute store/fetch results 32 31 32
  • 17. 24/06/2019 17 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. before we begin ... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. some controversy to start 33 34
  • 18. 24/06/2019 18 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. here's the problem... 35 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ... it's us 35 36
  • 19. 24/06/2019 19 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. a small digression Copyright © 2018, Oracle and/or its affiliates. All rights reserved. a little bit of history on servers ... 37 38
  • 20. 24/06/2019 20 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. (not too) long ago Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 39 40
  • 21. 24/06/2019 21 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. FSB = "front side bus" 41 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. any access to ... anything 41 42
  • 22. 24/06/2019 22 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. CPU capped 43 44
  • 23. 24/06/2019 23 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. hypertransport 45 46
  • 24. 24/06/2019 24 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. memory access direct from CPU 47 47 48
  • 25. 24/06/2019 25 ~2007 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. "why do I care?" 49 50
  • 26. 24/06/2019 26 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. you can't blame the server (anymore) 51 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. if you can't get that performance ... 51 52
  • 27. 24/06/2019 27 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ... it's you 53 54
  • 29. 24/06/2019 29 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. all have their place... 57 58
  • 30. 24/06/2019 30 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. "The sooner your company admits that you are not Google, the sooner you can get down to some real work" - Ted Dziuba 59 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. so what's holding you back ? 59 60
  • 31. 24/06/2019 31 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. It all comes down to... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. too much work or ... 61 62
  • 32. 24/06/2019 32 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ... not being able to do work 63 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. PART #1 64 compiling SQL 63 64
  • 33. 24/06/2019 33 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ... how to avoid work when processing SQL Copyright © 2018, Oracle and/or its affiliates. All rights reserved. terminology 66 65 66
  • 34. 24/06/2019 34 cursors declare cursor C(p number) is select * from DEPT where DEPTNO = p; begin for rec in C loop … end loop; end; select * from EMPLOYEE where EMPNO > 1234; delete from MY_TABLE; drop table MY_TABLE; begin MY_PROCEDURE(1,2,3); end; Copyright © 2018, Oracle and/or its affiliates. All rights reserved. all of them ! 67 68
  • 35. 24/06/2019 35 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 3 phases Copyright © 2018, Oracle and/or its affiliates. All rights reserved. open process close gimme some memory do some stuff using that memory, maybe access other memory here's your memory back 70 69 70
  • 36. 24/06/2019 36 memory access = controlled access a quick primer on (database) memory 71 72
  • 40. 24/06/2019 40 79 limited resource lots of people want it concurrent access causes problems it's a complex system 79 80
  • 43. 24/06/2019 43 SGA protected by 2) access memory SGA protected by 3) release latch 85 86
  • 45. 24/06/2019 45 active wait spinning can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? can I have the latch ? 90 89 90
  • 50. 24/06/2019 50 DONE Copyright © 2018, Oracle and/or its affiliates. All rights reserved. its importance to Oracle 99 100
  • 51. 24/06/2019 51 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. early Oracle Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 2000 times ! (_spin_count) 102 101 102
  • 52. 24/06/2019 52 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Zzzzzzzzzz.... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. nowadays... yield mutex posting Good reading: http://andreynikolaev.wordpress.com/ CAS 103 104
  • 53. 24/06/2019 53 "Errrr.... weren't we talking SQL?" to run a SQL statement 105 106
  • 54. 24/06/2019 54 Syntax Validity Optimization Rowsourcing Execution (Fetch) SQL> select * 2 frmo emp; frmo emp * ERROR at line 2: ORA-00923: FROM keyword not found where expected 107 Syntax Validity Optimization Rowsourcing Execution (Fetch) SQL> select empnoo 2 from emp; select empnoo * ERROR at line 1: ORA-00904: invalid column name 107 108
  • 55. 24/06/2019 55 Syntax Validity Optimization Rowsourcing Execution (Fetch) PLAN ------------------------------------- SELECT STATEMENT TABLE ACCESS BY INDEX ROWID EMP INDEX RANGE SCAN EMP_PK EMP_PK EMP Syntax Validity Optimization Rowsourcing Execution (Fetch) 109 110
  • 58. 24/06/2019 58 lots of memory access 115 lots of latching ! 116 115 116
  • 59. 24/06/2019 59 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. impossible to avoid ? Copyright © 2018, Oracle and/or its affiliates. All rights reserved. library cache 117 118
  • 60. 24/06/2019 60 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. previously executed statements 119 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. parse statement already in library cache ? reuse optimizer info reuse row source info select * from emp where empno = 123 syntactically, semantics OK 119 120
  • 61. 24/06/2019 61 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. select surname, firstname from emp where empno = 123 select * from dept where deptno = 4567 select * from customer where locality = 17 select prno from property where reference = 'X123G' select * from dept where deptno = 23 select surname, firstname from emp where empno = 123 select * from customer where locality = 256 select * from organisation where officer = 'OFF876' select surname, firstname from emp where empno = 7632 select * from dept where deptno = 4567 select offender from crimes where crno = 247462 Two full parses avoided library cache Copyright © 2018, Oracle and/or its affiliates. All rights reserved. select surname, firstname from emp where empno = 123 select * from dept where dname = 'SALES' probability of reuse low ? 121 122
  • 62. 24/06/2019 62 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. some queries are “nearly the same” 123 select surname, firstname from emp where empno = 123 select surname, firstname from emp where empno = 456 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. binding parse this... now run it with ? = 123 now run it with ? = 456 select surname, firstname from emp where empno = ? 123 124
  • 63. 24/06/2019 63 select surname, firstname from emp where empno = ? select * from dept where deptno = ? select * from customer where locality = ? select prno from property where reference = ? select * from dept where deptno = ? select surname, firstname from emp where empno = ? select * from customer where locality = ? select * from organisation where officer = ? select surname, firstname from emp where empno = ? select * from dept where deptno = ? select offender from crimes where crno = ? Five full parses avoided library cache demo ParseDemo ParseDemoBind 125 126
  • 64. 24/06/2019 64 "performance still looks ok" let's make it real ParseDemo2 nn ParseDemo2Bind nn 127 128
  • 65. 24/06/2019 65 much more serious select pk from parse_demo where pk = 17 select pk from parse_demo where pk = 123 select pk from parse_demo where pk = 43 select pk from parse_demo where pk = 33 select pk from parse_demo where pk = 127 select pk from parse_demo where pk = 5432 select pk from parse_demo where pk = 12 select pk from parse_demo where pk = 876 select pk from parse_demo where pk = 76 select pk from parse_demo where pk = 19 select pk from parse_demo where pk = 213 select pk from parse_demo where pk = 543 select pk from parse_demo where pk = 4532 select pk from parse_demo where pk = 7544 select pk from parse_demo where pk = 6543 select pk from parse_demo where pk = 452 129 130
  • 66. 24/06/2019 66 building SQL by concatenation you'll get hacked 131 132
  • 67. 24/06/2019 67 select ename from emp where empno = 6543 select ename from emp where empno = 6543 and 1=0 union all select table_name from all_tables where table_name like '%SECURITY%' select ename from emp where empno = 6543 and 1=0 union all select username from app_security where ... 134 133 134
  • 69. 24/06/2019 69 it takes 5 minutes to hack you for fast, secure SQL ... 137 138
  • 70. 24/06/2019 70 ... always bind user input Copyright © 2018, Oracle and/or its affiliates. All rights reserved. while we are talking binding 139 140
  • 71. 24/06/2019 71 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. parse statement already in library cache ? reuse optimizer info reuse row source info select * from emp where empno = 123 syntactically, semantics OK Yes! Yes! ??? ??? Copyright © 2018, Oracle and/or its affiliates. All rights reserved. still some parsing to do 142 141 142
  • 72. 24/06/2019 72 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Syntax Validity Optimization Rowsourcing Execution (Fetch) Hard Parse Soft Parse foundnot found in lib cache Copyright © 2018, Oracle and/or its affiliates. All rights reserved. scott@mydb> select * 2 from EMP 3 where EMPNO = :b1 mike@mydb> select * 2 from EMP 3 where EMPNO = :b1 SQL> desc mike.emp Name Null? Type ----------------- -------- ------------ EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) 143 144
  • 73. 24/06/2019 73 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. (even soft) parsing = memory access = latching Copyright © 2018, Oracle and/or its affiliates. All rights reserved. can we do better ? 145 146
  • 74. 24/06/2019 74 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. recall Copyright © 2018, Oracle and/or its affiliates. All rights reserved. open process close gimme some memory do some stuff using that memory, probably access other memory here's your memory back 148 147 148
  • 75. 24/06/2019 75 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. your SQL is always shareable library cache select * from emp Copyright © 2018, Oracle and/or its affiliates. All rights reserved. your cursor points to it library cache select * from emp cursor 150 149 150
  • 76. 24/06/2019 76 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. close cursor = lose pointer library cache select * from emp cursor Copyright © 2018, Oracle and/or its affiliates. All rights reserved. next time we parse soft parse 151 152
  • 77. 24/06/2019 77 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. impossible to avoid ? 153 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. what if we don't close the cursor 153 154
  • 78. 24/06/2019 78 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. emplist(String[] args) { if "first time I'm using this" { else "reset the pointer, re-execute" } library cache select * from emp cursor 1 select * from dept cursor 2 select * from emp where ... cursor 3 pstmt_all_emp = con.prepareStatement("select * from emp"); Copyright © 2018, Oracle and/or its affiliates. All rights reserved. don’t close the cursorhang on to the memoryremember what cursors we’ve usedreuse whenever possibledon’t exhaust memorydon’t allow invalid cursor reuse 155 156
  • 79. 24/06/2019 79 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. sounds complicated ... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. a free solution exists 157 158
  • 80. 24/06/2019 80 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. plsql Copyright © 2018, Oracle and/or its affiliates. All rights reserved. create procedure MY_PROC is begin select empno, ename, ... into … from emp where empno = my_plsql_variable ... end; 159 160
  • 81. 24/06/2019 81 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. static SQL in stored PLSQL automatically uses bind variables hold’s cursors open (even if you close) Copyright © 2018, Oracle and/or its affiliates. All rights reserved. better cursor caching 161 162
  • 82. 24/06/2019 82 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. procedure MY_PROC is cursor c_emp is select * from emp; begin open c_emp; fetch c_emp into …; close c_emp; end; select * from emp procedure MY_PROC is begin select * into … from dept where … end; select * from dept … Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 164 PART #2 get | store results we'll come back to running SQL 163 164
  • 83. 24/06/2019 83 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 165 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. start with reading data 166 165 166
  • 84. 24/06/2019 84 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. querying data Copyright © 2018, Oracle and/or its affiliates. All rights reserved. data is stored in blocks | pages 167 168
  • 85. 24/06/2019 85 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. to read data, you read blocks... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> select * 2 from EMP 3 where ... /u01/my_data1.dat memory 169 170
  • 86. 24/06/2019 86 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. but (database) memory access is complex 171 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> select * 2 from EMP 3 where … SQL> update EMP 2 set … 3 where … SQL> select * 2 from EMP 3 where … SQL> insert into 2 EMP 3 values (…) SQL> select * 2 from CUSTOMER 3 / SQL> select max(EMP) 2 from DEPT SQL> delete 2 from DEPT 3 / 171 172
  • 87. 24/06/2019 87 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. we have to latch ! Copyright © 2018, Oracle and/or its affiliates. All rights reserved. • Get latch • Search along list of blocks in memory • Read block • Extract row of interest • Release latch • Give row back to client "fetch a row" 174 173 174
  • 88. 24/06/2019 88 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. lots of rows Copyright © 2018, Oracle and/or its affiliates. All rights reserved. lots of latching 175 176
  • 89. 24/06/2019 89 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. typical program Copyright © 2018, Oracle and/or its affiliates. All rights reserved. rs = stmt.executeQuery("..."); while(rs.next()) { v1 = rs.getInt(1); v2 = rs.getString(2); ... } • Get latch • Walk along list • Get block • Extract single row • Release latch • Give row back to client • Get latch (again) • Walk along list (again) • Get block (again) • Extract single row • Release latch (again) • Give row back to client (again) Row #1 Row #2 • Get latch (again) • Walk along list (again) • Get block (again) • Extract single row • Release latch (again) • Give row back to client (again) Row #3 178 177 178
  • 90. 24/06/2019 90 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. a better strategy.... “pinning” Copyright © 2018, Oracle and/or its affiliates. All rights reserved. "fetch a row" • Get latch • Walk along list • Get block • Pin the block • Release latch • Give row 1 back to client • Give row 2 back to client … • Give row n to client • Get latch • Walk along list • Remove my pin on the block • Release latch “and .. I’ll will need several rows from this block” 179 180
  • 91. 24/06/2019 91 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. how do you say “I may want many rows” ? Copyright © 2018, Oracle and/or its affiliates. All rights reserved. responsibility of the client 181 182
  • 92. 24/06/2019 92 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 183 FetchDemo nn Copyright © 2018, Oracle and/or its affiliates. All rights reserved. focus on blocks 184 183 184
  • 93. 24/06/2019 93 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. focus on roundtrips 185 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. all that data goes somewhere 186 185 186
  • 94. 24/06/2019 94 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> declare . . . 9 begin 10 open c; 11 loop 12 fetch c 13 into r; 14 exit when c%notfound; . . . 19 / Elapsed: 00:01:11.72 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> declare . . . 9 begin 10 open c; 11 12 fetch c 13 bulk collect 14 into r; 15 . . . 19 / Elapsed: 00:00:08.93 187 188
  • 95. 24/06/2019 95 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> select * from v$mystats 2 where name like '%pga%' 3 / NAME VALUE ------------------------------ ---------- session pga memory 501534672 session pga memory max 501534672 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 500m of memory ! 189 190
  • 96. 24/06/2019 96 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. law of diminishing returns Copyright © 2018, Oracle and/or its affiliates. All rights reserved. pin rows on a block (8k) 192 191 192
  • 97. 24/06/2019 97 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> declare . . . 9 begin 10 open c; 11 loop 12 fetch c 13 bulk collect 14 into r limit 100; 15 exit when c%notfound; . . . 19 / Elapsed: 00:00:08.17 NAME VALUE ------------------------------ ---------- session pga memory 625912 session pga memory max 625912 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. SQL> declare . . . 9 begin 10 for i in c loop 12 ... 16 end loop; 17 end; 18 / Elapsed: 00:00:08.21 193 194
  • 98. 24/06/2019 98 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. PART #2(a) storing data Copyright © 2018, Oracle and/or its affiliates. All rights reserved. the same rules apply 196 195 196
  • 99. 24/06/2019 99 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. think blocks not rows 197 when appropriate Copyright © 2018, Oracle and/or its affiliates. All rights reserved. commit when it makes sense to 198 197 198
  • 100. 24/06/2019 100 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. demo 199 InsertDemo3 InsertDemo4 nn Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 200 PART #3 running SQL 199 200
  • 101. 24/06/2019 101 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. so you've written your SQL ... 201 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 202 PreparedStatement stmt = conn.prepareStatement( "select count(*) from vehicles where make = ? and model = ?"); stmt.setString(1, "HONDA"); stmt.setString(2, "ACCORD"); stmt.execute(); 201 202
  • 102. 24/06/2019 102 203 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 1) what is the real query 204 203 204
  • 103. 24/06/2019 103 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 205 ? Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 206 SQL> set autotrace traceonly stat SQL> select * 2 from LOOKS_SO_INNOCENT 3 where CREATED > sysdate Statistics ------------------------------------------ 651 recursive calls 0 db block gets 253243 consistent gets 24 physical reads 205 206
  • 104. 24/06/2019 104 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 207 SQL> variable c clob SQL> begin 2 dbms_utility.expand_sql_text 3 ( 'select * from LOOKS_SO_INNOCENT '|| 4 'where created > sysdate',:c); 5 end; 6 / PL/SQL procedure successfully completed. SQL> print c Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 208 SQL> create or replace 2 view LOOKS_SO_INNOCENT as 3 select 4 o.owner, 5 o.created, 6 s.bytes, 7 s.tablespace_name 8 from 9 dba_segments s, 10 all_objects o 11 where o.owner = s.owner 12 and o.object_name = s.segment_name; View created. 207 208
  • 105. 24/06/2019 105 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 209 SELECT "A1"."OWNER" "OWNER", "A1"."NAME" "NAME", "A1"."CREATED" "CREATED", "A1"."BYTES" "BYTES", "A1"."TABLESPACE_NAME" "TABLESPACE_NAME" FROM (SELECT "A2"."OWNER" "OWNER", "A2"."OBJECT_NAME" "NAME", "A2"."CREATED" "CREATED", "A3"."BYTES" "BYTES", "A3"."TABLESPACE_NAME" "TABLESPACE_NAME" FROM (SELECT "A4" ."OWNER" "OWNER", "A4"."SEGMENT_NAME" "SEGMENT_NAME", "A4"."PARTITION_NAME" "PARTITION_NAME", "A4"."SEGMENT_TYPE" "SEGMENT_TYPE", "A4"."SEGMENT_SUBTYPE" "SEGMENT_SUBTYPE", "A4"."TABLESPACE_NAME" "TABLESPACE_NAME", "A4"."HEADER_FILE" "HEADER_FILE", "A4"."HEADER_BLOCK" "HEADER_BLOCK", DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072, "A4"."BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1, "SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_BLOCKS"("A4"."TABLESPACE_ID", "A4"."RELATIVE_FNO", "A4"."HEADER_BLOCK", "A4"."SEGMENT_TYPE_ID", "A4"."BUFFER_POOL_ID", "A4"."SEGMENT_FLAGS", "A4"."SEGMENT_OBJD", "A4"."BLOCKS"), "A4"."BLOCKS"))*"A4"."BLOCKSIZE" "BYTES", DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072, "A4"."BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1, "SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_BLOCKS"("A4"."TABLESPACE_ID", "A4"."RELATIVE_FNO", Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 210 "A4"."HEADER_BLOCK", "A4"."SEGMENT_TYPE_ID", "A4"."BUFFER_POOL_ID", "A4"."SEGMENT_FLAGS", "A4"."SEGMENT_OBJD", "A4"."BLOCKS"), "A4"."BLOCKS")) "BLOCKS",DECODE(BITAND("A4"."SEGMENT_FLAGS",131072),131072, "A4"."EXTENTS",DECODE(BITAND("A4"."SEGMENT_FLAGS",1),1, "SYS"."DBMS_SPACE_ADMIN"."SEGMENT_NUMBER_EXTENTS"("A4"."TABLESPACE_ID", "A4"."RELATIVE_FNO", "A4"."HEADER_BLOCK", "A4"."SEGMENT_TYPE_ID", "A4"."BUFFER_POOL_ID", "A4"."SEGMENT_FLAGS", "A4"."SEGMENT_OBJD", "A4"."EXTENTS"), "A4"."EXTENTS")) "EXTENTS", "A4"."INITIAL_EXTENT" "INITIAL_EXTENT", "A4"."NEXT_EXTENT" "NEXT_EXTENT", "A4"."MIN_EXTENTS" "MIN_EXTENTS", "A4"."MAX_EXTENTS" "MAX_EXTENTS", "A4"."MAX_SIZE" "MAX_SIZE", "A4"."RETENTION" "RETENTION", "A4"."MINRETENTION" "MINRETENTION", "A4"."PCT_INCREASE" "PCT_INCREASE", "A4"."FREELISTS" "FREELISTS", "A4"."FREELIST_GROUPS" "FREELIST_GROUPS", "A4"."RELATIVE_FNO" "RELATIVE_FNO", DECODE("A4"."BUFFER_POOL_ID",1,'KEEP',2,'RECYCLE','DEFAULT') "BUFFER_POOL", DECODE("A4"."FLASH_CACHE",1,'KEEP',2, 'NONE','DEFAULT') "FLASH_CACHE",DECODE("A4"."CELL_FLASH_CACHE",1,'KEEP',2,'NONE','DEFAULT') "CELL_FLASH_CACHE" FROM ( (SELECT NVL("A199"."NAME",'SYS') "OWNER", "A198"."NAME" "SEGMENT_NAME", 209 210
  • 106. 24/06/2019 106 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 211 "A198"."SUBNAME" "PARTITION_NAME", "A196"."OBJECT_TYPE" "SEGMENT_TYPE", "A195"."TYPE#" "SEGMENT_TYPE_ID",DECODE(BIT AND("A195"."SPARE1",2097408),2097152,'SECUREFILE',256,'ASSM','MSSM') "SEGMENT_SUBTYPE", "A197"."TS#" "TABLESPACE_ID", "A197"."NAME" "TABLESPACE_NAME", "A197"."BLOCKSIZE" "BLOCKSIZE", "A194"."FILE#" "HEADER_FILE", "A195"."BLOCK#" "HEADER_BLOCK", "A195"."BLOCKS"*"A197"."BLOCKSIZE" "BYTES", "A195"."BLOCKS" "BLOCKS", "A195"."EXTENTS" "EXTENTS", "A195"."INIEXTS"*"A197"."BLOCKSIZE" "INITIAL_EXTENT", "A195"."EXTSIZE"*"A197"."BLOCKSIZE" "NEXT_EXTENT", "A195"."MINEXTS" "MIN_EXTENTS", "A195"."MAXEXTS" "MAX_EXTENTS",DECODE(BITAND("A195"."SPARE1",4194304),4194304, "A195"."BITMAPRANGES",NULL) "MAX_SIZE",TO_CHAR(DECODE( BITAND("A195"."SPARE1",2097152),2097152, DECODE("A195"."LISTS",0,'NONE',1,'AUTO',2,'MIN',3,'MAX',4,'DEFAULT','INVALID'),NULL)) "RETENTION",DECODE(BITAND("A195"."SPARE1",2097152),2097152, "A195"."GROUPS",NULL) "MINRETENTION",DECODE(BITAND("A197"."FLAGS",3),1,TO_NUMBER(NULL), "A195"."EXTPCT") "PCT_INCREASE",DECODE(BITAND("A197"."FLAGS",32),32, TO_NUMBER(NULL),DECODE("A195"."LISTS",0,1, "A195"."LISTS")) "FREELISTS",DECODE(BITAND("A197"."FLAGS",32),32,TO_NUMBER(NULL),DECODE("A195"."GROUPS",0,1, "A195"."GROUPS")) "FREELIST_GROUPS", "A195"."FILE#" "RELATIVE_FNO",BITAND("A195"."CACHEHINT",3) "BUFFER_POOL_ID", BITAND("A195"."CACHEHINT",12)/4 "FLASH_CACHE",BITAND("A195"."CACHEHINT",48)/16 "CELL_FLASH_CACHE", NVL("A195"."SPARE1",0) "SEGMENT_FLAGS",DECODE(BITAND("A195"."SPARE1",1),1, "A195"."HWMINCR", "A198"."DATAOBJ#") "SEGMENT_OBJD" FROM "SYS"."USER$" "A199", "SYS"."OBJ$" "A198", "SYS"."TS$" "A197", ( (SELECT Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 212 DECODE(BITAND("A209"."PROPERTY",8192),8192,'NESTED TABLE','TABLE') "OBJECT_TYPE", 2 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID", "A209"."OBJ#" "OBJECT_ID", "A209"."FILE#" "HEADER_FILE", "A209"."BLOCK#" "HEADER_BLOCK", "A209"."TS#" "TS_NUMBER" FROM "SYS"."TAB$" "A209" WHERE BITAND("A209"."PROPERTY",1024)=0) UNI ON ALL (SELECT 'TABLE PARTITION' "OBJECT_TYPE",19 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID", "A208"."OBJ#" "OBJECT_ID", "A208"."FILE#" "HEADER_FILE", "A208"."BLOCK#" "HEADER_BLOCK", "A208"."TS#" "TS_NUMBER" FROM "SYS"."TABPART$" "A208") UNION ALL (SELECT 'CLUSTER' "OBJECT_TYPE",3 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID", "A207"."OBJ#" "OBJECT_ID", "A207"."FILE#" "HEADER_FILE", "A207"."BLOCK#" "HEADER_BLOCK", "A207"."TS#" "TS_NUMBER" FROM "SYS"."CLU$" "A207") UNION ALL (SELECT DECODE("A206"."TYPE#",8,'LOBINDEX','INDEX') "OBJECT_TYPE",1 "OBJECT_TYPE_ID",6 "SEGMENT_TYPE_ID", "A206"."OBJ#" "OBJECT_ID", "A206"."FILE#" "HEADER_FILE", "A206"."BLOCK#" "HEADER_BLOCK", "A206"."TS#" "TS_NUMBER" FROM "SYS"."IND$" "A206" WHERE "A206"."TYPE#"=1 OR "A206"."TYPE#"=2 OR "A206"."TYPE#"=3 OR "A206"."TYPE#"=4 OR "A206"."TYPE#"=6 OR "A206"."TYPE#"=7 OR "A206"."TYPE#"=8 OR "A206"."TYPE#"=9) UNION ALL (SELECT 'INDEX PARTITION' "OBJECT_TYPE",20 "OBJECT_TYPE_ID",6 "SEGMENT_TYPE_ID", "A205"."OBJ#" "OBJECT_ID", "A205"."FILE#" "HEADER_FILE", "A205"."BLOCK#" "HEADER_BLOCK", 211 212
  • 107. 24/06/2019 107 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 213 "A205"."TS#" "TS_NUMBER" FROM "SYS"."INDPART$" "A205") UNION ALL (SELECT 'LOBSEGMENT' "OBJECT_TYPE",21 "OBJECT_TYPE_ID",8 "SEGMENT_TYPE_ID", "A204"."LOBJ#" "OBJECT_ID", "A204"."FILE#" "HEADER_FILE", "A204"."BLOCK#" "HEADER_BLOCK", "A204"."TS#" "TS_NUMBER" FROM "SYS"."LOB$" "A204" WHERE BITAND("A204"."PROPERTY",64)=0 OR BITAND("A204"."PROPERTY",128)=128) UNION ALL (SELECT 'TABLE SUBPARTITION' "OBJECT_TYPE",34 "OBJECT_TYPE_ID",5 "SEGMENT_TYPE_ID", "A203"."OBJ#" "OBJECT_ID", "A203"."FILE#" "HEADER_FILE", "A203"."BLOCK#" "HEADER_BLOCK", "A203"."TS#" "TS_NUMBER" FROM "SYS"."TABSUBPART$" "A203") UNION ALL (SELECT 'INDEX SUBPARTITION' "OBJECT_TYPE",35 "OBJECT_TYPE_ID",6 "SEGMENT_TYPE_ID", "A202"."OBJ#" "OBJECT_ID", "A202"."FILE#" "HEADER_FILE", "A202"."BLOCK#" "HEADER_BLOCK", "A202"."TS#" "TS_NUMBER" FROM "SYS"."INDSUBPART$" "A202") UNION ALL (SELECT DECODE("A201"."FRAGTYPE$",'P','LOB PARTITION','LOB SUBPARTITION') "OBJECT_TYPE",DECODE("A201"."FRAGTYPE$",'P',40,41) "OBJECT_TYPE_ID",8 "SEGMENT_TYPE_ID", "A201"."FRAGOBJ#" "OBJECT_ID", "A201"."FILE#" "HEADER_FILE", "A201"."BLOCK#" "HEADER_BLOCK", "A201"."TS#" "TS_NUMBER" FROM "SYS"."LOBFRAG$" "A201")) "A196", "SYS"."SEG$" "A195", "SYS"."FILE$" "A194" WHERE "A195"."FILE#"="A196"."HEADER_FILE" AND "A195"."BLOCK#"="A196"."HEADER_BLOCK" AND "A195"."TS#"="A196"."TS_NUMBER" AND "A195"."TS#"="A197"."TS#" AND "A198"."OBJ#"="A196"."OBJECT_ID" AND "A198"."OWNER#"="A199"."USER#"(+) AND "A195"."TYPE#"="A196"."SEGMENT_TYPE_ID" AND "A198"."TYPE#"="A196"."OBJECT_TYPE_ID" AND "A195"."TS#"="A194"."TS#" AND Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 21 ... 214 more ... pages ! 213 214
  • 108. 24/06/2019 108 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 215 back to our problem SQL PreparedStatement stmt = conn.prepareStatement( "select count(*) from vehicles where make = ? and model = ?"); Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 216 2) is it sensible/correct ? 215 216
  • 109. 24/06/2019 109 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 217 SQL> select ... 2 from EMP e, 3 DEPT d 4 where e.JOB = 'SALES' 5 and d.LOC = 'NORTH'; 6 and d.DEPTNO = e.DEPTNO Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 218 SQL> select REGION, min(AMOUNT) 2 from EMP e, 3 SALES_TRANSACTIONS s 4 where e.JOB = 'SALES' 5 and s.EMPNO = e.EMPNO 6 and s.REGION = 'CA' 7 and s.TAX_AMT > 10 8 or s.SUBSIDY > 0 9 group by REGION 7 and s.TAX_AMT > 10 8 or s.SUBSIDY > 0 9 group by REGION ( ) 217 218
  • 110. 24/06/2019 110 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 219 SQL> select * 2 from sales 3 where region in 4 ( select region 5 from promoted_locations 6 where campaign = 'BLACK FRIDAY'); SQL> desc PROMOTED_LOCATIONS Name Null? Type ------------------------ -------- ------------- ID NOT NULL NUMBER CAMPAIGN NOT NULL VARCHAR2(128) PROMOTED_REGION NOT NULL VARCHAR2(24) ... Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 220 3) the execution plan 219 220
  • 111. 24/06/2019 111 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. optimizer got it wrong... 221 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. or 222 221 222
  • 112. 24/06/2019 112 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ... it cannot run better 223 legitimate response Copyright © 2018, Oracle and/or its affiliates. All rights reserved. ------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | ------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1| 5| | 1 | SORT AGGREGATE | | 1| 5| | 2 | TABLE ACCESS BY INDEX ROWID | VEHICLES | 20K| 687K| | 3 | INDEX RANGE SCAN | MAKE_IX | 45K| 592K| ------------------------------------------------------------------ 224 PreparedStatement stmt = conn.prepareStatement( "select count(*) from vehicles where make = ? and model = ?"); 223 224
  • 113. 24/06/2019 113 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 225 now what ? Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 226 for human beings ... 225 226
  • 114. 24/06/2019 114 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 227 ... cardinality is everything Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 228 227 228
  • 115. 24/06/2019 115 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 230 same with the optimizer 229 230
  • 116. 24/06/2019 116 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. understanding cardinality and cost crucial Copyright © 2018, Oracle and/or its affiliates. All rights reserved. summary 231 232
  • 117. 24/06/2019 117 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. avoid parsing costs for high frequency SQL Copyright © 2018, Oracle and/or its affiliates. All rights reserved. use binding and don't get hacked ! 233 234
  • 118. 24/06/2019 118 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. using PLSQL is golden SQL - the features 235 236