Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty Prasad Bidwai CMPE 226 Database Systems
What is Query Optimization ?? It`s a way to determine the most efficient way to execute a given query. There could be multiple ways(query plans) to execute a given query. Compare the cost of all possibilities and provide a good execution plan.
Why is it needed ? Necessary for the time critical applications with very less response time. To minimize consumption of system resources (disk access) To avoid table locking and data corruption issues(often occurred by long running queries). Necessary for database performance tuning.
How to do it ? Smart use of indexing use of aggregate tables denormalization horizontal partitioning
contd... some quick hacks: understand how database executes your query(explain plan for [SQL Query]). retrieve as little data as possible(do not use select *). store intermediate results.
Query traversal through DBMS SQL Query Evaluation Engine in DB
Query Evaluation at DB level An SQL statement expresses what you want. Query evaluation engine in DB tells the server how to do it. Typical Evaluation Flow Initially, the server validates the query as soon as it receives it Further, it translates it to a relational calculus expression Later, query optimizer picks up the query and examines execution plans for it. Next, the code generator transforms it into calls to query processor Finally, the query processor executes the query.
Query Plan The way that a statement can be physically executed is called an execution plan or a query plan. Query plans are formed by examining the possible access paths (e.g., primary index access, secondary index access, full file scan) and various relational table join techniques (e.g., merge join, hash join, product join). It consist of primitive operators like sort, index scan and merge joins Query plans are sketched as a tree: information flows from leaves to the root.
Query Optimizer The component of the evaluation engine that is responsible for computing the optimal execution plan is called query optimizer. It takes a parsed representation of a SQL query as input. It generates an efficient execution plan for the given SQL query from the space of possible execution plans. The optimizer bases its decision on its knowledge of the database content. The query optimizer is responsible for generating the input for the query processor.
Example query evaluation Tables emp(name,age,sal,dno) dept(dno,dname,floor,budget,mgr,ano) SQL Query Select name, floor from emp, dept where emp.dno=dept.dno and sal>100K
Stats of DB contents and structure
Execution Plans for Query Plan 1 with below primitive operators: Nested loop Index Scan on emp and dept Plan 2 with below primitive operators: Page level nested loops No index Plan 3 with below primitive operators: Tuple level cross product Sequential Scan to test join and selection
Query tree for 3 execution plans
Query Plan Performance Depends on order in which tables are joined Amount of I/O resources consumed Number of page reads CPU Time Amount of disk buffer space
Thank You