More Related Content Similar to Latin America Tour 2019 - slow data and sql processing (20) More from Connor McDonald (19) Latin America Tour 2019 - slow data and sql processing2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Lo siento, no hablo español :-(
3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
But I will do my best to help
Pero haré todo lo posible para ayudar
4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Connor McDonald
9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
10https://asktom.oracle.com
10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
https://asktom.oracle.com/officehours
13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
no matter how many layers ...
13
no importa cuántas capas
14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
somewhere you are doing ...
14
XXXalgun lado tú lo estás haciendo
15. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... data access with SQL
15
acceso a datos con SQL
16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL is like any language
13
SQL es como ninguna idioma
17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile it
14
compilarlo
18. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile it
14
on the fly
sobre la marcha
19. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
run it
15
ejecutarlo
20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
get | store results
16
obtener | almacenar resultados
21. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
for successful applications...
17
para aplicaciones exitosas
22. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile it ... fast
18
rápido
23. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
run it ... fast
19
rápido
24. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
get | store results ... fast
20
rápido
25. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
that is all!
21
¡Eso es!
26. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
what everyone talks about
22
de lo que todos hablan
27. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile
execute
store/fetch results
23
28. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
this session
24
esta sesión
29. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
compile
execute
store/fetch results
25
30. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
before we begin ...
antes de que comencemos
31. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | connor_mc_dconnor mcdonald.com
controversy
31
controversia
32. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
here is the problem...
32
aqui esta el problema
34. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a small digression
una pequeña digresión
35. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a little bit of history on servers ...
un poco de historia en los servidores
36. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(not too) long ago
no hace mucho
38. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
FSB = "front side bus"
38
39. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
any access to ... anything
cualquier acceso a ... cualquier cosa
42. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
hypertransport
44. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
memory access direct from CPU
44
acceso a memoria directo desde la CPU
47. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"why do I care?"
"¿Porqué me importa?"
48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
you can't blame the server (anymore)
48
ya no puedes culpar al servidor
49. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
if you can't get that performance ...
si no puedes obtener ese rendimiento
50. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... it is our fault
está nuestro culpa
54. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
good code
buen código
55. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
so what's holding us back ?
¿qué nos está frenando?
56. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
It all comes down to...
Todo se reduce a
57. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
too much work or ...
demasiado trabajo
58. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... not being able to do work
58
no poder hacer el trabajo
59. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
PART #1
56
compiling SQL
60. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... how to avoid work when processing SQL
cómo evitar el trabajo excesivo al procesar SQL
61. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
terminology
61
63. 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;
64. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all of them !
todos ellos !
66. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
open
process
close
give me some memory
do some stuff using that memory,
maybe access other memory
here's your memory back
66
Dame un poco de memoria
usa la memoria
devolver la memoria
68. a quick primer on (database) memory
una cartilla rápida en memoria
75. 71
limited resource
lots of people want it
concurrent access causes problems
it's a complex system
recurso limitado
mucha gente lo quiere
el acceso concurrente causa problemas
es un systema complejo
90. 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
99. Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
DONEno logras nada
100. "When are we talking about SQL?"
"¿Cuándo estamos hablando de SQL?
101. to run a SQL statement
para ejecutar una declaración SQ
113. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
imposible de evitar?
114. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
library cache
115. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
previously executed statements
120
declaraciones ejecutadas previamente
116. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement select * from emp where empno = 123
117. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement
already in library cache ?
select * from emp where empno = 123
118. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement
already in library cache ?
select * from emp where empno = 123
syntactically, semantics OK
119. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
parse statement
already in library cache ?
reuse optimizer info
select * from emp where empno = 123
syntactically, semantics OK
120. 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
121. 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
122. 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 ?
probabilidad de reutilización baja?
123. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
some queries are “nearly the same”
128
select surname, firstname from emp where empno = 123
select surname, firstname from emp where empno = 456
algunas consultas son "casi iguales"
125. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
binding
parse this...
select surname, firstname from emp where empno = ?
126. 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 = ?
127. 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
137. 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%'
138. 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 ...
145. it takes 5 minutes to hack you
se necesita 5 minutos
149. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
while we are talking about binding
mientras hablamos de encuadernación
150. 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!
???
???
151. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
still some parsing to do
156
152. 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
153. 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
154. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
scott@mydb> select *
2 from EMP
3 where EMPNO = :b1
anna@mydb> select *
2 from EMP
3 where EMPNO = :b1
SQL> desc anna.emp
Name Null? Type
----------------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
155. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(even soft) parsing =
memory access =
latching
156. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
can we do better ?
podemos hacerlo mejor?
157. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
recall
recuerda
158. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
open
process
close
give me some memory
do some stuff using that memory,
maybe access other memory
here's your memory back
163
Dame un poco de memoria
usa la memoria
devolver la memoria
159. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your SQL is always shareable
library cache
select *
from emp
tu SQL siempre se puede compartir
160. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your cursor points to it
library cache
select *
from emp
cursor
165
161. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
close cursor = lose pointer
library cache
select *
from emp
cursor cercano = perder puntero
cursor
162. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
next time we parse
soft parse
la próxima vez
163. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
168
imposible de evitar?
164. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
what if we don't close the cursor
Y si nos no lo hagas cierra el cursor
165. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
library cache
select *
from emp
cursor 1
select *
from dept
cursor 2
select *
from emp
where ...
cursor 3
166. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
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");
167. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
emplist(String[] args) {
if "first time" {
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");
168. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
don’t close the cursor
no cierres el cursor
169. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
hang on to the memory
mantener la memoria
170. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
remember what cursors we’ve used
recuerda qué cursores hemos usado
171. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
reuse whenever possible
reutilizar siempre que sea posible
172. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
don’t exhaust memory
no agotar la memoria
173. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
don’t allow invalid cursor reuse
no permitir la reutilización inválida del cursor
174. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
sounds complicated ...
Suena complicado
175. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a free solution exists
gratis la solución
177. 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;
178. 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)
179. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better cursor caching
mejor almacenamiento en caché del cursor
180. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
procedure MY_PROC is
begin
select *
into …
from dept
where …
end;
select * from dept …
181. 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 …
182. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
plsql in the database
183. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
171
PART #2
get | store results
185. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
start with reading data
173
Empezar con leyendo datos
186. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
querying data
187. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
data is stored in blocks | pages
los datos se almacenan en bloques | paginas
188. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
to read data, you read blocks...
para leer datos, lees bloques
189. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
SQL> select *
2 from EMP
3 where ...
/u01/my_data1.dat
memory
190. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
but (database) memory access is complex
195
pero el acceso a la memoria es complejo
191. 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 /
192. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
we have to latch !
193. 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"
198
Obtener el pestillo
Buscar en la lista de bloques en la memoria
Leer bloquear
Extraer fila de interés
Liberar el pestillo
Devolver la fila al cliente
194. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
lots of rows
muchas filas
195. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
lots of latching
196. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
typical program
197. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
rs = stmt.executeQuery("...");
while(rs.next()) {
v1 = rs.getInt(1);
v2 = rs.getString(2);
...
}
202
198. 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
203
199. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a better strategy.... “pinning”
una mejor estrategia
200. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"fetch a row"
201. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"fetch a row"
“and .. I may need
many rows from this
block”
Puedo necesitar muchos
filas de esto bloquear"
202. 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 3 back to client
…
•Give row n to client
•Get latch
•Walk along list
•Remove my pin on the block
•Release latch
“and .. I may need
many rows from this
block”
203. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
how do you say ?
“I may want many rows”
Cómo se dice ?
"Puedo querer muchas filas"
204. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
responsibility of the client
responsabilidad del cliente
209. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
190
FetchDemo nn
210. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
focus on blocks
191
211. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
focus on roundtrips
192
212. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
nothing in life is free !
192
nada en la vida es gratis
213. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all that data goes somewhere
220
todos esos datos van algun lado
214. 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
215. 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
216. 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
217. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
500m of memory !
218. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
pin rows on a block (8k)
225
219. 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
220. 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
221. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
PART #2(a)
storing data
222. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
the same rules apply
205
se aplican las mismas reglas
223. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
think blocks not rows
206
when appropriate
pensar bloques no filas
224. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
demo
208
InsertDemo3
InsertDemo4 nn
226. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
avoid parsing costs
for high frequency SQL
evitar costos de análisis
para SQL de alta frecuencia
227. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
use binding and don't get hacked !
¡usa el enlace y no te hackeen!
228. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
PLSQL in the database is golden
PLSQL en la base de datos es dorado