query processing
play

Query Processing 5DV120 Database System Principles Ume a - PowerPoint PPT Presentation

Query Processing 5DV120 Database System Principles Ume a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner Query Processing 20130530 Slide 1 of 62 Overview Question: How is a


  1. Query Processing 5DV120 — Database System Principles Ume˚ a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner Query Processing 20130530 Slide 1 of 62

  2. Overview Question: How is a query in SQL processed by the system to produce the answer? • The block diagram below identifies the three main steps. Lexer + Parser + View Resolver: In the first step, the (declarative) SQL query is translated to an operational query, usually but not always expressed in the relational algebra. Query Optimizer: In the second step, the algebraic representation is augmented to be an execution plan , with possible rearrangement of the algebraic operations as well as implementation-specific information on how to evaluate in an efficient fashion. Code Generator and Evaluator: In the final step, the plan of execution is carried out. Lexer Algebraic SQL Execution Result Query Code Generator Parser Optimizer and Evaluator Source Rep. Plan View Resolver Query Processing 20130530 Slide 2 of 62

  3. Scanning, Parsing, and View Resolution • Lexical analysis and parsing are carried out using well-known techniques from translator design. Lexical analysis: The lexer (or lexical analyzer or tokenzier ) breaks the input stream up into tokens . Parsing: The parser builds a parse tree for the tokens according to a grammar for the language. • These topics will not be considered in this course. View resolution: corresponds to the generation of an intermediate representation in programming-language translator. • In the case of SQL, the relational algebra is often but not always used the the intermediate language. • The relationship between SQL and the relational algebra, as well as how to translate queries from one to the other, has already been studied in the introductory course. • The topic of how to obtain an equivalent expression in the relational algebra from a query in SQL will not be considered further here. Query Processing 20130530 Slide 3 of 62

  4. Query Optimization • Query optimization involves at least two distinct processes. Query rewriting: A given expression in the relational algebra ( e.g. , the output of the lexer + parser + view resolver) may be represented by an equivalent expression which is amenable to more efficient evaluation. Annotation: An expression in the relational algebra may be annotated with specific information on how to carry out its steps, such as: • which algorithms to use; • which indices to use. • These topics will be examined in these slides. Lexer Algebraic SQL Execution Result Query Code Generator Parser Optimizer and Evaluator Source Rep. Plan View Resolver Query Processing 20130530 Slide 4 of 62

  5. Code Generation and Evaluation • Executing an execution plan is a relatively straightforward process, although there are certainly nontrivial details which must be addressed. • This task will not be examined further in these slides. • Thus, the focus will be upon query optimization. Lexer Algebraic SQL Execution Result Query Code Generator Parser Optimizer and Evaluator Source Rep. Plan View Resolver Query Processing 20130530 Slide 5 of 62

  6. Code Generation and Evaluation • Executing an execution plan is a relatively straightforward process, although there are certainly nontrivial details which must be addressed. • This task will not be examined further in these slides. • Thus, the focus will be upon query optimization. Lexer Algebraic SQL Execution Result Query Code Generator Parser Optimizer and Evaluator Source Rep. Plan View Resolver Query Processing 20130530 Slide 5 of 62

  7. Executing Operations in the Relational Algebra • In these slides, the relational algebra will be used as the intermediate language for (unannotated) execution plans. • Therefore, it is important to begin with a study of algorithms for the following, under a variety of conditions for index availability. projection selection join removal of duplicates ordering of results aggregation Query Processing 20130530 Slide 6 of 62

  8. Basic Measures of Cost for Data Access • Recall that access to secondary storage (usually hard disks) takes much longer (thousands of times longer) than access to primary storage (main memory). • Minimizing the number of times that secondary storage must be accessed is therefore paramount in the design of efficient algorithms for query processing. • It is useful to begin with some basic parameters for disk access. t S : The (average) time required to access one block of data (seek time + rotational latency). t T : The (average) time required to transfer one block of data from secondary to primary storage. • An operation which requires n s seeks to transfer n b blocks thus requires a total time of n s · t S + n b · t T . • The relationship between n s and n b depends upon how the required blocks are arranged on the secondary device (random vs. sequential neighbors). Query Processing 20130530 Slide 7 of 62

  9. Cost Measures Associated with Indices • If an attribute is indexed and a query involves that attribute, then it is often the case that an optimal evaluation algorithm will involve access via that attribute. h i : For an index which is a B + -tree, the depth of the index; i.e. , one less than the length of a path from the root to a leaf. • As noted previously, it is access to secondary storage which is the prime consumer of time. • Therefore, it is appropriate to decompose h i = h i pri + h i sec as follows. h i pri : In a path from the root to a leaf, the (average) number of pointers whose destination is already in main memory. h i sec : In a path from the root to a leaf, the (average) number of pointers whose destination is not in main memory. Disk access rule of thumb: In most cases, access times along h i pri may be ignored, since they will be thousands of times less than for h i sec . Query Processing 20130530 Slide 8 of 62

  10. Cases for Selection • Selection is the most basic operation of the relational algebra which involves the use of indices. • It is convenient to decompose access into a number of cases. Simple cases: A is an attribute and e is a fixed expression which may be evaluated in constant time. Equality on a single attribute: σ A = e Inequality on a single attribute: σ A � = e Simple range on a single attribute: σ A ≤ e , σ A ≥ e , σ A < e , σ A > e Compound cases: θ and each θ i is a simple condition A i ⊙ e i with ⊙ ∈ { = , ≤ , <, ≥ , >, � = } . Conjunction: σ θ 1 ∧ θ 2 ∧ ... ∧ θ k Disjunction: σ θ 1 ∨ θ 2 ∨ ... ∨ θ k Negation: σ ¬ θ (Not really needed as a separate case, since, for example σ ¬ ( A ≤ e ) is the same as σ A > e , but care must be taken to handle null values correctly.) Query Processing 20130530 Slide 9 of 62

  11. Simple Selection without Index Support • The case labels used here match those of the textbook. n blk : The number of blocks used for the file containing the relation. A1: Linear search: The entire relation is searched for tuples which match the condition A ⊙ e , where ⊙ ∈ { = , ≤ , <, ≥ , >, � = } . Total cost (time): t S + n blk · t T . • t S only occurs once because the data are searched sequentially. A1: Linear search; equality on a key: The entire relation is searched for tuples which match the condition A = e . Worst-case total cost (time): t S + n blk · t T . Best-case total cost (time): t S + t T . Average-case total cost (time): t S + ( n blk / 2) · t T . • On the average, about half of the file must be searched before the key is found. Query Processing 20130530 Slide 10 of 62

  12. Simple Selection with Primary Index Support A2: Primary B + -tree index on a key; equality search on the index attribute: This is the best of all possible cases. All-cases total cost (time): ( h i sec + 1) · ( t S + t T ). • There is one seek plus disk access for each level of the index which is not in main memory, plus one more to reach the block containing the desired record A3: Primary B + -tree index on a non-key; equality search on index attribute: This is almost as good, but more than one block may need to be retrieved. All-cases total cost (time): ( h i sec + 1) · t S + ( h i sec + n rec ) · t T . • The number of seeks is exactly as in the previous case. • The number of transfers is determined by the number n rec of blocks which contain records matching the key. • The times will be even less if the block and/or more of the index is cached. Query Processing 20130530 Slide 11 of 62

  13. Simple Selection with Secondary Index Support A4: Secondary B + -tree index on key; equality search on index attribute: The analysis is the similar to the case for a primary index, since only one record is retrieved. All-cases total cost (time): ( h i sec + 2) · ( t S + t T ). • There is one more seek+access than for a primary index — the leaves of the B + -tree will contain references, not actual records. A4: Secondary B + -tree index on non-key; equality search on index attribute: Here the records to be found need not lie in the same block, or even in contiguous blocks. Worst-case total cost (time): ( h i sec + 1 + n rec ) · ( t S + t T ). • n rec is the number of records which are retrieved. • There is one seek and one access for each such record. • There is one additional seek+access per record because the leaves of the B + -tree will contain references and not the actual records. • The average-case is likely not much better. • The times will be less if the blocks and/or more of the index is cached. Query Processing 20130530 Slide 12 of 62

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend