1 / 37

Query Optimization

Query Optimization. CS 157B Ch. 14 Mien Siao. Outline. Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction in DBMS Cost-based query Optimization: Algebraic Expressions. Introduction. What is Query Optimization?

Download Presentation

Query Optimization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Query Optimization CS 157B Ch. 14 Mien Siao

  2. Outline • Introduction • Steps in Cost-based query optimization- Query Flow • Projection Example • Query Interaction in DBMS • Cost-based query Optimization: Algebraic Expressions

  3. Introduction • What is Query Optimization? • Suppose you were given a chance to visit 15 pre-selected different cities in Europe. The only constraint would be ‘Time’ -> Would you have a plan to visit the cities in any order?

  4. Europe

  5. Plan: -> Place the 15 cities in different groups based on their proximity to each other. -> Start with one group and move on to the next group. Important point made over here is that you would have visited the cities in a more organized manner, and the ‘Time’ constraint mentioned earlier would have been dealt with efficiently.

  6. Query Optimization works in a similar way: There can be many different ways to get an answer from a given query. The result would be same in all scenarios. DBMS strive to process the query in the most efficient way (in terms of ‘Time’) to produce the answer. Cost = Time needed to get all answers

  7. Starting with System-R, most of the commercial DBMSs use cost-based optimizers. • The estimation should be accurate and easy. Another important point is the need for being logically consistent because the least cost plan will always be consistently low.

  8. Steps in a Cost-based query optimization • Parsing • Transformation • Implementation • Plan selection based on cost estimates

  9. Basic Steps in Query Processing 1. Parsing and translation 2. Optimization 3. Evaluation

  10. Basic Steps in Query Processing (Cont.) • Parsing and translation • translate the query into its internal form. This is then translated into relational algebra. • Parser checks syntax, verifies relations • Evaluation • The query-execution engine takes a query-evaluation plan, executes that plan, and returns the answers to the query.

  11. Query Flow SQL Parser Optimizer Code Generator/Interpreter Processor

  12. Query Parser – Verify validity of the SQL statement. Translate query into an internal structure using relational calculus. • Query Optimizer – Find the best expression from various different algebraic expressions. Criteria used is ‘Cheapness’ • Code Generator/Interpreter – Make calls for the Query processor as a result of the work done by the optimizer. • Query Processor – Execute the calls obtained from the code generator.

  13. Measures of Query Cost • Cost is generally measured as total elapsed time for answering query • Many factors contribute to time cost • disk accesses, CPU, or even network communication • Typically disk access is the predominant cost, and is also relatively easy to estimate. Measured by taking into account • Number of seeks * average-seek-cost • Number of blocks read * average-block-read-cost • Number of blocks written * average-block-write-cost • Cost to write a block is greater than cost to read a block • data is read back after being written to ensure that the write was successful

  14. Cost of physical plans includes processor time and communication time. The most important factor to consider is disk I/Os because it is the most time consuming action. • Some other costs associated are: - Operations (joins, unions, intersections). - The order of operations. Why?

  15. Joins, unions, and intersections are associative and commutative. - Management of storage of arguments and passing of it. Factors mentioned above should be limited and minimized when creating the best physical plan.

  16. Projection Example: • Projections produce a result tuple for every argument tuple. • What is the change? • Change in the output size is the change in the length of tuples Let’s take a relation ‘R’ Relation (20,000 tuples): R(a, b, c) Each Tuple (190 bytes): header = 24 bytes, a = 8 bytes, b = 8 bytes, c = 150 bytes Each Block (1024): header = 24 bytes

  17. We can fit 5 tuples into 1 block • 5 tuples * 190 bytes/tuple = 950 bytes can fit into 1 block • For 20,000 tuples, we would require 4,000 blocks (20,000 / 5 tuples per block = 4,000 With a projection resulting in elimination of column c (150 bytes), we could estimate that each tuple would decrease to 40 bytes (190 – 150 bytes)

  18. Now, the new estimate will be 25 tuples in 1 block. • 25 tuples * 40 bytes/tuple = 1000 bytes will be able to fit into 1 block • With 20,000 tuples, the new estimate is 800 blocks (20,000 tuples / 25 tuples per block = 800 blocks) Result is reduction by a factor of 5

  19. Query interaction in DBMS • How does a query interact with a DBMS? - Interactive users - Embedded queries in programs written in C, C++, etc. • What is the difference between these two ?

  20. Interactive Users: - When there is an interactive user query, the query goes through the Query Parser, Query Optimizer, Code Generator, and Query Processor each time.

  21. Embedded Query: • When there is an embedded query, the query does not have to through the Query Parser, Query Optimizer, Code Generator, and the Query Processor each time.

  22. In an embedded query, the calls generated by the code generator are stored in the database. Each time the query is reached within the program at run-time, the Query Processor invokes the stored calls in the database. • Optimization is independent in embedded queries.

  23. Cost-based query Optimization: Algebraic Expressions If we had the following query- SELECT p.pname, d.dname FROM Patients p, Doctors d WHERE p.doctor = d.dname AND d.dgender = ‘M’

  24. projection filter join Scan (Patients) Scan (Doctors)

  25. Cost-based query Optimization : Transformation projection projection filter join join Filter Scan (Patients) Scan (Doctors) Scan(Patients) Scan(Doctors)

  26. Cost-based query Optimization: Implementation projection projection filter hash join natural join filter Scan(Patients) Scan(Doctors) Scan(Patients) Scan(Doctors)

  27. Cost-based query Optimization: Plan selection based on costs projection projection filter hash join natural join filter Scan(Patients) Scan(Doctors) Scan(Patients) Scan(Doctors) Estimated Costs = 100ms Estimated Costs = 50ms

  28. Intermission: a preview of sorting • Data can only be sorted when in memory • But tables often *much* bigger than memory • One solution: merge sort • Every one stand up • Go to the aisle by the windows • I will take 10 people at a time onto the stage • I will sort each group of 10 on last name from A to Z • Groups will then be merged

More Related