Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing

Slides:



Advertisements
Similar presentations
Youre Smarter than a Database Overcoming the optimizers bad cardinality estimates.
Advertisements

The Basics of Efficient SQL Written for myself –Writing doesnt make you rich Proof of what works –and what doesnt Three parts: –Data Model Tuning –SQL.
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc
Introduction to SQL Tuning Brown Bag Three essential concepts.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
Semantec Ltd. Oracle Performance Tuning Boyan Pavlov Indexes Indexes.
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.
8-1 Outline  Overview of Physical Database Design  File Structures  Query Optimization  Index Selection  Additional Choices in Physical Database Design.
Optimization Exercises. Question 1 How do you think the following query should be computed? What indexes would you suggest to use? SELECT E.ename, D.mgr.
Chapter 8 Physical Database Design. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Overview of Physical Database.
Access Path Selection in a Relation Database Management System (summarized in section 2)
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 Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Oracle Data Block Oracle Concepts Manual. Oracle Rows Oracle Concepts Manual.
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.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
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 Optimizing Your ColdFusion Applications for Oracle Justin Fidler, CNA, CPS, CCFD Chief Technology Officer Bantu, Inc. 8 May 2001.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
Star Transformations Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
SQL Performance and Optimization l SQL Overview l Performance Tuning Process l SQL-Tuning –EXPLAIN PLANs –Tuning Tools –Optimizing Table Scans –Optimizing.
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.
Database Management COP4540, SCS, FIU Physical Database Design (2) (ch. 16 & ch. 6)
Module 4 Database SQL Tuning Section 3 Application Performance.
1 Chapter 13 Parallel SQL. 2 Understanding Parallel SQL Enables a SQL statement to be: – Split into multiple threads – Each thread processed simultaneously.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 8 Physical Database Design. Outline Overview of Physical Database Design Inputs of Physical Database Design File Structures Query Optimization.
Chapter 5 Index and Clustering
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Sorting and Joining.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
How is data stored? ● Table and index Data are stored in blocks(aka Page). ● All IO is done at least one block at a time. ● Typical block size is 8Kb.
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
Practical Database Design and Tuning
Tuning Transact-SQL Queries
SQL Trace and TKPROF.
Indexes By Adrienne Watt.
SQL Tuning.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Physical Database Design
Database Performance Tuning &
Choosing Access Path The basic methods.
Teradata Join Processing
Database Performance Tuning and Query Optimization
CHAPTER 5: PHYSICAL DATABASE DESIGN AND PERFORMANCE
Cse 344 April 25th – Disk i/o.
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Physical Database Design
Execution Plans Demystified
Steve Hood SimpleSQLServer.com
Practical Database Design and Tuning
Understanding Indexes
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
EXECUTION PLANS Quick Dive.
Introduction to the Optimizer
SQL Performance for DBAs
Presentation transcript:

Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing The Optimizer Making SQL Efficient Finding Problem Queries Oracle Enterprise Manager Wait Event Interface

The Basics of Efficient SQL SELECT * FROM division; SELECT division_id, name, city, state, country FROM division; SELECT division_id FROM division; SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY

The Basics of Efficient SQL SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY Avoid unintentional full table scans SELECT * FROM division WHERE country LIKE '%a%'; Match indexes Exact hits (equality) SELECT * FROM division WHERE division_id = 1; Range scans / skip scans / full index scans EXISTS (correlate) faster than IN Biggest filters first Full table scans can sometimes be faster

The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes The Basics of Efficient SQL SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY

The Basics of Efficient SQL Resorts on result after WHERE and GROUP BY Don’t repeat sorting (ORDER BY often ignored) by SELECT SELECT division_id FROM division ORDER BY division_id; by WHERE SELECT * FROM division WHERE division_id < 10 ORDER BY division_id; GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; by DISTINCT SELECT DISTINCT(state) FROM division ORDER BY state; by indexes The Basics of Efficient SQL SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY

The Basics of Efficient SQL SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY Use WHERE not HAVING GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';

The Basics of Efficient SQL SELECT FOR UPDATE Filtering WHERE ORDER BY often ignored by the Optimizer depends on query and index complexity may need ORDER BY using composite indexes GROUP BY Use WHERE not HAVING GROUP BY SELECT state, COUNT(state) FROM division GROUP BY state ORDER BY state; HAVING (filters aggregate) SELECT state, COUNT(state) FROM division GROUP BY state HAVING COUNT(state) > 1; use WHERE SELECT state, COUNT(state) FROM division WHERE state = 'NY' GROUP BY state; not HAVING SELECT state, COUNT(state) FROM division GROUP BY state HAVING state = 'NY';

The Basics of Efficient SQL Functions conversions miss indexes counteract with function based indexing avoid using DECODE CASE expressions set operators (UNION) Use sequences Use equality (=) or range scans (>) avoid negatives (!=, NOT) avoid LIKE

The Basics of Efficient SQL Joins avoid Cartesian Products avoid anti joins avoid outer joins perhaps replace multiple table complex joins with subquery semi joins and inline views Be careful with views

Common Sense Indexing Don’t always need indexes How to index table with few columns static data small tables appended tables (SQL*Loader) How to index single column surrogate sequences don’t override PK and FKs avoid nullable columns

Common Sense Indexing Read write indexing Often read only BTree Bitmaps IOTs Clusters

Common Sense Indexing Read write indexing BTree function based can help a lot get out of control everybody wants one reverse key surrogate keys High insertion rates not DW Oracle RAC

Common Sense Indexing Often read only Bitmaps can be much faster than BTrees can deteriorate drastically over time twice as fast in my book at a previous client 1 year of DML activity 100s of times slower problem was nobody knew why and nobody wanted to change anything

Common Sense Indexing Often read only IOTs small number of columns small tables heard good things in Oracle RAC even highly active DML environments

The Optimizer Is intelligent Is usually correct better with simple queries Is usually correct Nothing is set in stone Verify SQL code efficiency use EXPLAIN PLAN SET AUTOTRACE ON EXPLAIN $ORACLE_HOME/rdbms/admin/utlxplan.sql

The Optimizer Everything cost based Maintain statistics rule based is redundant Maintain statistics Dynamic sampling OPTIMIZER_DYNAMIC_SAMPLING Set TIMED_STATISTICS Histograms maintain as for statistics use for unevenly distributed indexes

The Optimizer Tables full Table scans sample table scans ROWID scans small static tables reading most of the rows over 10% for the Optimizer reading deleted rows parallel table scans sample table scans retrieve only portion of table by blocks or % SELECT * FROM generalledger SAMPLE(0.001); ROWID scans

The Optimizer Indexes index unique scan index range scan reverse order index range scan index skip scan index full scan fast full index scan others (very specific)

The Optimizer Joins nested loop join hash join most efficient row sets both small one large and one small row set one sorted hash join both large with little difference temporary hash table generated

The Optimizer More on joins sort merge join other join types inefficient both rows sets sorted then merge sorted other join types semi joins bitmap joins star queries Cartesian joins outer joins (nested, hash or sort merge)

The Optimizer Hints can change things influence the optimizer CURSOR_SHARING configuration parameter FORCE (OLTP) EXACT (DW) FIRST or ALL_ROWS DYNAMIC_SAMPLING change table scans FULL

The Optimizer More on hints change index scans change joins INDEX_ASC, INDEX_DESC INDEX_FFS INDEX_JOIN (join indexes) INDEX_SS_ASC or INDEX_SS_DESC NO_INDEX, NO_INDEX_FFS or NO_INDEX_SS change joins can change join type and influence with parameters parallel SQL

Finding Problem Queries EXPLAIN PLAN SET AUTOTRACE ON EXPLAIN $ORACLE_HOME/rdbms/admin/utlxplan.sql SQL Trace and TKPROF

Finding Problem Queries Performance views V$SQLAREA executions parsing sorting disk and buffer reads fetching V$SQL optimizer cost CPU time elapsed time Wait Event Interface

Wait Event Interface

Wait Event Interface

Wait Event Interface

Wait Event Interface

Wait Event Interface

Wait Event Interface

Wait Event Interface

Performance overview

Database health overview

Drilldown

More drilldown

TopSQL

EXPLAIN PLAN

Wait Event Interface

Wait Event Interface

Wait Event Interface

Use the HELP FILES in Oracle Enterprise Manager