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

The Last Gedi

DouEnergy
October 01, 2022
39

The Last Gedi

2022 COSCUP

DouEnergy

October 01, 2022
Tweet

Transcript

  1. 1

  2. • SQL 為什麼可以優化 ? • SQL 為什麼需要優化? • 優化 SQL

    有多困難? • Postgres 怎麼優化 SQL ? • 下個世代可能的優化技術是什麼? 2
  3. Postgres Query Optimizer 1. Tom Lane’s Code 2. Tom Lane’s

    Talk 3. Tom Lane’s Mails From Postgresql.org “ Involved in all aspects of PostgreSQL, including bug evaluation and fixes, performance improvements, and major new features, such as schemas. He is also responsible for the optimizer. ” 3 Source Oleg bartunov
  4. 11

  5. 12

  6. 自然數的乘法滿足 • 交換律 (Commutative Law) 3 × 5 = 5

    × 3 • 結合律 (Associative Law) (7 × 3) × 5 = 7 × (3 × 5) 19
  7. Just reorder what you want !!! If there has some

    5 and 2 , make them together. If there has some 0 , then the final answer must be 0. 20
  8. HEURISTIC-BASED OPTIMIZATION • Predicate Pushdown • Projection Pushdown • Constant

    Folding • Function inlining • … 95% will work, just do it. Like always make 5 × 2 21
  9. 24

  10. 25

  11. Matrix Chain Multiplication Problem Ma × Mb × Mc ×

    Md × Me Bottom up Dynamic Programming Level 2 (Ma × Mb ),(Mb × Mc ),(Mc × Md ),(Md × Me ) Level 3 (Ma × Mb × Mc ),(Mb × Mc × Md ),(Mc × Md × Me ) Level 4 (Ma × Mb × Mc × Md ),(Mb × Mc × Md × Me ) Level 5 (Ma × Mb × Mc × Md × Me ) 26
  12. Matrix Chain Multiplication Problem Ma × Mb × Mc ×

    Md × Me Level 4 的其中一個狀態 (Ma × Mb × Mc × Md ) 1. ((Ma × Mb × Mc ) × Md ) 2. (Ma × (Mb × Mc × Md )) 3. ((Ma × Mb ) × (Mc × Md ) ) 27
  13. “ 先求不傷身體,再講求效果。 “ — 五州製藥 “ For a given query

    , find a correct execution plan that has the lowest cost . “ — CMU 15-721 29
  14. 遇到 NP-Hard 的問題該怎麼辦? N (geqo_threshold) < 12 (大致上) 1. N

    很小的的時候硬算(standard_join_search) N (geqo_threshold) >= 12 (大致上) 2. 只解部分子問題 3. 近似演算法(保證跟最佳解最多差多少) 4. 隨機演算法(Randomized Algorithm) 35
  15. 遇到 NP-Hard 的問題該怎麼辦? N (geqo_threshold) < 12 (大致上) 1. N

    很小的的時候硬算 N (geqo_threshold) >= 12 (大致上) 2. 只解部分子問題 3. 近似演算法(保證跟最佳解最多差多少) 4. 隨機演算法(Randomized Algorithm) 36
  16. 37

  17. ”Все счастливые семьи похожи друг надруга, каждая несчастливая семьянесчастлива по-своему.”

    – Анна Каренина 44 ”幸福的家庭都是相似的, 不幸的家庭各有各的不幸” Genetic algorithm 的精神
  18. 45

  19. 46

  20. 56

  21. 57

  22. 58

  23. 59

  24. 60 Recommended Reading List • An Introduction to Join Ordering

    – Justin Jaffray (my favorite database writer 🤩) • Introduction to the Join Ordering Problem – Alexey Goncharuk • What is the Postgres' SQL GAME plan (I) ? – DouEnergy