Slide 1

Slide 1 text

Postgresql Explain Chennai.rb meet-up 19-Sep-2015

Slide 2

Slide 2 text

Postgresql Explain Abhishek Yadav @h6165

Slide 3

Slide 3 text

Postgresql Explain: Agenda ● Relevance and justification ● Syntax ● Usage - SELECT ● Index basics ● Some Heuristics ● Usage GROUP ● Usage – JOIN ● A bit about Mysql

Slide 4

Slide 4 text

Postgresql Explain: Relevance and Justification ● Explain helps us understand the query execution plan – hence enabling us to speculate its performance and make amends ● Explain provides some insights into the algorithms used.

Slide 5

Slide 5 text

Postgresql Explain: Syntax ● EXPLAIN ● EXPLAIN ANALYSE – BEGIN; EXPLAIN ANALYSE ; ROLLBACK ● ActiveRecord::Relation#explain

Slide 6

Slide 6 text

Postgresql Explain: Usage ● Simple SELECT query

Slide 7

Slide 7 text

Postgresql Explain: SELECT ● SELECT with WHERE (no index)

Slide 8

Slide 8 text

Postgresql Explain: SELECT ● SELECT with WHERE indexed

Slide 9

Slide 9 text

Postgresql Explain: SELECT ● SELECT with WHERE indexed (bitmap)

Slide 10

Slide 10 text

Postgresql Explain: Index intro ● On-disk data structure that helps locate data in tables ● Write operations update the indexes ● Mostly B-tree. Bitmap, Gin, Gist ● Db chooses whether to use index, or which one ● Cardinality matters

Slide 11

Slide 11 text

Postgresql Explain: Heuristics ● Seq scan on large tables => An index can help ● Seq scan on indexed tables => Wrongly indexed/ low-cardinality

Slide 12

Slide 12 text

Postgresql Explain: Group – Arbitrary example

Slide 13

Slide 13 text

Postgresql Explain: Join ● LEFT JOIN – low cardinality – 100 posts, 50k comments

Slide 14

Slide 14 text

Postgresql Explain: Join ● LEFT JOIN – better cardinality – 10k posts, 50k comments

Slide 15

Slide 15 text

Postgresql Explain: Join ● INNER JOIN with GROUP and ORDER BY – No filters: 10k posts, 50k comments

Slide 16

Slide 16 text

Postgresql Explain: Join ● INNER JOIN with GROUP and ORDER BY – With filters: 10k posts, 50k comments

Slide 17

Slide 17 text

Postgresql Explain: More Indexing Facts ● Indexing JOIN predicates is useless ● Filtering can help. ● Join algorithm can change based on the table sizes: Nested or Hash (or Sort-Merge) ● Index access can itself be inefficient ● ORDER BY clause has extra cost. Can be reduced with indexes ● GROUP BY improves with indexing ● Partial indexes are available

Slide 18

Slide 18 text

Postgresql Explain: A little bit about Mysql ● Mysql has a fairly basic EXPLAIN ● Watch out for: Type column – 'Using index', 'eq_ref', 'const', 'primary' are fine – 'ref', 'range' may be okay – 'ALL' is not fine ● 'Rows' column is also a good indicator

Slide 19

Slide 19 text

Postgresql Explain: Conclusions