Query Optimization Through the Looking Glass Some Lessons From - - PowerPoint PPT Presentation

query optimization through the looking glass some lessons
SMART_READER_LITE
LIVE PREVIEW

Query Optimization Through the Looking Glass Some Lessons From - - PowerPoint PPT Presentation

Query Optimization Through the Looking Glass Some Lessons From Building an LLVM-Based Query Compiler Viktor Leis Technische Universitt Mnchen Introduction: Query Optimization HJ B cardinality cost INL B T SELECT ... estimation


slide-1
SLIDE 1

Query Optimization Through the Looking Glass Some Lessons From Building an LLVM-Based Query Compiler

Viktor Leis

Technische Universität München

slide-2
SLIDE 2

Introduction: Query Optimization

SELECT ... FROM R,S,T WHERE ...

v

B B

R S T

HJ INL

cardinality estimation cost model plan space enumeration

slide-3
SLIDE 3

Introduction: Query Optimization

SELECT ... FROM R,S,T WHERE ...

v

B B

R S T

HJ INL

cardinality estimation cost model plan space enumeration

◮ How good are cardinality estimators? ◮ When do bad estimates lead to slow queries? ◮ How important is an accurate cost model for the overall query

  • ptimization process?

◮ (How large does the enumerated plan space need to be?)

slide-4
SLIDE 4

Join Order Benchmark: Data Set

◮ Internet Movie Data Base data set (imdb.com) ◮ around 4GB, 21 relations ◮ information about movies and related facts about actors,

directors, production companies, etc.

◮ publicly available for non-commercial use ◮ like all real-world data sets, full of join-crossing correlations

slide-5
SLIDE 5

Join Order Benchmark: 113 Queries

SELECT cn.name, mi.info as rating, miidx.info as reldate FROM company_name cn, company_type ct, info_type it, info_type it2, title t kind_type kt, movie_info mi, movie_companies mc, movie_info_idx miidx WHERE cn.country_code = ’[us]’ AND it.info = ’rating’ AND ct.kind = ’production companies’ AND kt.kind = ’movie’ AND it2.info = ’release dates’ AND ...

movie_info_idx movie_companies title info_type company_type company_name kind_type movie_info info_type

slide-6
SLIDE 6

Methodology

◮ cardinality extraction

  • 1. load data set into different systems
  • 2. run statistics tool (default settings)
  • 3. collect estimates for all subexpressions (e.g., using EXPLAIN in

PostgreSQL) to obtain cardinality estimates

  • 4. also run SELECT COUNT(*) queries to obtain true cardinalities

◮ cardinality injection into PostgreSQL

slide-7
SLIDE 7

Cardinality Estimation

slide-8
SLIDE 8

Cardinality Estimation for Base Table Selections

◮ q-error: max(e/r, r/e) (over- underestimation factor)

median 90th 95th max PostgreSQL 1.00 2.08 6.10 207 DBMS A 1.01 1.33 1.98 43.4 DBMS B 1.00 6.03 30.2 104000 DBMS C 1.06 1677 5367 20471 HyPer 1.02 4.47 8.00 2084

slide-9
SLIDE 9

Cardinality Estimation for Joins (1)

PostgreSQL 1e8 1e6 1e4 1e2 1 1e2 1e4 1 2 3 4 5 6 ← underestimation [log scale] overestimation →

95th percentile 5th percentile median 75th percentile 25th percentile

slide-10
SLIDE 10

Cardinality Estimation for Joins (2)

PostgreSQL DBMS A DBMS B DBMS C HyPer 1e8 1e6 1e4 1e2 1 1e2 1e4 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 number of joins ← underestimation [log scale] overestimation →

95th percentile 5th percentile median 75th percentile 25th percentile

slide-11
SLIDE 11

When do estimation errors result in bad query plans?

slide-12
SLIDE 12

Effect of Estimates on Query Performance (1)

◮ performance with true cardinalities vs. PostgreSQL’s estimates default + no nested-loop join + rehashing

(a) (b) (c)

0% 20% 40% 60% [ . 3 , . 9 ) [ . 9 , 1 . 1 ) [ 1 . 1 , 2 ) [ 2 , 1 ) [ 1 , 1 ) > 1 [ . 3 , . 9 ) [ . 9 , 1 . 1 ) [ 1 . 1 , 2 ) [ 2 , 1 ) [ 1 , 1 ) > 1 [ . 3 , . 9 ) [ . 9 , 1 . 1 ) [ 1 . 1 , 2 ) [ 2 , 1 ) [ 1 , 1 ) > 1

slide-13
SLIDE 13

Effect of Estimates on Query Performance (2)

PK indexes PK + FK indexes

(a) (b)

0% 20% 40% 60% [ . 3 , . 9 ) [ . 9 , 1 . 1 ) [ 1 . 1 , 2 ) [ 2 , 1 ) [ 1 , 1 ) > 1 [ . 3 , . 9 ) [ . 9 , 1 . 1 ) [ 1 . 1 , 2 ) [ 2 , 1 ) [ 1 , 1 ) > 1

slide-14
SLIDE 14

Cost Model

slide-15
SLIDE 15

Cost vs. Runtime

standard cost model tuned cost model simple cost model 1e+01 1e+03 1e+05 1 100 10000 PostgreSQL estimates true cardinalities 1e2 1e4 1e2 1e4 1e2 1e4

runtime [ms] [log scale] cost [log scale]

slide-16
SLIDE 16

Conclusions

◮ cardinality estimates are quite bad for all tested systems ◮ nested-loop joins are dangerous ◮ the more indexes are available the more difficult it becomes to

find the optimal plan

◮ cost model is much less important than cardinality estimates

The paper (“How Good Are Query Optimizers, Really?”) is available at: www.vldb.org/pvldb/vol9/p204-leis.pdf

slide-17
SLIDE 17

Part II: Building an LLVM-Based Query Compiler

slide-18
SLIDE 18

HyPer

◮ research project started at TU Munich in 2010 by Thomas

Neumann and Alfons Kemper

◮ relational main-memory DBMS ◮ SQL dialect is (mostly) PostgreSQL compatible, follows

PostgreSQL server protocol

◮ goal: similar performance as hand-written C programs ◮ startup acquired by Tableau Software in 2016

slide-19
SLIDE 19

Compiling Query Plans

select * from R1,R3, (select R2.z, count(*) from R2 where R2.y=3 group by R2.z) R2 where R1.x=7 and R1.a=R3.b and R2.z=R3.c

R1 R2 R3

x=7 y=3 z;count(*) a=b z=c

initialize memory of

a=b, c=z, and Γz

for each tuple t in R1 if t.x = 7 materialize t in hash table of

a=b

for each tuple t in R2 if t.y = 3 aggregate t in hash table of Γz for each tuple t in Γz materialize t in hash table of

z=c

for each tuple t3 in R3 for each match t2 in

z=c[t3.c]

for each match t1 in

a=b[t3.b]

  • utput t1 ◦t2 ◦t3
slide-20
SLIDE 20

Compiling Queries: Interface

◮ each operator implements the following interface:

◮ produce(): generate code for that operator (and its child

  • perators)

◮ consume(attributes,source): generate code that receives a

tuple from input

slide-21
SLIDE 21

Compiling Queries: Example Operators

scan.produce(): print “for each tuple in relation” scan.parent.consume(attributes,scan) σ.produce: σ.input.produce() σ.consume(a,s): print “if ”+σ.condition σ.parent.consume(attr,σ) .produce(): .left.produce() .right.produce() .consume(a,s): if (s== .left) print “materialize tuple in hash table” else print “for each match in hashtable[” +a.joinattr+“]” .parent.consume(a+new attributes)

slide-22
SLIDE 22

Compiling to LLVM IR

◮ compilation to LLVM Intermediate Representation (IR)

(“machine-independent assembler”) using C++ API

◮ attributes are kept in CPU registers as far as possible ◮ generating code can be tedious but many compile time

abstractions help, e.g.:

◮ high-level control flow constructs (if, for) ◮ SQL value abstraction that handles null semantics, overflow

checking, etc.

◮ these abstractions do not cost any runtime

◮ for pragmatic and code size reasons some operators are

partially/mostly implemented in C++

slide-23
SLIDE 23

Performance (1 GB, 1 thread)

TPC-H O0 O1 # PG 9.6 comp. exec. comp. exec. 1 4908 6 161 42 77 2 254 23 13 149 8 3 1258 10 104 69 80 4 193 7 67 47 45 5 516 15 60 104 37

slide-24
SLIDE 24

Lessons

◮ LLVM IR as a target language is great

◮ stable ◮ portable ◮ generates efficient machine code

◮ for large queries, compilation times can become a problem

slide-25
SLIDE 25

More Information

“Efficiently Compiling Efficient Query Plans”: www.vldb.org/pvldb/vol9/p204-leis.pdf “Compiling Database Queries into Machine Code”: sites.computer.org/debull/A14mar/p3.pdf all HyPer papers: www.hyper-db.com interested in doing a PhD at TU Munich? contact us (leis@in.tum.de)