Exploring Best Practices and Guidelines for Tuning SQL Statements

Similar documents
Exploring Oracle Database 11g/12c Partitioning New Features and Best Practices. Ami Aharonovich Oracle ACE & OCP

Query Optimizer, Who Influences & How it works ++ optimization techniques

Querying Data with Transact SQL

SQL Gone Wild: Taming Bad SQL the Easy Way (or the Hard Way) Sergey Koltakov Product Manager, Database Manageability

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Oracle 11g Invisible Indexes Inderpal S. Johal. Inderpal S. Johal, Data Softech Inc.

Interpreting Explain Plan Output. John Mullins

Oracle Database 11g: SQL Tuning Workshop

Seminar: Presenter: Oracle Database Objects Internals. Oren Nakdimon.

Oracle Database 11gR2 Optimizer Insights

Querying Data with Transact-SQL

ORACLE DATABASE 12C INTRODUCTION

Ensuring Optimal Performance. Vivek Sharma. 3 rd November 2012 Sangam 2012

Objectives. After completing this lesson, you should be able to do the following:

Oracle 9i Application Development and Tuning

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

EXISTS NOT EXISTS WITH

Querying Data with Transact-SQL

Creating and Managing Tables Schedule: Timing Topic

Querying Data with Transact-SQL (20761)

Oracle Hints. Using Optimizer Hints

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Oracle Sql Tuning- A Framework

Oralogic Education Systems

Session id: The Self-Managing Database: Guided Application and SQL Tuning

20 Essential Oracle SQL and PL/SQL Tuning Tips. John Mullins

ORACLE PL/SQL DATABASE COURSE

Oracle Optimizer: What s New in Oracle Database 12c? Maria Colgan Master Product Manager

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

20461: Querying Microsoft SQL Server

MIS NETWORK ADMINISTRATOR PROGRAM

Oracle SQL Tuning for Developers Workshop Student Guide - Volume I

Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

Semantic Errors in Database Queries

Database Programming - Section 18. Instructor Guide

Oracle Database 11g: SQL Tuning Workshop. Student Guide

DB2 9 for z/os Selected Query Performance Enhancements

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Querying Data with Transact-SQL

Oracle Database 10g: Introduction to SQL

Now, we can refer to a sequence without having to use any SELECT command as follows:

Oracle DB-Tuning Essentials

Adaptive Cursor Sharing: An Introduction

Querying Data with Transact-SQL (761)

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins..

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Oracle Database: SQL and PL/SQL Fundamentals NEW

Data Warehouse Tuning. Without SQL Modification

Querying Data with Transact-SQL

Optimizer with Oracle Database 12c Release 2 O R A C L E W H I T E P A P E R J U N E

Oracle Database 11g: SQL and PL/SQL Fundamentals

Sanity-check the operating systems of all machines involved with user performance. By sanity-checking. Two of the biggest

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Join, Sub queries and set operators

Advanced Oracle SQL Tuning v3.0 by Tanel Poder

MTA Database Administrator Fundamentals Course

Oracle Database 12c: SQL Tuning for Developers

20761 Querying Data with Transact SQL

Oracle Database Performance Tuning, Benchmarks & Replication

Course Contents of ORACLE 9i

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

20761B: QUERYING DATA WITH TRANSACT-SQL

Oracle Developer Track Course Contents. Mr. Sandeep M Shinde. Oracle Application Techno-Functional Consultant

Infrastructure at your Service. In-Memory-Pläne für den 12.2-Optimizer: Teuer oder billig?

Subquery: There are basically three types of subqueries are:

Advanced SQL and the Power of Rewriting Queries

Querying Data with Transact-SQL

Oracle Database: Introduction to SQL Ed 2

Querying Microsoft SQL Server (MOC 20461C)

Real-World Performance Training SQL Introduction

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Sql Server Syllabus. Overview

EZY Intellect Pte. Ltd., #1 Changi North Street 1, Singapore

Exploring Oracle Database 12c Multitenant Best Practices for your Cloud

Querying Data with Transact-SQL

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

5. Single-row function

Exploring Oracle Database 12c Multitenant Best Practices for your Cloud Ami Aharonovich

Top 7 Plan Stability Pitfalls & How to Avoid Them. Neil Chandler Chandler Systems Ltd UK

AVANTUS TRAINING PTE LTD

Introduction to Oracle9i: SQL

Performance Optimization for Informatica Data Services ( Hotfix 3)

Appendix: Application of the Formal Principle for the Analysis of Performance Problems After an Oracle Migration

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

20461: Querying Microsoft SQL Server 2014 Databases

<Insert Picture Here> Inside the Oracle Database 11g Optimizer Removing the black magic

COURSE OUTLINE: Querying Microsoft SQL Server

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Querying Microsoft SQL Server

Top 5 Issues that Cannot be Resolved by DBAs (other than missed bind variables)

SQL (Structured Query Language)

Transcription:

Exploring Best Practices and Guidelines for Tuning SQL Statements Ami Aharonovich Oracle ACE & OCP Ami@DBAces.co.il

Oracle ACE Who am I Oracle Certified Professional DBA (OCP) Founder and CEO, DBAces Oracle DBA consultant and instructor, specializes in providing professional services and delivering Oracle trainings dealing with Oracle database core technologies and features Frequent speaker at the Oracle Open World annual event and various user group conferences around the globe President, Israel Oracle User Group

Agenda Parsing, Bind Peeking and Adaptive Cursor Sharing Using EXISTS Operator and the WITH Clause Writing Scalar Subqueries Oracle Database 11g SQL Query Result Cache Indexes and Nulls and Invisible Index Oracle Database 12c New Features: Adaptive Query Optimization Enhanced Statistics

Parsing Time is Important BEGIN FOR i IN 1..100000 LOOP EXECUTE IMMEDIATE 'INSERT INTO t (x,y) VALUES (' i ',''A'')'; END LOOP; END; / PL/SQL procedure successfully completed. Elapsed: 00:00:42.91 Parsing_Time_is_Important.sql

Parsing Time is Important BEGIN FOR i IN 1..100000 LOOP EXECUTE IMMEDIATE 'INSERT INTO t (x,y) VALUES (:i,''a'')' USING i; END LOOP; END; / PL/SQL procedure successfully completed. Elapsed: 00:00:08.26

Parsing Time is Important SELECT SUBSTR(sql_text,11,8) "Bind", COUNT(*), ROUND(SUM(sharable_mem)/1024) "Memory KB" FROM v$sqlarea WHERE sql_text LIKE 'INSERT%INTO t (x,y)%' GROUP BY SUBSTR(sql_text,11,8); Bind COUNT(*) Memory KB ------------ ------------ ------------ NO_BIND 9,349 131,580 USE_BIND 1 14

Bind Variable Peeking when the query is first hard parsed, the optimizer will peek at the binds in order to determine how to optimize the query Lets the optimizer determines the selectivity Since Oracle9i (9.0.1) _optim_peek_user_binds=false

Bind Variable Peeking SELECT * FROM jobs WHERE min_salary >:min_sal 12000 Plan A First time you execute Next time you execute min_sal=12000 SELECT * FROM jobs WHERE min_salary >:min_sal 18000 18000

Bind Variable Peeking SELECT * FROM jobs WHERE min_salary > :min_sal 1 min_sal=12000 2 min_sal=18000 Plan A 3 min_sal=7500 One plan not always appropriate for all bind values

11g Adaptive Cursor Sharing One plan not always appropriate for all bind values Provides intelligent cursor sharing for statements that use bind variables and allows the optimizer to generate a set of plans that are optimal for different sets of bind values Adaptive Cursor Sharing benefits: Automatically detects when different executions would benefit from different execution plans Limits the number of generated child cursors to a minimum Automated mechanism that cannot be turned off Adaptive_Cursor_Sharing.sql

Inefficient SQL: Examples 1 SELECT p.prod_id,p.prod_name,p.prod_list_price FROM SH.products p WHERE p.prod_list_price > 5 * (SELECT AVG(unit_cost) FROM SH.costs c WHERE c.prod_id = p.prod_id); 2 3 SELECT * FROM job_history jh, employees e WHERE substr(to_char(e.employee_id),2) = substr(to_char(jh.employee_id),2); SELECT * FROM orders WHERE order_id_char = 1205; 4 SELECT * FROM employees WHERE to_char(salary) = :sal; 5 SELECT * FROM parts_old UNION SELECT * FROM parts_new;

Which One is Better? SELECT p.prod_id, p.prod_name, p.prod_list_price FROM SH.products p WHERE p.prod_list_price > 5 * (SELECT AVG(unit_cost) FROM SH.costs c WHERE c.prod_id = p.prod_id); Cost = 222K / Elapsed = 00:00:01.38 SELECT p.prod_id, p.prod_name, p.prod_list_price FROM SH.products p, (SELECT prod_id, AVG(unit_cost) avg_cost FROM SH.costs GROUP BY prod_id) c WHERE p.prod_id = c.prod_id AND p.prod_list_price > 5 * c.avg_cost; Cost = 991 / Elapsed = 00:00:00.26 Which_One_is_Better.sql

WITH Clause Define a query block before using it in a query Use the same query block in a select statement when it occurs more than once within a complex query Particularly useful when a query has many references to the same query block and there are joins and aggregations

WITH Clause Benefits Makes the query easier to read Evaluates a clause only once, even if it appears multiple times in the query In most cases, may improve performance for large queries Which_One_is_Better_2.sql

Use a Join Not Always In general, you use a join when you need data from more than one table in the ultimate SELECT list When you need data only from one table, it is unlikely that you would consider a join. Instead consider using either WHERE EXISTS or WHERE IN SELECT DISTINCT d.dname,d.loc FROM emp e,dept d WHERE e.deptno=d.deptno; SELECT d.dname,d.loc FROM dept d WHERE EXISTS ( SELECT 1 FROM emp e WHERE e.deptno = d.deptno);

Using the EXISTS Operator Used to test for existence of rows in the result set of the subquery Ensures that the search in the inner query does notcontinue when at least one match is found Should be used with correlated subqueriesto test whether a value retrieved by the outer query exists in the results set of the values retrieved by the inner query

Scalar Subqueries Subquery that returns exactly one column value from one row Allows you to embed SQL statements which return a scalar value within SQL statements Can be used in: The condition and expression part of DECODE/CASE All clauses of SELECTexcept GROUPBY The SETclause and WHEREclause of an UPDATE statement

Scalar Subqueries Caching Most important (and surprising) advantage of using scalar subqueries Oracle caches the results of a scalar subqueryand reuses the value, even though the query should be called more than once Scalar_Subquery.sql

SQL Query Result Cache In the past, the database always cached blocks of data, the building blocks used to build result sets Starting with Oracle Database 11g, the database can now also cache result sets! If you have a query that is executed over and over again against slowly or never-changing data, you will find the new server results cache to be of great interest This is a feature from which virtually every application can and will benefit

SQL Query Result Cache: Overview A dedicated memory buffer stored in the shared pool is used for storing and retrieving the cached results The query results become invalid when data in the objects being accessed by the query is modified Multiple users can see these results without repeating the same query Good candidate statements: Access many rows Return few rows SQL Query Result Cache 2 3 SELECT SELECT Session 1 Session 2 1 SQL_Query_Result_Cache.sql

Indexes and Nulls SELECT * FROM employees WHERE department_id = 90; --------------------------------------------------------- Id Operation Name --------------------------------------------------------- 0 SELECT STATEMENT 1 TABLE ACCESS BY INDEX ROWID EMPLOYEES * 2 INDEX RANGE SCAN EMP_DEPARTMENT_IX --------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("department_id"=90)

Indexes and Nulls SELECT * FROM employees WHERE department_id IS NULL; ---------------------------------------- Id Operation Name ---------------------------------------- 0 SELECT STATEMENT * 1 TABLE ACCESS FULL EMPLOYEES ---------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("department_id" IS NULL)

Indexes and Nulls CREATE INDEX dept_ix_with_nulls ON employees(department_id,0); SELECT * FROM employees WHERE department_id IS NULL; ---------------------------------------------------------- Id Operation Name ---------------------------------------------------------- 0 SELECT STATEMENT 1 TABLE ACCESS BY INDEX ROWID EMPLOYEES * 2 INDEX RANGE SCAN DEPT_IX_WITH_NULLS ---------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("department_id" IS NULL)

Nulls and Cardinality and Indexes There is a pervasive myth that indexes and NULLs are like matter and anti-matter There is the though that WHERE COLUMN IS NULL cannot use an index There is a thought that NULLs cannot be indexed So lets use invalid values instead of the NULLs Nulls_and_Cardinality.sql

Oracle11g Invisible Index: Overview Invisible indexes are ignored by the optimizer unless you explicitly set OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE(default is FALSE) Alternative to making it unusable or dropping it Using invisible indexes you can: Test the removal of an index before dropping it Use temporary index structures for certain operations without affecting the overall application Invisible index are maintained during DML statements

Invisible Indexes: Examples Index is altered as not visible to the optimizer: ALTER INDEX ind1 INVISIBLE; Optimizer does not consider this index: SELECT /*+ index(tab1 IND1) */ COL1 FROM TAB1 WHERE ; Optimizer will always consider the index: ALTER INDEX ind1 VISIBLE; Creating an index as invisible initially: CREATE INDEX IND1 ON TAB1(COL1) INVISIBLE;

You Are Being Watched We watch what you ask for and change how statistics are gathered based on that SELECT histogram FROM user_tab_cols WHERE table_name = 'T' AND column_name = 'STATUS'; SELECT histogram FROM user_tab_cols WHERE table_name = 'T' AND column_name = 'STATUS'; HISTOGRAM --------------- NONE HISTOGRAM --------------- FREQUENCY You_Are_Being_Watched.sql

Oracle Database 12c Adaptive Query Optimization Better SQL execution with intervention Optimizer is able to learn from its mistakes Set of capabilities that enable the optimizer to make run-time adjustments to execution plans and discover additional information that can lead to better statistics Two distinct aspects: Adaptive plans Adaptive statistics

Oracle Database 12c Adaptive Plans Enables the optimizer to defer the final plan decision for a statement until execution time At runtime, optimizer can detect if its cardinality estimates differ greatly from the actual number of rows seen by the operations in the plan If there is a significant difference then the plan or a portion of it can be automatically adapted to avoid suboptimal performance on the first execution of a SQL statement

Oracle Database 12c Enhanced Statistics Enhanced dynamic sampling Hybrid histograms Statistics during loads Session private statistics for GTT s

Thank You! Ami Aharonovich Oracle ACE & OCP Ami@DBAces.co.il