Outline ■Introduction Transformation of Relational Expressions Catalog Information for Cost Estimation Statistical Information for Cost Estimation Cost-based optimization Dynamic Programming for Choosing Evaluation Plans ■Materialized views Database System Concepts-7th Edition 16.2 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 16.2 ©Silberschatz, Korth and Sudarshan th Edition Outline ▪ Introduction ▪ Transformation of Relational Expressions ▪ Catalog Information for Cost Estimation ▪ Statistical Information for Cost Estimation ▪ Cost-based optimization ▪ Dynamic Programming for Choosing Evaluation Plans ▪ Materialized views
Introduction Alternative ways of evaluating a given query Equivalent expressions 。 Different algorithms for each operation I name,title Odept name-Music Iname,title instructor Odept name-Music teaches Icourse id.tite instructor teaches Icourse id.tirle course course (a)Initial expression tree (b)Transformed expression tree Database System Concepts-7th Edition 16.3 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 16.3 ©Silberschatz, Korth and Sudarshan th Edition Introduction ▪ Alternative ways of evaluating a given query • Equivalent expressions • Different algorithms for each operation
Introduction (Cont.) An evaluation plan defines exactly what algorithm is used for each operation,and how the execution of the operations is coordinated. (sort to remove duplicates) Π ame,title (merge join) sortD sorti ☒(hash join) dept name-Music course id,title (use index 1) instructor teaches course Find out how to view query execution plans on your favorite database Database System Concepts-7th Edition 16.4 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 16.4 ©Silberschatz, Korth and Sudarshan th Edition Introduction (Cont.) ▪ An evaluation plan defines exactly what algorithm is used for each operation, and how the execution of the operations is coordinated. ▪ Find out how to view query execution plans on your favorite database
Introduction (Cont.) Cost difference between evaluation plans for a query can be enormous E.g.,seconds vs.days in some cases Steps in cost-based query optimization 1.Generate logically equivalent expressions using equivalence rules 2.Annotate resultant expressions to get alternative query plans 3.Choose the cheapest plan based on estimated cost Estimation of plan cost based on: Statistical information about relations.Examples: number of tuples,number of distinct values for an attribute Statistics estimation for intermediate results to compute cost of complex expressions Cost formulae for algorithms,computed using statistics Database System Concepts-7th Edition 16.5 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 16.5 ©Silberschatz, Korth and Sudarshan th Edition Introduction (Cont.) ▪ Cost difference between evaluation plans for a query can be enormous • E.g., seconds vs. days in some cases ▪ Steps in cost-based query optimization 1. Generate logically equivalent expressions using equivalence rules 2. Annotate resultant expressions to get alternative query plans 3. Choose the cheapest plan based on estimated cost ▪ Estimation of plan cost based on: • Statistical information about relations. Examples: ▪ number of tuples, number of distinct values for an attribute • Statistics estimation for intermediate results ▪ to compute cost of complex expressions • Cost formulae for algorithms, computed using statistics
Viewing Query Evaluation Plans Most database support explain <query> Displays plan chosen by query optimizer,along with cost estimates Some syntax variations between databases Oracle:explain plan for <query>followed by select*from table (dbms_xplan.display) SQL Server:set showplan_text on Some databases(e.g.PostgreSQL)support explain analyse <query> Shows actual runtime statistics found by running the query,in addition to showing the plan Some databases(e.g.PostgreSQL)show cost as f./ f is the cost of delivering first tuple and is cost of delivering all results Database System Concepts-7th Edition 16.6 ©Silberscha乜,Korth and Sudarshan
Database System Concepts - 7 16.6 ©Silberschatz, Korth and Sudarshan th Edition Viewing Query Evaluation Plans ▪ Most database support explain <query> • Displays plan chosen by query optimizer, along with cost estimates • Some syntax variations between databases ▪ Oracle: explain plan for <query> followed by select * from table (dbms_xplan.display) ▪ SQL Server: set showplan_text on ▪ Some databases (e.g. PostgreSQL) support explain analyse <query> • Shows actual runtime statistics found by running the query, in addition to showing the plan ▪ Some databases (e.g. PostgreSQL) show cost as f..l • f is the cost of delivering first tuple and l is cost of delivering all results