Query Tuning.

Slides:



Advertisements
Similar presentations
Physical Database Design and Tuning R&G - Chapter 20 Although the whole of this life were said to be nothing but a dream and the physical world nothing.
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
AOBD07/08H. Galhardas Query Tuning. H. Galhardas AOBD07/08 Query Tuning SELECT s.RESTAURANT_NAME, t.TABLE_SEATING, to_char(t.DATE_TIME,'Dy, Mon FMDD')
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
Manajemen Basis Data Pertemuan 7 Matakuliah: M0264/Manajemen Basis Data Tahun: 2008.
DB performance tuning using indexes Section 8.5 and Chapters 20 (Raghu)
1 Physical Database Design and Tuning Module 5, Lecture 3.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Physical Database Monitoring and Tuning the Operational System.
DAY 16: ACCESS CHAPTER 2 Tazin Afrin October 10,
Practical Database Design and Tuning. Outline  Practical Database Design and Tuning Physical Database Design in Relational Databases An Overview of Database.
Database Tuning Prerequisite Cluster Index B+Tree Indexing Hash Indexing ISAM (indexed Sequential access)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Chapter 16 Practical Database Design and Tuning Copyright © 2004 Pearson Education, Inc.
Physical Database Design I, Ch. Eick 1 Physical Database Design I About 25% of Chapter 20 Simple queries:= no joins, no complex aggregate functions Focus.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Management COP4540, SCS, FIU Physical Database Design (2) (ch. 16 & ch. 6)
Methodology – Physical Database Design for Relational Databases.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Query Tuning. Types of Nested Queries Uncorrelated subqueries with aggregates in the nested query SELECT ssnum FROM employee WHERE salary > (select avg(salary)
Lec 7 Practical Database Design and Tuning Copyright © 2004 Pearson Education, Inc.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
1 Chapter 4. Tuning a Relational Database System May 2002 Prof. Sang Ho Lee School of Computing, Soongsil University
1 Chapter 3. Tuning Indexes May 2002 Prof. Sang Ho Lee School of Computing, Soongsil University
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Query Tuning. overview Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept;
1 CS122A: Introduction to Data Management Lecture #15: Physical DB Design Instructor: Chen Li.
Practical Database Design and Tuning
Tuning Transact-SQL Queries
COP Introduction to Database Structures
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Physical Changes That Don’t Change the Logical Design
Database Management System
Chapter 2: Relational Model
Introduction to Database Systems, CS420
Methodology – Physical Database Design for Relational Databases
Physical Database Design for Relational Databases Step 3 – Step 8
Chapter 12: Query Processing
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Chapter 15 QUERY EXECUTION.
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
Evaluation of Relational Operations: Other Operations
Using the Set Operators
國立臺北科技大學 課程:資料庫系統 fall Chapter 18
Relational Operations
Module 5: Overview of Normalization
Practical Database Design and Tuning
SQL: The Query Language Part 1
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
SQL: Structured Query Language
Tuning Queries from (E&N)
Chapter 5 Advanced Data Modeling
Advance Database Systems
Overview of Query Evaluation
Implementation of Relational Operations
Projecting output in MySql
Evaluation of Relational Operations: Other Techniques
Lecture 5- Query Optimization (continued)
IST 318 Database Administration
Manipulating Data Lesson 3.
Evaluation of Relational Operations: Other Techniques
Chapter 7a: Overview of Database Design -- Normalization
SQL: The Query Language (Part III)
Presentation transcript:

Query Tuning

overview

Query Monitoring Two ways to identify a slow query: It issues far too many disk accesses, e.g., a point query scans an entire table Its query plan, i.e. the plan chosen by the optimizer to execute the query, fails to use promising indexes

Query Rewriting The first tuning method to try is the one whose effects are purely local Adding an index, changing the schema, modifying transactions have global effects that are potentially harmful Query rewriting only impacts a particular query

Running Example Employee(ssnum, name, manager, dept, salary, numfriends) Clustering index on ssnum Non clustering indexes (i) on name and (ii) on dept Ssnum determines all the other attributes Student(ssnum, name, degree_sought, year) Non clustering index on name Tech(dept, manager, location) Clustering index on dept; dept is key.

Query Rewriting Techniques Index usage DISTINCTs elimination (Correlated) subqueries Use of temporaries Join conditions Use of Having Use of views Materialized views.

Index Usage Many query optimizers will not use indexes in the presence of: Arithmetic expressions WHERE salary/12 >= 4000; Substring expressions SELECT * FROM employee WHERE SUBSTR(name, 1, 1) = ‘G’; Numerical comparisons of fields with different types Comparison with NULL.

Avoid Redundant DISTINCT DISTINCT usually entails a sort operation Slow down query optimization because one more “interesting” order to consider Remove if you know the result has no duplicates SELECT DISTINCT ssnum FROM Employee WHERE dept = ‘information systems’

Eliminate unneeded DISTINCTs Query: Find employees who work in the information systems department. There should be no duplicates. SELECT distinct ssnum FROM employee WHERE dept = ‘information systems’ DISTINCT is unnecessary, since ssnum is a key of employee so certainly is a key of a subset of employee.

Eliminate unneeded DISTINCTs Query: Find social security numbers of employees in the technical departments. There should be no duplicates. SELECT DISTINCT ssnum FROM employee, tech WHERE employee.dept = tech.dept Is DISTINCT needed? Since dept is a key of the tech table, each employee record will join with at most one record in tech. Because ssnum is a key for employee, distinct is unnecessary.

Reaching The relationship among DISTINCT, keys and joins can be generalized: Call a table T privileged if the fields returned by the select contain a key of T Let R be an unprivileged table. Suppose that R is joined on equality by its key field to some other table S, then we say R reaches S. Now, define reaches to be transitive. So, if R1 reaches R2 and R2 reaches R3 then say that R1 reaches R3.

Reaches: Main Theorem There will be no duplicates among the records returned by a selection, even in the absence of DISTINCT if one of the two following conditions hold: Every table mentioned in the FROM clause is privileged Every unprivileged table reaches at least one privileged table.

Reaches: Proof Sketch If every relation is privileged then there are no duplicates The keys of those relations are in the from clause Suppose some relation T is not privileged but reaches at least one privileged one, say R. Then the qualifications linking T with R ensure that each distinct combination of privileged records is joined with at most one record of T.

Reaches: Example 1 The same employee record may match several tech records (because manager is not a key of tech), so the ssnum of that employee record may appear several times. Tech does not reach the privileged relation employee. SELECT ssnum FROM employee, tech WHERE employee.manager = tech.manager

Reaches: Example 2 Each repetition of a given ssnum vlaue would be accompanied by a new tech.dept since tech.dept is a key of tech Both relations are privileged. SELECT ssnum, tech.dept FROM employee, tech WHERE employee.manager = tech.manager

Reaches: Example 3 Student is priviledged Employee does not reach student (name is not a key of employee) DISTINCT is needed to avoid duplicates. SELECT student.ssnum FROM student, employee, tech WHERE student.name = employee.name AND employee.dept = tech.dept;

summary