Introduction to SQL Tuning Brown Bag Three essential concepts.

Slides:



Advertisements
Similar presentations
Using the SQL Access Advisor
Advertisements

Youre Smarter than a Database Overcoming the optimizers bad cardinality estimates.
Structured Query Language (SQL)
Maria Colgan & Thierry Cruanes
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 116.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 40.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 28.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 38.
Structured Query Language (SQL)
10 Copyright © 2005, Oracle. All rights reserved. Dimensions.
1.
Examples of Physical Query Plan Alternatives
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Understanding SQL Server Query Execution Plans
1 Proven Process for SQL Tuning Dean Richards Senior DBA, Confio Software.
Database Performance Tuning and Query Optimization
M ODULE 4 D ATABASE T UNING Section 3 Application Performance 1 ITEC 450 Fall 2012.
Overview of performance tuning strategies Oracle Performance Tuning Allan Young June 2008.
© Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi © Bharati Vidyapeeths Institute of Computer Applications and.
Database Performance Tuning and Query Optimization
Yong Choi School of Business CSU, Bakersfield
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
Overview of Query Evaluation (contd.) Chapter 12 Ramakrishnan and Gehrke (Sections )
Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 12, Part A.
Jonathan Lewis EOUG Jun 2000 Execution Plans Agenda What are execution plans Where do you find execution plans Key mechanisms of execution Understanding.
What Happens when a SQL statement is issued?
Exadata Distinctives Brown Bag New features for tuning Oracle database applications.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Finding the Performance Bottlenecks in Your Application Ian Jones and Roger Schrag Database Specialists, Inc. IOUG-A Live! 1999 Paper.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
AN INTRODUCTION TO EXECUTION PLAN OF QUERIES These slides have been adapted from a presentation originally made by ORACLE. The full set of original slides.
Relational Database Performance CSCI 6442 Copyright 2013, David C. Roberts, all rights reserved.
Executing Explain Plans and Explaining Execution Plans Craig Martin 01/20/2011.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Star Transformations Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
Module 4 Database SQL Tuning Section 3 Application Performance.
Indexes and Views Unit 7.
University of Sunderland COM 220 Lecture Ten Slide 1 Database Performance.
1 Chapter 13 Parallel SQL. 2 Understanding Parallel SQL Enables a SQL statement to be: – Split into multiple threads – Each thread processed simultaneously.
J.NemecAre Your Statistics Bad Enough?1 Verify the effectiveness of gathering optimizer statistics Jaromir D.B. Nemec UKOUG
Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Sorting and Joining.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
CS 540 Database Management Systems
Query Tuning without Production Data
Choosing Access Path The basic methods.
Optimizing SQL Queries
Introduction to Execution Plans
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Databases & Consistency
Introduction to Execution Plans
Contents Preface I Introduction Lesson Objectives I-2
Introduction to Execution Plans
Database SQL.
Introduction to Execution Plans
Presentation transcript:

Introduction to SQL Tuning Brown Bag Three essential concepts

Introduction to SQL Tuning How to speed up a slow query? Find a better way to run the query Cause the database to run the query your way

Introduction to SQL Tuning How does a database run a SQL query? Join order Join method Access method

Example Query SQL> select 2 sale_date, product_name, customer_name, amount 3 from sales, products, customers 4 where 5 sales.product_number=products.product_number and 6 sales.customer_number=customers.customer_number and 7 sale_date between 8 to_date('01/01/2012','MM/DD/YYYY') and 9 to_date('01/31/2012','MM/DD/YYYY') and 10 product_type = 'Cheese' and 11 customer_state = 'FL'; SALE_DATE PRODUCT_NAME CUSTOMER_NAME AMOUNT JAN-12 Feta Sunshine State Co JAN-12 Chedder Sunshine State Co JAN-12 Feta Green Valley Inc JAN-12 Chedder Green Valley Inc 200

Join Order Join Order = order in which tables in from clause are joined Two row sources at a time Row source: Table Result of join View as tree – execution tree or plan

Join Order – sales, products, customers productssales join 1customers join 2

Join Order as Plan Execution Plan SELECT STATEMENT 1 0 HASH JOIN 2 1 HASH JOIN 3 2 TABLE ACCESS (FULL) OF 'SALES' (TABLE) 4 2 TABLE ACCESS (FULL) OF 'PRODUCTS' (TABLE 5 1 TABLE ACCESS (FULL) OF 'CUSTOMERS' (TABLE)

Bad Join Order – customers, products, sales productscustomers join 1sales join 2

Cartesian Join – all products to all customers SQL> -- joining products and customers SQL> -- cartesian join SQL> SQL> select 2 product_name,customer_name 3 from products, customers 4 where 5 product_type = 'Cheese' and 6 customer_state = 'FL'; PRODUCT_NAME CUSTOMER_NAME Chedder Sunshine State Co Chedder Green Valley Inc Feta Sunshine State Co Feta Green Valley Inc

Plan with Cartesian Join Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 MERGE JOIN (CARTESIAN) 2 1 TABLE ACCESS (FULL) OF 'PRODUCTS' (TABLE) 3 1 BUFFER (SORT) 4 3 TABLE ACCESS (FULL) OF 'CUSTOMERS' (TABLE)

Selectivity Selectivity = percentage of rows accessed versus total rows Use non-joining where clause predicates sale_date, product_type, customer_state Compare count of rows with and without non-joining predicates

Count(*) to get selectivity -- # selected rows select count(*) from sales where sale_date between to_date('01/01/2012','MM/DD/YYYY') and to_date('01/31/2012','MM/DD/YYYY'); -- total #rows select count(*) from sales;

Selectivity of sub-tree SQL> select count(*) from sales, products 3 where 4 sales.product_number=products.product_number and 5 sale_date between 6 to_date('01/01/2012','MM/DD/YYYY') and 7 to_date('01/31/2012','MM/DD/YYYY') and 8 product_type = 'Cheese'; COUNT(*) SQL> select count(*) 2 from sales, products 3 where 4 sales.product_number=products.product_number; COUNT(*)

Modifying the Join Order Tables with selective predicates first Gather Optimizer Statistics Estimate Percent Histogram on Column Cardinality Hint Leading Hint Break Query into Pieces

Gather Optimizer Statistics set preferences begin DBMS_STATS.SET_TABLE_PREFS(NULL,'SALES','ESTIMATE_PERCENT','10'); DBMS_STATS.SET_TABLE_PREFS(NULL,'SALES','METHOD_OPT', 'FOR COLUMNS SALE_DATE SIZE 254 PRODUCT_NUMBER SIZE 1 '|| 'CUSTOMER_NUMBER SIZE 1 AMOUNT SIZE 1'); end; / regather table stats with new preferences execute DBMS_STATS.GATHER_TABLE_STATS (NULL,'SALES');

Cardinality Hint SQL> select /*+cardinality(sales 1) */ 2 sale_date, product_name, customer_name, amount 3 from sales, products, customers 4 where 5 sales.product_number=products.product_number and 6 sales.customer_number=customers.customer_number and 7 sale_date between 8 to_date('01/01/2012','MM/DD/YYYY') and 9 to_date('01/31/2012','MM/DD/YYYY') and 10 product_type = 'Cheese' and 11 customer_state = 'FL'; SALE_DATE PRODUCT_NAME CUSTOMER_NAME AMOUNT JAN-12 Feta Sunshine State Co JAN-12 Chedder Sunshine State Co JAN-12 Feta Green Valley Inc JAN-12 Chedder Green Valley Inc 200

Plan with Cardinality hint Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 HASH JOIN 2 1 HASH JOIN 3 2 TABLE ACCESS (FULL) OF 'SALES' (TABLE) 4 2 TABLE ACCESS (FULL) OF 'PRODUCTS' (TABLE 5 1 TABLE ACCESS (FULL) OF 'CUSTOMERS' (TABLE)

Leading Hint SQL> select /*+leading(sales) */ 2 sale_date, product_name, customer_name, amount 3 from sales, products, customers 4 where 5 sales.product_number=products.product_number and 6 sales.customer_number=customers.customer_number and 7 sale_date between 8 to_date('01/01/2012','MM/DD/YYYY') and 9 to_date('01/31/2012','MM/DD/YYYY') and 10 product_type = 'Cheese' and 11 customer_state = 'FL'; SALE_DATE PRODUCT_NAME CUSTOMER_NAME AMOUNT JAN-12 Feta Sunshine State Co JAN-12 Chedder Sunshine State Co JAN-12 Feta Green Valley Inc JAN-12 Chedder Green Valley Inc 200

Break Query Into Pieces SQL> create global temporary table sales_product_results 2 ( 3 sale_date date, 4 customer_number number, 5 amount number, 6 product_type varchar2(12), 7 product_name varchar2(12) 8 ) on commit preserve rows; Table created.

Break Query Into Pieces SQL> insert /*+append */ 2 into sales_product_results 3 select 4 sale_date, 5 customer_number, 6 amount, 7 product_type, 8 product_name 9 from sales, products 10 where 11 sales.product_number=products.product_number and 12 sale_date between 13 to_date('01/01/2012','MM/DD/YYYY') and 14 to_date('01/31/2012','MM/DD/YYYY') and 15 product_type = 'Cheese'; 4 rows created.

Break Query Into Pieces SQL> select 2 sale_date, product_name, customer_name, amount 3 from sales_product_results spr, customers c 4 where 5 spr.customer_number=c.customer_number and 6 c.customer_state = 'FL'; SALE_DATE PRODUCT_NAME CUSTOMER_NAME AMOUNT JAN-12 Chedder Sunshine State Co JAN-12 Chedder Green Valley Inc JAN-12 Feta Sunshine State Co JAN-12 Feta Green Valley Inc 400

Join Methods Join Method = way that data from two sources is joined Nested Loops Small number of rows in first table Unique index on second large table Hash Join Smaller or equal number of rows in first table No index required

Join Method – Nested Loops Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'CUSTOMERS' (TABLE) 2 1 NESTED LOOPS 3 2 NESTED LOOPS 4 3 TABLE ACCESS (FULL) OF 'SALES' (TABLE) 5 3 TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' 6 5 INDEX (RANGE SCAN) OF 'PRODUCTS_INDEX' (INDEX) 7 2 INDEX (RANGE SCAN) OF 'CUSTOMERS_INDEX' (INDEX)

Join Method – Hash Join Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 HASH JOIN 2 1 HASH JOIN 3 2 TABLE ACCESS (FULL) OF 'SALES' (TABLE) 4 2 TABLE ACCESS (FULL) OF 'PRODUCTS' 5 1 TABLE ACCESS (FULL) OF 'CUSTOMERS' (TABLE)

Modifying the Join Method Hints use_hash use_nl Add Index Hash_area_size parameter

Join Methods Hints /*+ use_hash(products) use_nl(customers) */

Join Methods Indexes create index products_index on products(product_number); create index customers_index on customers(customer_number);

Join Methods Hash_Area_Size NAME TYPE VALUE hash_area_size integer sort_area_size integer workarea_size_policy string MANUAL

Access Methods Access method = way that data is retrieved from table Index scan – small number of rows accessed Full scan – larger number of rows accessed

Modifying the Access Method Set Initialization Parameter optimizer_index_caching optimizer_index_cost_adj db_file_multiblock_read_count Set Parallel Degree > 1 Hints Full Index

Set Initialization Parameter alter system set optimizer_index_cost_adj=1000 scope=both sid='*';

Set Parallel Degree alter table sales parallel 8 ;

Full Scan and Index Hints /*+ full(sales) index(customers) index(products) */

Conclusion Use count queries to determine selective parts of where clause Modify the join order, join methods, and access methods using Optimizer statistics Hints Initialization parameters Breaking the query into pieces Parallel degree Indexes Compare elapsed time of query with new plan to original

Check For Improved Elapsed Time SQL> set timing on SQL> SQL> select … … removed for clarity … SALE_DATE PRODUCT_NAME CUSTOMER_NAME AMOUNT JAN-12 Chedder Sunshine State Co JAN-12 Chedder Green Valley Inc JAN-12 Feta Sunshine State Co JAN-12 Feta Green Valley Inc 400 Elapsed: 00:00:00.00

Further Reading Oracle Database Concepts Chapter 7 SQL Oracle Database Performance Tuning Guide Chapter 11 The Query Optimizer Chapter 19 Using Optimizer Hints Oracle Database Reference Chapter 1 Initialization Parameters Oracle Database PL/SQL Packages and Types Reference Chapter 141 DBMS_STATS Cost-Based Oracle Fundamentals - Jonathan Lewis