Two of the critical requirements of a database are:
- run fast
- data integrity
The database can achieve these things, but only as long as you understand the mechanisms correctly. If you don't, then things can go downhill fast.
6. Copyright Š 2018, Oracle and/or its affiliates. All rights reserved. |
Stuff
youtube bit.ly/youtube-connor
blog bit.ly/blog-connor
twitter bit.ly/twitter-connor
400+ posts mainly on database & development
250 technical videos, new uploads every week
rants and raves on tech and the world :-)
7. Copyright Š 2018, Oracle and/or its affiliates. All rights reserved.
etc...
facebook bit.ly/facebook-connor
linkedin bit.ly/linkedin-connor
instagram bit.ly/instagram-connor
slideshare bit.ly/slideshare-connor
16. 16
developer
team lead
DBA
proj mgr
director
CIO
CEO
âhmmm, my code has a fatal catastrophic bugâ
âoh my god! All of the data is corruptâ
âweâve got some data corruptionâ
âsome small issues with data qualityâ
âmoving forward with data challengesâ
âmaximising opportunities in data spaceâ
âwe pride ourselves on perfect dataâ
46. 46
PROCEDURE string_check (p_text_in varchar2,p_ret out number) is
char_list varchar2(255);
v_single_char varchar2(1);
v_in_string number;
cursor c1(m number) is
select substr(char_list,m,1) from dual;
cursor c2(v varchar2) is
select instr(p_text_in,v) from dual;
BEGIN
p_ret := 0;
for i in 1 .. 45 loop
char_list := char_list || chr(i);
end loop;
char_list := char_list || chr(47);
for i in 58 .. 255 loop
char_list := char_list || chr(i);
end loop;
for i in 1 .. length(char_list) loop
open c1(i);
fetch c1 into v_single_char;
close c1;
open c2(v_single_char);
fetch c2 into v_in_string;
close c2;
if v_in_string > 0 then
p_ret := 1;
end if;
end loop;
END;
62. 62
procedure STRING_CHECK(p_text_in varchar2, p_ret out number) is
x number;
begin
x := to_number(p_text_in);
p_ret := 0;
exception
when others then p_ret := 1;
end;
no logical I/O
more accurate
78. SQL> create or replace
2 trigger WHO_DID_THIS_ROW
3 before insert or update
4 on PEOPLE
5 for each row
6 begin
7 if inserting then :new.created_by := user; end if;
8 :new.modified_by := user;
9 end;
10 /
Trigger created.
82. SQL> create or replace
2 trigger WHO_DID_THIS_ROW
3 before insert or update
4 on PEOPLE
5 for each row
6 begin
7 if inserting then
8 :new.created_by := sys_context('USERENV','SESSION_USER');
9 end if;
10 :new.modified_by := sys_context('USERENV','SESSION_USER') ;
11 end;
12 /
Trigger created.
83. insert into people
select rownum,'x','y',null,null
from dba_objects
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.02 0 0 0 0
Execute 1 0.67 0.67 0 26864 2646 78703
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 0.67 0.69 0 26864 2646 78703
100. 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
111. 111
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;
every time we
create/change a
product...
...make sure the
manufacturer that
is provided already
exists
112. 112
create or replace
trigger MAN_PROD_CHECK
before delete on MANUFACTURER
for each row
declare
l_prd_cnt number;
begin
select count(*)
into l_prd_cnt
from product
where man_id = :old.man_id;
if l_prd_cnt > 0 then
raise_application_error(-20001,
'Manufacturer in use by product records');
end if;
end;
before you can
delete a manufacturer...
...no products
can belong to it
114. 114
SQL> insert into PRODUCT (...,MAN_ID,...) values (...,100, ...);
1 row created.
SQL> insert into PRODUCT (...,MAN_ID,...) values (...,101,...);
ERROR at line 1:
ORA-20000: Manufacturer is not valid
SQL> delete from MANUFACTURER where man_id = 5768;
ERROR at line 1:
ORA-20001: Manufacturer in use by product records
137. 137
the "dreaded" module specification
"When creating a product...
validate registration date â ensure not in the future,
validate âŚ
validate âŚ
validate manufacturer ID â must exist in Manufacturer table"
152. select prod_id, max(amount)
from stores st,
customers c,
sales s
where s.cust_id = c.cust_id(+)
and c.store_id = st.store_id
and s.amount > 10
group by prod_id
152
hash outer join ? nested loop ?
STORES first ?
sort merge ?
173. 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 ?
173
194. ⢠Get latch
⢠Search along list of blocks in memory (or "chain")
⢠Read block
⢠Extract row of interest
⢠Release latch
⢠Give row back to client
"give me a row"
194
198. OPEN c_emp_list
LOOP
FETCH c_emp_list INTO ...
insert into EMP_COPY
( . . . )
values
( . . . );
END LOOP;
⢠Get latch
⢠Walk along chain
⢠Get block
⢠Extract single row
⢠Release latch
⢠Give row back to client
⢠Get latch (again)
⢠Walk along chain (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 chain (again)
⢠Get block (again)
⢠Extract single row
⢠Release latch (again)
⢠Give row back to client (again)
Row #3
198
200. "give me a row"
⢠Get latch
⢠Walk along chain
⢠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 chain
⢠Remove my pin on the block
⢠Release latch
âby the way, Iâll be
wanting several
rows from this
blockâ
207. SQL> declare
2 n number;
3 d date;
4 cursor c is
5 select empno, hiredate
6 from big_table;
7 begin
8 open c;
9 loop
10 fetch c into n,d;
11 exit when c%notfound;
12 end loop;
13 close c;
14 end;
15 /
Elapsed: 00:01:15.06
1,000,000 rows
208. SQL> declare
2 cursor c is
3 select empno, hiredate
4 from big_table;
5 type c_list is table of c%rowtype;
6 r c_list;
7 begin
8 open c;
9 fetch c
10 bulk collect into r;
11 close c;
12 end;
13 /
Elapsed: 00:00:10.02
210. SQL> select * from v$mystats
2 where name like '%pga%'
3 /
NAME VALUE
------------------------------ ----------
session pga memory 101534672
session pga memory max 101534672
217. SQL> begin
2 for i in 1 .. 200000 loop
3 insert into t1 values ( i );
4 end loop;
5 end;
6 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:11.03
217
218. SQL> declare
2 type num_list is table of number;
3 n num_list := num_list();
4 begin
5 n.extend(200000);
6 for i in 1 .. 200000 loop
7 n(i) := i;
8 end loop;
9 forall i in 1 .. 200000
10 insert into t1 values ( n(i) );
11 end;
12 /
Elapsed: 00:00:00.56
228. I love your feedback!
Rate your trip with Connor
229. Follow and subscribe
youtube bit.ly/youtube-connor
blog bit.ly/blog-connor
twitter bit.ly/twitter-connor
400+ posts mainly on database & development
250 technical videos, new uploads every week
rants and raves on tech and the world :-)