Weitere ähnliche Inhalte Ähnlich wie APEX Connect 2019 - successful application development (20) Mehr von Connor McDonald (20) Kürzlich hochgeladen (20) APEX Connect 2019 - successful application development3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Connor McDonald
8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
8https://asktom.oracle.com
9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
https://asktom.oracle.com/officehours
10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
200 hours free access so far
10
11. 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
12. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
before we begin
13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
recommended reading
14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
docs.oracle.com
16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Concepts guide
http://bit.ly/oraconcepts
17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better than lots of current people
18. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Application Developers guide
http://bit.ly/oradevdoc
19. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better than lots of current people
20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Part 1
SQL processing
21. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
some controversy to start...
22. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
here's the problem...
22
24. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a little digression on servers
25. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a little bit of history ...
26. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(not too) long ago
28. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
FSB = "front side bus"
28
29. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
any access to ... anything
33. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
hypertransport
35. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
memory access direct from CPU
35
38. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
"why do I care?"
39. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
you can't blame the server
39
40. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
if you can't get that performance ...
41. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... it's you
46. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all have their place...
47. 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
47
48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
so what's holding you back ?
49. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
It all comes down to...
50. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
too much work or ...
51. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... not being able to do work
51
52. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
this part ...
53. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
... how to avoid work when processing SQL
54. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
terminology
54
55. 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;
56. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
all of them !
58. 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
58
76. 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 ?
76
85. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
its importance to Oracle
86. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
early Oracle
87. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
2000 times !
(_spin_count)
87
88. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
Zzzzzzzzzz....
89. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
nowadays...
yield
mutex
posting
Good reading: http://andreynikolaev.wordpress.com/
CAS
102. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
103. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
library cache
104. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
previously executed statements
104
105. 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
106. 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
107. 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 ?
108. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
some queries are “nearly the same”
108
select surname, firstname from emp where empno = 123
select surname, firstname from emp where empno = 456
109. 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 = ?
110. 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
115. 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
125. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
while we are talking binding
126. 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!
???
???
127. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
still some parsing to do
127
128. 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
129. 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)
130. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
(even soft) parsing =
memory access =
latching
131. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
can we do better ?
133. 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
133
134. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your SQL is always shareable
library cache
select *
from emp
135. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
your cursor points to it
library cache
select *
from emp
cursor
135
136. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
close cursor = lose pointer
library cache
select *
from emp
cursor
137. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
next time we parse
soft parse
138. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
impossible to avoid ?
138
139. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
what if we don't close the cursor
140. 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");
141. 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
142. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
sounds complicated ...
143. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
a free solution exists
145. 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;
146. 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)
147. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
better cursor caching
148. 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 …
150. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
avoid parsing costs
for high frequency SQL
152. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
using PLSQL is gold
153. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
if time permits ....
156. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
156
"Readers don’t block writers"
"Writers don't block readers"
157. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
157
Maximum concurrency…
… very few locking worries
158. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
158
lack of understanding
160. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
160
some revision / fundamentals
161. SQL> update EMP
2 set SAL = SAL * 1.10
3 /
14 rows updated.
SQL> rollback;
Rollback complete.
how ?
161
163. SQL> update EMP
2 set SAL = SAL * 1.10
3 / undo header
smith $1,0001
block 2754
jones $9002
brown $1,5003
wells $2,0004
wilson $1,0005
block 2754, row 1,
sal $1000
block 2754, row 2,
sal $900
block 2754, row 3,
sal $1500
block 2754, row 4,
sal $2000
uba 5
5
$1,100
$990
$1,650
$2,200
uba 5.1uba 5.2
163
165. SQL> rollback;
undo header
smith $1,0001
block 2754
jones $9002
brown $1,5003
wells $2,0004
wilson $1,0005
block 2754, row 1,
sal $1000
block 2754, row 2,
sal $900
block 2754, row 3,
sal $1500
block 2754, row 4,
sal $2000
uba 5
5
$1,100
$990
$1,650
$2,200
uba 5.1uba 5.2
166. we can use this stuff for queries!
(Oracle version 4)
166
168. select * from emp
where hiredate > '01/01/2004'
update emp set …
where empno = 1234;
Block 3217
"I need block 3217…
… as it was at 9:00"
9:00
9:03
9:05
session 1 session 2
...and time = scn
173. Session 1
Request
Block 3217,
SCN 4567192
Block 3217,
SCN 4567234
"ABC"
Block 3217,
SCN 4567003,
change ABC => XYZ
undo segment block(s)
No good..too new
take a copy of the block
Locate
apply undo
Block 3217,
SCN 4567234
"ABC"
Block 3217,
SCN 4567234
"ABC"
Block 3217,
SCN 4567003
"XYZ"
Block 3217,
SCN 4567003
"XYZ"
Session 2
update emp
set ename = "ABC"
where empno = 1234;
commit;
Done !
173
183. create or replace
trigger PROD_MAN_CHECK
before insert or update on PRODUCT
for each row
declare
l_man_id number;
begin
select man_id
into l_man_id
from manufacturer
where man_id = :new.man_id;
exception
when no_data_found then
raise_application_error(-20000,
'Manufacturer is not valid');
end;
184. create or replace
trigger MAN_PROD_CHECK
before delete on MANUFACTURERS
for each row
declare
l_prd_cnt number;
begin
select count(*)
into l_prd_cnt
from product
where prd_id = :old.prd_id;
if l_prd_cnt > 0 then
raise_application_error(-20001,
'Manufacturer in use by product records');
end if;
end;
186. SQL> insert into PRODUCT (...,MAN_ID,...)
2 values (...,100, ...);
1 row created.
SQL> insert into PRODUCT (...,MAN_ID,...)
2 values (...,101,...);
ERROR at line 1:
ORA-20000: Manufacturer is not valid
SQL> delete from MANUFACTURER
2 where man_id = 5768;
ERROR at line 1:
ORA-20001: Manufacturer in use by product records
189. SQL> select count(*)
2 from
3 ( select man_id from PRODUCT
4 minus
5 select man_id from MANUFACTURER )
6 /
COUNT(*)
----------
86
189
191. Time
create PRD with MAN=7
Trigger fires
- does MAN=7 exist?
- "Yes"
- OK
delete MAN=7
Trigger fires
- any PRD with MAN=7
- "No" (not yet)
- OK
commit
commit
194. beware the module specification
"When creating a product...
validate registration date – ensure not in the future,
validate …
validate …
validate manufacturer ID – must exist in Manufacturer table"
197. “All that stuff is a DBA issue,
I just want my code to run fast”
demo3
198. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
summary revisited
199. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
use binding for SQL
200. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
use SQL wherever possible
201. Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
use PLSQL to run your SQL