1
Connor McDonald
bit.ly/techguysong
The fundamentals of SQL processing
Connor McDonald
Database Advocate
Copyright Š 2019 Oracle and/or its affiliates.
Slower and less secure in 45 minutes
Connor McDonald
Database Advocate
Copyright Š 2019 Oracle and/or its affiliates.
4 4
5 5
7
Me
youtube bit.ly/youtube-connor
blog connor-mcdonald.com
twitter @connor_mc_d
400+ posts mainly on database & development
250 technical videos, new uploads every week
rants and raves on tech and the world :-)
8
etc...
facebook bit.ly/facebook-connor
linkedin bit.ly/linkedin-connor
instagram bit.ly/instagram-connor
slideshare bit.ly/slideshare-connor
10
SQL is like any language
11
compile it
12
compile it
on the fly
13
run it
14
get | store results
15
for successful applications...
16
compile it ... fast
17
run it ... fast
18
get | store results ... fast
19
that's it!
20
what everyone talks about
21
compile
execute
store/fetch results
22
this session
23
compile
execute
store/fetch results
24
before we begin ...
25
some controversy to start
26
here's the problem...
27
... it's us
28
a small digression
29
a little bit of history on servers ...
30
(not too) long ago
32
FSB = "front side bus"
33
any access to ... anything
34
CPU capped
36
hypertransport
memory access direct from CPU
~2007
41
"why do I care?"
42
you can't blame the server (anymore)
44
... it's us
pkdemo.cmd
50
so what's holding us back ?
52
It all comes down to...
53
too much work or ...
54
... not being able to do work
PART #1
COMPILING SQL
58
terminology
59
cursorsdeclare
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;
60
all of them!
62
open
process
close
gimme some memory
do some stuff using that memory,
maybe access other memory
here's your memory back
63
memory access = controlled access
64
a quick primer on (database) memory
65
66
metaphor
67 67
68
71
limited resource
lots of people want it
concurrent access causes problems
it's a complex system
73
same with memory
74
SGA
75
SGA
protected by
76
SGA
protected by
1) get latch
77
SGA
protected by
2) access memory
78
SGA
protected by
3) release latch
79
latch contention
80
someone must wait ...
SGA
protected by
81
active wait
82
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 ?
84
85
latch contention....
86
hurts CPU...
87
hurts concurrency
YOU
GET
NOTHING
DONE
97
"Errrr.... weren't we talking SQL?"
98
to run a SQL statement
99
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
100
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
SQL> select empnoo
2 from emp;
select empnoo
*
ERROR at line 1:
ORA-00904: invalid column name
101
PLAN
-------------------------------------
SELECT STATEMENT
TABLE ACCESS BY INDEX ROWID EMP
INDEX RANGE SCAN EMP_PK
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
102
EMP_PK EMP
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
103
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
104
Syntax
Validity
Optimization
Rowsourcing
Execution
(Fetch)
105
lots of preliminaries
106
parsing
107
lots of memory access
108
lots of latching !
109
impossible to avoid ?
110
two mechanisms
111
1) library cache
112
previously executed statements
113
parse statement
already in library cache ?
reuse optimizer info
reuse row source info
select * from emp where empno = 123
syntactically, semantics OK
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
115
select surname, firstname from emp where empno = 123
select * from dept where dname = 'SALES'
probability of reuse low ?
116
many queries are “nearly the same”
select surname, firstname from emp where empno = 123
select surname, firstname from emp where empno = 456
117
2) binding
118
binding
parse this...
now run it with ? = 123
now run it with ? = 456
select surname, firstname from emp where empno = ?
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
120
demo
ParseDemo
ParseDemoBind
121
"performance still looks ok"
122
let's make it real
ParseDemo2 nn
ParseDemo2Bind nn
123
much more serious
124
125
building SQL by concatenation
126
you'll get hacked
127
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 ...
129
#1 hacking app .... Google
132
it takes 5 minutes to hack you
133
PART #2
get | store results
136
start with reading data
137
querying data
138
data is stored in blocks | pages
139
to read data, you read blocks...
SQL> select *
2 from EMP
3 where ...
/u01/my_data1.dat
memory
141
but (database) memory access is complex
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 /
143
we have to latch !
• 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"
145
lots of rows
146
lots of latching
147
typical program
rs = stmt.executeQuery("...");
while(rs.next()) {
v1 = rs.getInt(1);
v2 = rs.getString(2);
...
}
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
a better strategy.... “pinning”
"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 3 back to client
…
• Give row n to client
• Get latch
• Walk along list
• Remove my pin on the block
• Release latch
“and ... I will need
several rows from
this block”
152
how do you say
“I may want many rows” ?
153
responsibility of the client
FetchDemo nn
159
focus on blocks
160
focus on roundtrips
163
all that data goes somewhere
164
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
165
SQL> declare
. . .
9 begin
10 open c;
11
12 fetch c
13 bulk collect
14 into r;
15
. . .
19 /
Elapsed: 00:00:08.93
166
SQL> select * from v$mystats
2 where name like '%pga%'
3 /
NAME VALUE
------------------------------ ----------
session pga memory 501534672
session pga memory max 501534672
167
500m of memory !
168
law of diminishing returns
169
pin rows on a block (8k)
170
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
171
SQL> declare
. . .
9 begin
10 for i in c loop
12 ...
16 end loop;
17 end;
18 /
Elapsed: 00:00:08.21
PART #2(a)
storing data
174
the same rules apply
175
think blocks not rows
when appropriate
177
demo
InsertDemo3
InsertDemo4 nn
summary
179
for slow and insecure applications...
180
no binding, no SQL reuse, no pinning
181
for fast, secure SQL ...
182
... hence fast, secure applications
183
... always bind user input
184
... use pinning/array processing
186
Thank you
youtube bit.ly/youtube-connor
blog bit.ly/blog-connor
twitter bit.ly/twitter-connor

Sangam 19 - Successful Applications on Autonomous