CSE 530A. Query Planning. Washington University Fall 2013

Similar documents
CSE 530A. B+ Trees. Washington University Fall 2013

What s in a Plan? And how did it get there, anyway?

Reminders - IMPORTANT:

Next-Generation Parallel Query

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst October 23 & 25, 2007

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Database Systems: Fall 2009 Quiz I Solutions

Introduction to Database Systems CSE 344

PostgreSQL Query Optimization. Step by step techniques. Ilya Kosmodemiansky

SQL QUERY EVALUATION. CS121: Relational Databases Fall 2017 Lecture 12

Reminders. Query Optimizer Overview. The Three Parts of an Optimizer. Dynamic Programming. Search Algorithm. CSE 444: Database Internals

Hash-Based Indexing 165

Advanced Database Systems

Query Processing. Lecture #10. Andy Pavlo Computer Science Carnegie Mellon Univ. Database Systems / Fall 2018

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst March 8 and 13, 2007

Principles of Data Management. Lecture #12 (Query Optimization I)

DATABASE PERFORMANCE AND INDEXES. CS121: Relational Databases Fall 2017 Lecture 11

CSE 344 FEBRUARY 14 TH INDEXING

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Database Systems: Fall 2017 Quiz I

Overview of Implementing Relational Operators and Query Evaluation

CSE 444: Database Internals. Lecture 11 Query Optimization (part 2)

Hash table example. B+ Tree Index by Example Recall binary trees from CSE 143! Clustered vs Unclustered. Example

Database Systems CSE 414

Chapter 12: Query Processing. Chapter 12: Query Processing

Chapter 12: Indexing and Hashing. Basic Concepts

Introduction to Database Systems CSE 414. Lecture 26: More Indexes and Operator Costs

Parser: SQL parse tree

Chapter 12: Indexing and Hashing

Chapter 12: Query Processing

Query Processing & Optimization

Administrivia. CS 133: Databases. Cost-based Query Sub-System. Goals for Today. Midterm on Thursday 10/18. Assignments

CS122 Lecture 15 Winter Term,

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

PS2 out today. Lab 2 out today. Lab 1 due today - how was it?

Becoming a better developer with explain

Introduction to Database Systems CSE 344

Data Storage. Query Performance. Index. Data File Types. Introduction to Data Management CSE 414. Introduction to Database Systems CSE 414

CS222P Fall 2017, Final Exam

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 9 - Query optimization

Parallel Query In PostgreSQL

Design and Implementation of Bit-Vector filtering for executing of multi-join qureies

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Winter 2009 Lecture 6 - Storage and Indexing

CMSC424: Database Design. Instructor: Amol Deshpande

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

Relational Query Optimization. Highlights of System R Optimizer

Relational Query Optimization

Advanced Databases: Parallel Databases A.Poulovassilis

CSE 444: Database Internals. Lectures 5-6 Indexing

R & G Chapter 13. Implementation of single Relational Operations Choices depend on indexes, memory, stats, Joins Blocked nested loops:

Querying Data with Transact SQL

Principles of Data Management. Lecture #9 (Query Processing Overview)

On-Disk Bitmap Index Performance in Bizgres 0.9

Database System Concepts

Query Optimizer MySQL vs. PostgreSQL

CSE 544, Winter 2009, Final Examination 11 March 2009

Query Processing: A Systems View. Announcements (March 1) Physical (execution) plan. CPS 216 Advanced Database Systems

Chapter 13: Query Processing

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

Sorting and Searching

QUERY OPTIMIZATION E Jayant Haritsa Computer Science and Automation Indian Institute of Science. JAN 2014 Slide 1 QUERY OPTIMIZATION

Intro to DB CHAPTER 12 INDEXING & HASHING

Chapter 11: Indexing and Hashing

Query Processing: The Basics. External Sorting

Fundamentals of Database Systems

Query optimization. Elena Baralis, Silvia Chiusano Politecnico di Torino. DBMS Architecture D B M G. Database Management Systems. Pag.

Problem Set 2 Solutions

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Database Systems: Fall 2015 Quiz I

Query Processing: an Overview. Query Processing in a Nutshell. .. CSC 468 DBMS Implementation Alexander Dekhtyar.. QUERY. Parser.

Sorting and Searching

! A relational algebra expression may have many equivalent. ! Cost is generally measured as total elapsed time for

Chapter 13: Query Processing Basic Steps in Query Processing

CS330. Query Processing

2. Make an input file for Query Execution Steps for each Q1 and RQ respectively-- one step per line for simplicity.

Column Stores vs. Row Stores How Different Are They Really?

Query Optimizer MySQL vs. PostgreSQL

Schema for Examples. Query Optimization. Alternative Plans 1 (No Indexes) Motivating Example. Alternative Plans 2 With Indexes

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Overview of Query Evaluation. Overview of Query Evaluation

Relational Query Optimization

Database Systems CSE 414

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

D B M G Data Base and Data Mining Group of Politecnico di Torino

CS122 Lecture 4 Winter Term,

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

RELATIONAL OPERATORS #1

Chapter 12: Indexing and Hashing

Overview of Query Evaluation

PostgreSQL: Decoding Partition

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

6.830 Problem Set 2 (2017)

Database Systems CSE 414

Leveraging Query Parallelism In PostgreSQL

Database Tuning and Physical Design: Basics of Query Execution

7. Query Processing and Optimization

Physical DB design and tuning: outline

Indexing. Week 14, Spring Edited by M. Naci Akkøk, , Contains slides from 8-9. April 2002 by Hector Garcia-Molina, Vera Goebel

Informatik Vertiefung Database Systems

Query Parallelism In PostgreSQL

6.830 Lecture 8 10/2/2017. Lab 2 -- Due Weds. Project meeting sign ups. Recap column stores & paper. Join Processing:

Join (SQL) - Wikipedia, the free encyclopedia

Physical Design. Elena Baralis, Silvia Chiusano Politecnico di Torino. Phases of database design D B M G. Database Management Systems. Pag.

Transcription:

CSE 530A Query Planning Washington University Fall 2013

Scanning When finding data in a relation, we've seen two types of scans Table scan Index scan There is a third common way Bitmap scan

Bitmap Scans When performing an index scan it is usually necessary to also retrieve the actual matching records from the table Most often, the records in the table file are not in the order accessed This leads to many random accesses in the table file Sequential file access is generally faster than random access If we could access the table file in sequentially then performance could be increased

Bitmap Scans In a bitmap scan, a bitmap is created while performing an index scan The bitmap indicates the position of the matching records in the table file (also called the heap) The index scan step is called a bitmap index scan A scan of the heap (table file) is then done, retrieving the records indicated by the bitmap The table scan step is called a bitmap heap scan

Lossy Bitmap Scans If a table is too large to efficiently create a bitmap of the records then a bitmap of the pages can be used The bitmap indicates which table pages contain records that match the predicate The bitmap heap scan then fetches the indicated pages in sequential order Must recheck the records in each fetched page to find the ones that match the predicate

Disjunctions Bitmap scans can be helpful in implementing queries with disjunctions If we have a query like " WHERE year < '2010' OR major = 'CSE'" and separate indexes on year and major then we could do an index scan on year to create the bitmap do an index scan on major and set the matching bits in the bitmap do the bitmap heap scan

Query Planning We now have three ways of scanning a relation table scan index scan bitmap index scan When should each method be used? Answer is not always obvious

Query Planning If we need to access every record in the relation Do a table scan. Accessing the index is just unnecessary overhead. If no appropriate index is available Table scan is only option. If index is available Best method depends on size of table and percentage of matching records If the table is small enough then it might be fastest to just do a table scan If the number of matching records is small then an index scan is probably fastest If the number of matching records is above some limit then a bitmap scan or table scan might be fastest

Query Planning What is the limit at which we should switch from index scan to bitmap or table scan? No hard and fast rule Some references say just 5% or greater matching records (Seems a little low to me)

Query Planning Three ways of scanning relations table scan index scan bitmap index scan Three ways of joining relations nested loop join merge join hash join

Query Planning Selecting from a single table gives 3 possible options 3 ways of scanning Joining two tables gives 3 3 options 3 for selecting from each table 3 for the actual join Each additional table joined multiplies the possibilities by 3 2 3 for selecting from the additional table 3 for joining In addition, there are often multiple possibilities for when to handle the predicates How does the DBMS choose a query plan?

Query Planning A query plan can be represented as a tree leaves are scans inner nodes are joins, sorts, materializations nested loop join condition: students.mid = majors.mid table scan on students condition: year='2010' index scan on majors condition: title='cse'

Query Planning Some joins can be done in different orders For example, if we have x inner join y inner join z we'll get the same result if x join y is done first and then joined to z or y join z is done first and then joined to x join join join z x join x y y z

Query Planning It is the job of the query planner to pick the minimum cost plan Two issues The number of possible plans grows exponentially in the number of joined tables How is the cost of a plan calculated?

Query Planning We can assign a cost to a plan node if given The cost of the child nodes The size of the sets generated by the child nodes The cost of the actual operations of the node

Query Planning What do we mean by "cost"? Time! We're looking for the plan that takes the shortest time The cost of an operation is generally calculated as a function of the number of page fetches/writes and the CPU time spent I/O time usually dominates, but CPU time also plays a part Also often subdivided into sequential page and random page access as these can take different times Number of page accesses and CPU time directly dependent on size of data

Query Planning For a given plan node, the query planner tries to estimate both the cost (in some unit of time) and the size of the resulting set based on the type of operation and the size of the input sets The cost of a node in the plan tree includes the cost of its children So the cost of the root node is the total estimated cost of executing the plan

Query Planning The number of possible plans increases exponentially with the complexity of the query Quickly becomes impossible for a query planner to evaluate all possible plans The query planner itself needs to be fast Since the planner is choosing among various trees which often have identical subtrees, dynamic programming techniques (e.g., memoization) can be used Still may not have enough time to evaluate all possible plans

Query Planning Some query optimizers favor left-deep trees A left-deep tree adds each new table as the right child of a join left-deep right-deep bushy join join join join d a join join join join c b join a b c d a b c d

Query Planning The nodes can be made interchangeable by using an iterator interface Each child supplies the set resulting from its operation as an iterator to its parent Using an iterator interface also facilitates pipelining A parent node doesn't need to know if the child has a materialized set or is providing elements on demand

Query Planning Part of estimating the cost for a node requires estimating the number of records resulting from a scan or join For a select without a predicate we could use the number of rows in the table For an equals predicate on a unique column we know there will be at most one How do we know how many rows a scan with a predicate will return before actually doing the scan?

Query Planning The query planner tries to estimate the selection factor of predicates by using statistics on the distribution of values for each column Basically keeps a histogram for each column

Query Planning If the statistics kept by the DBMS for a column are significantly wrong then the performance of queries using that column can be greatly impacted The query planner could have vastly incorrect cost estimates and therefore pick a bad plan Tables with lots of modifications (inserts/updates/deletes) should periodically be analyzed The DBMS will scan the table and regenerate its histograms

Sargable Predicates A predicate is sargable if it does not interfere with the DBMS's ability to use an index Applying a function to a column in the WHERE clause usually makes it non-sargable Non-Sargable: WHERE year(date) = 2012 Sargable: WHERE date BETWEEN '2012-01-01' AND '2013-01-01 Non-Sargable: WHERE substring(model, 6) = 'Toyota' Sargable: WHERE model LIKE 'Toyota%' ("sargable" comes from Search ARGument ABLE)

Explain PostgreSQL has an EXPLAIN command that will give details on the chosen plan for a query Other DBMSs have similar features, but there is no standard EXPLAIN SELECT * FROM tenk1; QUERY PLAN ------------------------------------------------------------- Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244)

Explain EXPLAIN SELECT * FROM tenk1; QUERY PLAN ------------------------------------------------------------- Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244) The first cost value is the estimated start-up cost That is, the time before scan of the output can start The second cost value is the estimated cost to retrieve all the requested rows The time values are in arbitrary units based on the configured cost parameters (by default, in units of page fetches) The rows value is the estimated number of rows output by this node The width value is the estimated average width (in bytes) of the output rows

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 7000; QUERY PLAN ------------------------------------------------------------ Seq Scan on tenk1 (cost=0.00..483.00 rows=7033 width=244) Filter: (unique1 < 7000) Added a WHERE clause Number of rows has gone down Time has gone up slightly Reflects the extra CPU time used in checking the condition

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100; QUERY PLAN ------------------------------------------------------------------------------ Bitmap Heap Scan on tenk1 (cost=2.37..232.35 rows=106 width=244) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) Index Cond: (unique1 < 100) EXPLAIN outputs a textual representation of the plan tree The '->' indicates a child node and the level of indentation indicates the parent So in this example, the Bitmap Index Scan node is a child of the Bitmap Heap Scan node These generally always go together

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100; QUERY PLAN ------------------------------------------------------------------------------ Bitmap Heap Scan on tenk1 (cost=2.37..232.35 rows=106 width=244) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) Index Cond: (unique1 < 100) By changing the predicate, the estimated number of matching rows has dropped drastically which caused the planner to pick a different plan Note that the costs for the parent node includes the costs for the child nodes

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 3; QUERY PLAN ------------------------------------------------------------------------------ Index Scan using tenk1_unique1 on tenk1 (cost=0.00..10.00 rows=2 width=244) Index Cond: (unique1 < 3) Changing the predicate again has again drastically reduced the estimated number of rows and changed the selected plan Why was table scan picked for the large number of rows? index scan picked for the small number of rows? bitmap scan picked for the medium number of rows?

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 3 AND stringu1 = 'xxx'; QUERY PLAN ------------------------------------------------------------------------------ Index Scan using tenk1_unique1 on tenk1 (cost=0.00..10.01 rows=1 width=244) Index Cond: (unique1 < 3) Filter: (stringu1 = 'xxx'::name) Added a conjunction to the predicate Number of rows is smaller Cost is (very slightly) higher

Explain EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100 AND unique2 > 9000; QUERY PLAN ---------------------------------------------------------------------------------- Bitmap Heap Scan on tenk1 (cost=11.27..49.11 rows=11 width=244) Recheck Cond: ((unique1 < 100) AND (unique2 > 9000)) -> BitmapAnd (cost=11.27..11.27 rows=11 width=0) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) Index Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique2 (cost=0.00..8.65 rows=1042 width=0) Index Cond: (unique2 > 9000) In this example, the results of two bitmap index scans are being ANDed together before performing the bitmap heap scan Can be done since indexes exist on both columns Could also have been done by doing a bitmap scan on the one column and filtering by the other predicate

Explain EXPLAIN SELECT * FROM tenk1 t1, tenk2 t2 WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2; QUERY PLAN ------------------------------------------------------------------------------------ Nested Loop (cost=2.37..553.11 rows=106 width=488) -> Bitmap Heap Scan on tenk1 t1 (cost=2.37..232.35 rows=106 width=244) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) Index Cond: (unique1 < 100) -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.00..3.01 rows=1 width=244) Index Cond: (t2.unique2 = t1.unique2) This example uses a nested loop join

Explain SET enable_nestloop = off; EXPLAIN SELECT * FROM tenk1 t1, tenk2 t2 WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2; QUERY PLAN ------------------------------------------------------------------------------------- Hash Join (cost=232.61..741.67 rows=106 width=488) Hash Cond: (t2.unique2 = t1.unique2) -> Seq Scan on tenk2 t2 (cost=0.00..458.00 rows=10000 width=244) -> Hash (cost=232.35..232.35 rows=106 width=244) -> Bitmap Heap Scan on tenk1 t1 (cost=2.37..232.35 rows=106 width=244) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) Index Cond: (unique1 < 100) Here's the same example with nested loop joins disabled Generally not a good idea to disable features like this. Note the increased query cost.

Explain Analyze EXPLAIN ANALYZE can be used to compare the estimated costs with the actual costs Note that EXPLAIN ANALYZE will cause the query to actually be executed Might want to perform in a transaction and rollback

Explain Analyze EXPLAIN ANALYZE SELECT * FROM tenk1 t1, tenk2 t2 WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2; QUERY PLAN -------------------------------------------------------------------------------------------- Nested Loop (cost=2.37..553.11 rows=106 width=488) (actual time=1.392..12.700 rows=100 loops=1) -> Bitmap Heap Scan on tenk1 t1 (cost=2.37..232.35 rows=106 width=244) (actual time=0.878..2.367 rows=100 loops=1) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..2.37 rows=106 width=0) (actual time=0.546..0.546 rows=100 loops=1) Index Cond: (unique1 < 100) -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.00..3.01 rows=1 width=244) (actual time=0.067..0.078 rows=1 loops=100) Index Cond: (t2.unique2 = t1.unique2) Total runtime: 14.452 ms The actual times are in milliseconds