cost based query optimization christoph koch computing
play

Cost-based Query Optimization Christoph Koch Computing the cost of - PowerPoint PPT Presentation

Cost-based Query Optimization Christoph Koch Computing the cost of a query plan We know how to estimate the cost of each individual operator. But for doing this we need to know the size of the input. Compute the size of each relation


  1. Cost-based Query Optimization Christoph Koch

  2. Computing the cost of a query plan � We know how to estimate the cost of each individual operator. � But for doing this we need to know the size of the input. � Compute the size of each relation produced by some operator � Bottom-up � We need to know estimates of selectivities/reduction factors for both selection and join conditions. � Given a fixed query plan, if we exchange a particular operator implementation (e.g. NL join against Hashjoin), the output does not change and we do not have to recompute output sizes. 2

  3. Model of Query plans � We use a very powerful model. � Ingredients: � Algebra tree � For each operation, a choice of implementation � For some binary operations (e.g. NL joins), an annotation saying which of the two inputs is the outer and which is the inner loop. � A choice of whether an operator’s output is to be written back to disk or pipelined into the next operator (sometimes there is no choice). � An allocation of memory buffer pages to operators and their input/output lines. � In case of pipelining, the allocation may not be operator by operator but span several operations. 3

  4. A Remark on the cost function for block NL joins � On this slides I use the formula pages_outer + round_up(pages_outer / (buffer_pages – 1)) * (pages_inner - 1) + 1 � I explained this formula in class but it’s not in the book. � You can use the formula from the book instead: pages_outer + round_up(pages_outer / (buffer_pages – 1)) * pages_inner 4

  5. A University Database Professor (p) id name grad room 2125 Socrates Full 226 Takes (h) 2126 Russel Full 232 sid cid Course (v) 2127 Kopernikus Assoc 310 26120 5001 cid name credits taught_by 2133 Popper Assoc 52 27550 5001 5001 Foundations 4 2137 2134 Augustinus Assoc 309 27550 4052 5041 Ethics 4 2125 2136 Curie Full 36 28106 5041 5043 Epistemology 3 2126 2137 Kant Full 7 28106 5052 5049 Maieutics 2 2125 28106 5216 4052 Logics 4 2125 Student (s) 28106 5259 5052 Theory of Science 3 2126 sid name semester 29120 5001 5216 Bioethics 2 2126 24002 Xenokrates 18 29120 5041 5259 The Vienna Circle 2 2133 25403 Jonas 12 29120 5049 5022 CS432 2 2134 26120 Fichte 10 29555 5022 4630 The three critiques 4 2137 26830 Aristoxenos 8 25403 5022 27550 Schopenhauer 6 28106 Carnap 3 29120 Theophrastos 2 29555 Feuerbach 2 5

  6. Assumptions #tuples per page: � Relation sizes (# tuples) � |p|= 800 p: b 1024/50 c =20 � |s|= 38000 s: b 1024/50 c = 20 � |v|= 2000 � |h|= 60000 v: b 1024/100 c = 10 � Avg. Tuple size h: b 1024/16 c = 64 � p: 50 Bytes � s: 50 Bytes (page overhead is ignored) � v: 100 Bytes � h: 16 Bytes � selectivities #pages: � Sel[sh] = 2.6 * 10^ -5 p: 800/20 = 40 � Sel[hv] = 5 * 10^ -4 � Sel[vp] = 1.25 * 10^ -3 s: 38000/20 = 1900 � Sel[p.Name= ...] = 1.25 * 10^ -3 v: 2000/10 = 200 � Page size 1024 Bytes � Main memory buffers m = 20+ 1 pages h: d 60000/64 e = 938 (we ignore the overhead of the primary index.) 6

  7. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h 7

  8. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h 8

  9. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h 9

  10. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h 10

  11. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h 11

  12. π s.semester (with duplicates) Example 1 Join p.id=v.taught_by Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) Join s.sid=h.sid v s h And now we have to allocate memory buffers... 12

  13. π s.semester (with duplicates) Example 1a pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 9 Sel p.name=´Socrates´ 1 (inner: recompute!) pipl. Access(p) NL-Join s.sid=h.sid v 1 9 s h Buffer assignments in this color 13

  14. π s.semester (with duplicates) Example 1a pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 9 Sel p.name=´Socrates´ 1 (inner: recompute!) pipl. Access(p) NL-Join s.sid=h.sid v 1 9 s h 14

  15. π s.semester (with duplicates) Example 1a NL-Join p.id=v.taught_by NL-Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) NL-Join s.sid=h.sid v s h 15

  16. π s.semester (with duplicates) Example 1b pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 9 1 Sel p.name=´Socrates´ pipl. Access(p) NL-Join s.sid=h.sid v 1 9 s h Buffer assignments in this color 16

  17. π s.semester (with duplicates) Example 1b pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 9 1 Sel p.name=´Socrates´ pipl. Access(p) NL-Join s.sid=h.sid v 1 9 s h 17

  18. π s.semester (with duplicates) Example 1b NL-Join p.id=v.taught_by NL-Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) NL-Join s.sid=h.sid v s h 18

  19. π s.semester (with duplicates) Example 1c pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 1 18 Sel p.name=´Socrates´ Store intermediate pipl. result 1 Access(p) NL-Join s.sid=h.sid v 1 19 s h Buffer assignments in this color 19

  20. π s.semester (with duplicates) Example 1c pipl. 0 NL-Join p.id=v.taught_by 0 1 pipl. 0 NL-Join v.cid=h.cid 1 18 Sel p.name=´Socrates´ pipl. 1 Access(p) NL-Join s.sid=h.sid v 1 19 s h 20

  21. π s.semester (with duplicates) Example 1c NL-Join p.id=v.taught_by NL-Join v.cid=h.cid Sel p.name=´Socrates´ Access(p) NL-Join s.sid=h.sid v s h 21

  22. π s.semester Example 2 pipl. NL-Join s.sid=h.sid 5 0 s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 22

  23. π s.semester Example 2 pipl. NL-Join s.sid=h.sid (Same query, different 5 0 Join order) s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 23

  24. π s.semester Example 2 pipl. NL-Join s.sid=h.sid 5 0 s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 24

  25. π s.semester Example 2 pipl. NL-Join s.sid=h.sid 5 0 s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 25

  26. π s.semester Example 2 pipl. NL-Join s.sid=h.sid 5 0 s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 26

  27. π s.semester Example 2 pipl. NL-Join s.sid=h.sid 5 0 s NL-Join v.cid=h.cid 1 0 NL-Join p.id=v.taught_by 1 0 h Sel p.name=´Socrates´ v pipl. Access(p) 27

  28. Example 3a π s.semester Index-Join s.sid=h.sid Merge-Join v.cid=h.cid s Sort v.cid Sort h.cid Index-Join p.id=v.taught_by h Sel p.name=´Socrates´ v Access(p) taught_by 28

  29. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 29

  30. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 30

  31. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 31

  32. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 32

  33. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 33

  34. Example 3a pipl. 18 pipl. π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 34

  35. Example 3a pipl. 18 pipl. π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 35

  36. Example 3a π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 36

  37. IndexDup Example 3b pipl. 3 Hash pipl. π s.semester pipl. Index-Join s.sid=h.sid e.g. 2 5 Merge-Join v.cid=h.cid s 0 Sort v.cid Sort h.cid 1 20+1 pipl. Index-Join p.id=v.taught_by h 1 Sel p.name=´Socrates´ v pipl. Access(p) taught_by 37

Recommend


More recommend