Module 9: Selectivity Estimation

Similar documents
Chapter 9. Cardinality Estimation. How Many Rows Does a Query Yield? Architecture and Implementation of Database Systems Winter 2010/11

Cost Models. the query database statistics description of computational resources, e.g.

Module 4: Tree-Structured Indexing

Module 9: Query Optimization

ATYPICAL RELATIONAL QUERY OPTIMIZER

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

Optimization Overview

Database Applications (15-415)

Database System Concepts

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

Chapter 13: Query Processing

Evaluation of relational operations

7. Query Processing and Optimization

Chapter 12: Query Processing. Chapter 12: Query Processing

Advances in Data Management Query Processing and Query Optimisation A.Poulovassilis

! 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

Database Applications (15-415)

RELATIONAL OPERATORS #1

Principles of Data Management. Lecture #13 (Query Optimization II)

Chapter 12: Query Processing

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

Chapter 12: Query Processing

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

CMSC424: Database Design. Instructor: Amol Deshpande

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

Overview of Query Processing. Evaluation of Relational Operations. Why Sort? Outline. Two-Way External Merge Sort. 2-Way Sort: Requires 3 Buffer Pages

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Evaluation of Relational Operations: Other Techniques

Advanced Database Systems

Query Processing & Optimization. CS 377: Database Systems

CMPUT 391 Database Management Systems. Query Processing: The Basics. Textbook: Chapter 10. (first edition: Chapter 13) University of Alberta 1

Evaluation of Relational Operations

Something to think about. Problems. Purpose. Vocabulary. Query Evaluation Techniques for large DB. Part 1. Fact:

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

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

Evaluation of Relational Operations: Other Techniques

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

Chapter 3. Algorithms for Query Processing and Optimization

Query Processing & Optimization

Outline. Query Processing Overview Algorithms for basic operations. Query optimization. Sorting Selection Join Projection

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

Data Modeling and Databases Ch 9: Query Processing - Algorithms. Gustavo Alonso Systems Group Department of Computer Science ETH Zürich

Module 5: Hash-Based Indexing

Overview of Query Evaluation. Chapter 12

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

Database Applications (15-415)

! Parallel machines are becoming quite common and affordable. ! Databases are growing increasingly large

Chapter 20: Parallel Databases

Chapter 20: Parallel Databases. Introduction

Overview of Implementing Relational Operators and Query Evaluation

Implementation of Relational Operations: Other Operations

ECS 165B: Database System Implementa6on Lecture 7

DBMS Query evaluation

Relational Query Optimization

Data Modeling and Databases Ch 10: Query Processing - Algorithms. Gustavo Alonso Systems Group Department of Computer Science ETH Zürich

Chapter 18: Parallel Databases

Chapter 18: Parallel Databases. Chapter 18: Parallel Databases. Parallelism in Databases. Introduction

Query Optimization Overview

Database Applications (15-415)

Implementation of Relational Operations. Introduction. CS 186, Fall 2002, Lecture 19 R&G - Chapter 12

Evaluation of Relational Operations: Other Techniques

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 7 - Query execution

Query processing and optimization

Hash-Based Indexing 165

Chapter 17: Parallel Databases

Evaluation of Relational Operations

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

Module 4. Implementation of XQuery. Part 0: Background on relational query processing

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

Implementing Relational Operators: Selection, Projection, Join. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Review. Support for data retrieval at the physical level:

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #10: Query Processing

Evaluation of Relational Operations

Chapter 13: Query Optimization

Outline. Database Management and Tuning. Outline. Join Strategies Running Example. Index Tuning. Johann Gamper. Unit 6 April 12, 2012

CompSci 516 Data Intensive Computing Systems

Query Optimization in Distributed Databases. Dilşat ABDULLAH

Faloutsos 1. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Outline

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

CMPT 354: Database System I. Lecture 7. Basics of Query Optimization

15-415/615 Faloutsos 1

Query Processing: The Basics. External Sorting

Query Evaluation! References:! q [RG-3ed] Chapter 12, 13, 14, 15! q [SKS-6ed] Chapter 12, 13!

Architecture and Implementation of Database Systems Query Processing

Database Management Systems (COP 5725) Homework 3

Relational Databases

Database Applications (15-415)

Datenbanksysteme II: Caching and File Structures. Ulf Leser

CSIT5300: Advanced Database Systems

External Sorting Implementing Relational Operators

Evaluation of Relational Operations. Relational Operations

Principles of Database Management Systems

Overview of Query Evaluation. Overview of Query Evaluation

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

User Perspective. Module III: System Perspective. Module III: Topics Covered. Module III Overview of Storage Structures, QP, and TM

CMSC424: Database Design. Instructor: Amol Deshpande

CSE 190D Spring 2017 Final Exam Answers

Transcription:

Module 9: Selectivity Estimation Module Outline 9.1 Query Cost and Selectivity Estimation 9.2 Database profiles 9.3 Sampling 9.4 Statistics maintained by commercial DBMS Web Forms Transaction Manager Lock Manager Plan Executor Operator Evaluator Concurrency Control Applications SQL Commands Files and Index Structures Buffer Manager Disk Space Manager Parser Optimizer SQL Interface You are here! Query Processor Recovery Manager DBMS Index Files Data Files System Catalog Database 268

9.1 Query Cost and Selectivity Estimation The DBMS has a number of alternative implementations available for each (logical) algebra operator. Selecting an implementation for each of the operators in a plan is one of the tasks of the query optimizer. The applicability of implementations may depend on various physical properties of the argument files (or streams), such as sort orders, availability of indexes,... Among the applicable plans, the optimizer selects the ones with the least expected cost. The cost of the implementation algorithms is determined by the size of the input arguments. Therefore, it is crucial to know, compute, or estimate the sizes of arguments. This chapter presents some techniques to measure the quantitative characteristics of database files or input/output streams. 269

Principal Approaches There are two principal approaches to estimate sizes of relations: 1. Maintain a database profile, i.e., statistical information about numbers and sizes of tuples, distribution of attribute values and the like, as part of the database catalog (meta information) during database updates. Calculate similar parameters for (intermediate) query results based upon a (simple) statistical model during query optimization. Typically, the statistical model is based upon the uniformity and independence assumptions. Both are typically not valid, but they allow for very simple calculations. In order to provide more accuracy, the system can record histograms to more closely approximate value distributions. 2. Use sampling techniques, i.e., gather necessary characteristics of a query plan (input relations and intermediate results) by running the query on a small sample and by extrapolating to the full input size at query execution time. Crucial decision here is to find the right balance between size of the sample and resulting accuracy of estimation. 270

9.2 Database profiles 9.2.1 Simple profiles Keep statistical information in the database catalogs. For each relation, record R... the number of records for each relation R, R... the number of disk pages allocated by those records, s(r)... record size s(r) and blocksize b as an alternative ( R = R / b s(r) ) V (A, R)... the number of distinct values of attribute A in relation R,... and possibly more. Based on these, develop a statistical model, typically, a very simple one, using primitive assumptions. 271

Typical assumptions In order to obtain simple formulae, assume one of the following: Uniformity & independence assumption: all values of an attribute appear with the same probability; values of different attributes are distributed independent of each other. Simple, yet rarely realistic assumption. Worst case assumption: no knowledge available at all, in case of selections assume all records match the condition. Unrealistic assumption, can only be used for computing upper bounds. Perfect knowledge assumption: exact distribution of values is known. Unrealistic assumption, can only be used for computing lower bounds. Typically use uniformity assumption. 272

9.2.1.1 Selectivity estimation under uniformity assumption Using the parameters mentioned above, and assuming uniform and independent value distributions, we can compute the characteristics of intermediate results for the (logical) operators: 1. Selections Q := A=c(R) Selectivity: sel(a = c) = 1/V (A, R) uniformity! Number records: Q = sel(a = c) R Record size: s(q) = s(r) Number attribute values: V (A, Q) = { 1, for A = A, c( R, V (A, R), Q ), otherwise. 273

The number c( R, V (A, R), Q ) of distinct values in an attribute A after the selection, is estimated using a well-known formula from statistics. The number c of distinct colors obtained by drawing r balls from a bag of n balls in m different colors is c(n, m, r): r, for r < m/2, c(n, m, r) = (r + m)/3, for m/2 r < 2m, m, for r 2m 274

Element tests, e.g., Q := A IN (...) (R): An approximation for the selectivity could be obtained by multiplying the selectivity for an equality selection sel(a = c) with the number of elements in the list of values. 275 2. Other Selection Conditions Equality between attributes, e.g., Q := A=B(R): An approximation for the selectivity could be sel(a = B) = 1/ max(v (A, R), V (B, R)). This assumes that each value in the smaller attribute (i.e., the one with fewer distinct values) has a corresponding match in the other attribute. Range selections, e.g., Q := A>c(R): If the system also keeps track of the miminum and maximum value of each attribute (denoted High(A, R) and Low(A, R) hereinafter), we could approximate the selectivity by sel(a > c) = High(A, R) c High(A, R) Low(A, R).

3. Projections Q := L(R) Estimating the number of result tuples is difficult. Typically use: V (A, R), for L = {A}, R, for key(r) L, Q = R, without duplicate elimination, min ( R, A i L V (A i, R) ), otherwise s(q) = s(a i ) A i L V (A i, Q) = V (A i, R) for A i L 276

4. Unions Q := R S Q R + S s(q) = s(r) = s(s) same schema! V (A, Q) V (A, R) + V (A, S) 5. Differences Q := R S max (0, R S ) Q R s(q) = s(r) = s(s) V (A, Q) V (A, R) same schema! 277

6. Products Q := R S 7. Joins Q := R F S Q = R S s(q) = s(r) + s(s) { V (A, R), for A sch(r) V (A, Q) = V (A, S), for A sch(s) This is the most challenging operator for selectivity estimation! A few simple cases are: No common attributes (sch(r) sch(s) = ), or join predicate F = true : R F S = R S Join attribute, say A, is key in one of the relations, e.g., in R, and assuming the inclusion dependency π A (S) π A (R) : Q = R 278

In the more general case, again assuming inclusion dependencies, π A (S) π A (R) or π A (R) π A (S), we can use two estimates: Q = R S V (A, R) or Q = R S V (A, S) typically use the smaller of those two estimates, i.e., R S Q = max (V (A, R), V (A, S)) s(q) = s(r) + s(s) V (A, Q) A sch(r) sch(s) { min (V (A, R), V (A, S)), V (A, X), s(a) for natural join for A sch(r) sch(s) for A sch(x) 279

9.2.1.2 Selectivity estimation for composite predicates For selections with composite predicates, we compute the selectivities of the individual parts of the condition and combine them appropriately. Combining estimates...? Here we need our second (unrealistic but simplifying) assumption: we assume independence of attribute value distributions, for under this assumption, we can easily compute: Conjunctive predicates, e.g., Q := A=c 1 B=c 2 (R): sel(a = c 1 B = c 2 ) = sel(a = c 1 ) sel(b = c 2 ), which gives Q = R V (A, R) V (B, R). Disjunctive predicates, e.g., Q := A=c 1 B=c 2 (R): sel(a = c 1 B = c 2 ) = sel(a = c 1 )+sel(b = c 2 ) sel(a = c 1 ) sel(b = c 2 ), which gives Q = R V (A, R) + V (B, R) V (A, R) V (B, R). 280

9.2.2 Histograms Observation: in most cases, attribute values are not uniformly distributed across the domain of an attribute. to keep track of non-uniform value distribution of an attribute A, maintain a histogram to approximate the actual distribution: 1 Divide attribute domain into adjacent intervals by selecting boundaries b i dom(a). 2 Collect statistical parameters for each such interval, e.g., number of tuples with b i 1 < t(a) b i, number of distinct A-values in that interval. Two types of histograms can be used: equi-width histograms : intervals all have the same length, equi-depth histograms : intervals all contain the same number of tuples Histograms allow for more exact estimates of (equality and range) selections. I Example of a product using histograms The commercial DBMS Ingres used to maintain histograms. 281

Example of Approximations Using Histograms For skewed, i.e., non-uniform, data distributions, working without histograms gives bad estimates. For example, we would compute a selectivity sel(a > 13) = 45 15 = 3, which is far from exact, since we see that, in fact, 9 tuples qualify. 10 9 8 Actual value distribution D 8 9 10 9 Uniform distribution approximating D 8 7 7 6 6 5 4 4 4 5 3 2 2 3 3 2 3 2 2 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 2 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The error in estimation using the uniformity assumption is especially large for those values, which occur very often in the database. This is bad, since for these, we would actually need the best estimates! Histograms partition the range of (actual) values into smaller pieces ( buckets ) and keep the number of tuples and/or distinct values for each of those intervals. 282

Example of Approximations Using Histograms (cont d) With the equi-width histogram given below, we would estimate 5 result tuples, since the selection covers a third of the last bucket of the histogram (assuming uniform distribution within each bucket of the histogram). Using the equi-depth histogram also given below, we would, in this case, estimate the exact result (9 tuples), since the corresponding bucket contains only a single attribute value. 10,00 9,00 Equiwidth 10,00 9,00 Equidepth 8,00 8,00 7,00 7,00 6,00 6,00 5,00 5,00 5,00 5,00 5,00 5,00 5,00 5,00 4,00 4,00 3,00 2,67 2,67 2,67 3,00 2,00 1,00 1,33 1,33 1,33 1,00 1,00 1,00 2,00 1,00 0,00 0,00 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Typically, equi-depth histograms provide better estimates than equi-width histograms. Compressed histograms keep separate counts for the most frequent values (say, 7 and 14 in our example) and maintain an equi-depth (or whatever) histogram of the remaining values. 283

9.3 Sampling Idea: if maintaining a database profile is costly and error-prone (i.e., may provide far-off estimates), may be it is better to dispense with this idea at all. Rather, if a complex query is to be optimized, execute the query on a small sample to collect accurate statistics. Problem: How small shall/can the sample be? Small enough to be executed efficiently, but large enough to obtain useful characteristics. How precisely can we extrapolate? Good prediction requires large (enough) samples.... select a value for either one of those parameters and derive the other one from that. 284

Three approaches found in the literature 1 adaptive sampling : try to achieve a given precision with minimal sample size. 2 double (two-phase) sampling : first, obtain a coarse picture from a very small sample, just good enough to calculate necessary sample size for a useful precision in second step. 3 sequential sampling : sliding calculation of characteristics, stops when estimated precision is good enough. 285

9.4 Statistics maintained by commercial DBMS The research prototype System/R introduced the concept of a database profile recording simple statistical parameters about each stored relation, each defined index, each segment ( table space ). In addition to the parameters mentioned above (tuple and page cardinalities, number of distinct values), the system stores the current minimum and maximum attribute values (to keep track of the active domain and to provide input for the estimation of range queries). For each index (only B + tree-indexes are supported by System/R), store height of the index tree and number of leaf nodes. Many commercial DBMSs have adopted the same or similar catalog information for their optimizers. 286

N.B.: per segment information is used for estimating the cost of a segment scan. System/R stores more than one relation in a segment and uses the TID addressing scheme. Therefore, there is no way of doing a relation scan. If no other plan is available, the system will have to scan all pages in a segment! 287 Parameters of a DB profile in the System/R catalogs per segment NP number of used pages per relation Card(R) = R, number of tuples PCard(R) = R, number of blocks per index ICard(I) = V (A, R)...... number of distinct values in indexed attribute MinKey(I) minimum attribute value in indexed attribute MaxKey(I) maximum attribute value in indexed attribute NIndx(I) number of leaf nodes NLevels(I) height of index tree Clustered? is this a clustered index (yes/no) All values are approximations only! They are not maintained during database updates (to avoid hot spots ), rather they can be updated explicitly via SQL s update statistics command.

System/R estimation of selectivities Obviously, System/R can use ICard(I)-values for estimating the selectivity sel(a = c) for simple attribute-equals-constant -selections in those cases, where an index on attribute A is available. But, what can we do for the other cases? If there is no index on A, System/R arbitrarily assumes sel(a = c) = 1 10. For selection conditions A = B, the system uses sel(a = B) = 1 max (ICard(I 1 ), ICard(I 2 )) if indexes I 1 and I 2 are available for attributes A and B, respectively. This estimation assumes an inclusion dependency, i.e., each value from the smaller index, say I 1, has a matching value in the other index. Then, given a value a for A, assume that each of the ICard(I 2 ) values for B is equally likely. Hence, the number of tuples that have a given A-value a as their B-value is 1 ICard(I. 2) 1 If only one attribute has an index, assume selectivity sel(a = B) = ICard(I) ; if neither attribute has an index, assume the ubiquitous 1 10. 288,

These formulae are used whether or not A and B are from the same relation. Notice the correspondence with our estimation of join selectivity above!... System/R estimation of selectivities (cont d) For range selections A > c, exploit the MinKey and MaxKey parameters, if those are present (i.e., if an index is available). sel(a > c) = MaxKey(I) c MaxKey(I) MinKey(I) If A is not an arithmetic type or there is no index, a fraction less than half is arbitrarily chosen. Similar estimates can be derived for other forms of range selections. For selections of the form A IN (List of values), compute the selectivity of A = c and multiply by the number of values in the list. Note that this number can be the result of a complex selectivity estimation in case of SQL s nested subqueries. However, never use a resulting value greater than 1 2, since we believe that each selection eliminates at least half of the input tuples. 289

Other systems I Estimating query characteristics in commercial DBMSs DB2, Informix, and Oracle use one-dimensional equal height histograms. Oracle switches to duplicate counts for each value, whenever there are only few distinct values. MS SQL Server uses one-dimensional equal area histograms with some optimization (compression of adjacent ranges with similar distributions). SQL Server creates and maintains histograms automatically, without user interaction. Sampling is typically not used directly in commercial systems. Sometimes, utilities use sampling for estimating statistics or for building histograms. Sometimes sampling is used for load balancing during parallelization. 290

Bibliography Ceri, S. and Pelagatti, G. (1984). Distributed Databases, Principles and Systems. McGraw- Hill. Ling, Y. and Sun, W. (1992). A supplement to sampling-based methods for query size estimation in a database system. SIGMOD Record, 21(4):12 15. Ling, Y. and Sun, W. (1995). An evaluation of sampling-based size estimation methods for selections in database systems. In Yu, P. S. and Chen, A. L. P., editors, Proc. 11th IEEE Int l Conf. on Data Engineering, pages 532 539, Taipei, Taiwan. Mannino, M. V., Chu, P., and Sager, T. (1988). Statistical profile estimation in database systems. ACM Computing Surveys, 20(3):191 221. Mullin, J. (1993). Estimating the size of a relational join. Information Systems, 18(3):189 196. Ramakrishnan, R. and Gehrke, J. (2003). Database Management Systems. McGraw-Hill, New York, 3 edition. 291