Upgrade to Pro — share decks privately, control downloads, hide ads and more …

APACOUC successful applications on Autonomous

APACOUC successful applications on Autonomous

Well done! You've come up with the killer idea for 2021. You've got the best UI design anyone has ever seen! Your modern application ticks all the boxes - serverless, functional, Kubernetes, microservices, API-based, the list goes on. It runs on every OS and every type of device. But unfortunately, all of this counts for absolutely NOTHING if your data access is slow or buggy. But an Autonomous database will fix all that right? Only if you understand the fundamentals of how SQL is processed by the database. For novice developers, SQL can be hard to understand and sometimes totally hidden from view under an ORM. Let's peel back the covers to show how SQL is processed, how to avoid getting hacked, and how to get data back to your application in a snappy fashion

Avatar for Connor McDonald

Connor McDonald

November 01, 2020

More Decks by Connor McDonald

Other Decks in Technology

Transcript

  1. 7

  2. 38 "The sooner your company admits that you are not

    Google, the sooner you can get down to some real work" - Ted Dziuba
  3. 61 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;
  4. 64 open process close give me some memory do some

    stuff using that memory, maybe access other memory here's your memory back
  5. 67

  6. 70

  7. 73 limited resource lots of people want it concurrent access

    causes problems it's a complex system
  8. 85 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 ?
  9. 87

  10. YOU

  11. GET

  12. 102 Syntax Semantic Optimization Rowsourcing Execution (Fetch) SQL> select *

    2 frmo emp; frmo emp * ERROR at line 2: ORA-00923: FROM keyword not found where expected
  13. 103 Syntax Semantic Optimization Rowsourcing Execution (Fetch) SQL> select empnoo

    2 from emp; select empnoo * ERROR at line 1: ORA-00904: invalid column name
  14. 104 PLAN ------------------------------------- SELECT STATEMENT TABLE ACCESS BY INDEX ROWID

    EMP INDEX RANGE SCAN EMP_PK Syntax Semantic Optimization Rowsourcing Execution (Fetch)
  15. 116 parse statement already in library cache ? reuse optimizer

    info reuse row source info select * from emp where empno = 123 syntax, semantics OK
  16. 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
  17. 118 select surname, firstname from emp where empno = 123

    select * from dept where dname = 'SALES' probability of reuse low ?
  18. 119 many queries are “nearly the same” select surname, firstname

    from emp where empno = 123 select surname, firstname from emp where empno = 456
  19. 121 binding parse this... now run it with ? =

    123 now run it with ? = 456 select surname, firstname from emp where empno = ?
  20. 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
  21. 127

  22. 130 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 ...
  23. 135

  24. 140

  25. 141

  26. 142

  27. 143

  28. 144

  29. 147

  30. parse statement already in library cache ? reuse optimizer info

    reuse row source info select * from emp where empno = :bind syntax, semantics OK Yes! Yes! ??? ???
  31. scott@mydb> select * 2 from EMP 3 where EMPNO =

    :b1 mike@mydb> select * 2 from EMP 3 where EMPNO = :b1
  32. 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)
  33. open process close I need some memory do some stuff

    using that memory here's your memory back
  34. library cache select * from emp select * from dept

    select * from emp where ... cursor 1 cursor 2 cursor 3
  35. library cache select * from emp select * from dept

    select * from emp where ... pstmt_all_emp = con.prepareStatement("select * from emp"); cursor 1 cursor 2 cursor 3
  36. 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");
  37. create procedure MY_PROC is begin select empno, ename, ... into

    … from emp where empno = my_plsql_variable ... end;
  38. 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 ...
  39. 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 /
  40. • 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"
  41. 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
  42. "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”
  43. 213 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
  44. 214 SQL> declare . . . 9 begin 10 open

    c; 11 12 fetch c 13 bulk collect 14 into r; 15 . . . 19 / Elapsed: 00:00:08.93
  45. 215 SQL> select * from v$mystats 2 where name like

    '%pga%' 3 / NAME VALUE ------------------------------ ---------- session pga memory 501534672 session pga memory max 501534672
  46. 218 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
  47. 219 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
  48. 220 SQL> declare . . . 9 begin 10 for

    i in c loop 12 ... 16 end loop; 17 end; 18 / Elapsed: 00:00:08.21