Optimizing SQL Queries

Slides:



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

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,
Introduction to SQL Tuning Brown Bag Three essential concepts.
Exadata Distinctives Brown Bag New features for tuning Oracle database applications.
SQL Performance 2011/12 Joe Chang, SolidQ
The query processor does what the query plan tells it to do A “good” query plan is essential for a well- performing.
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.
Gain Performance & Scalability With RightNow Analytics
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.
Database Management 9. course. Execution of queries.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
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.
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.
1 Oracle Enterprise Manager Slides from Dominic Gélinas CIS
Module 4 Database SQL Tuning Section 3 Application Performance.
BlinkDB: Queries with Bounded Errors and Bounded Response Times on Very Large Data ACM EuroSys 2013 (Best Paper Award)
Partition Architecture Yeon JongHeum
CERN IT Department CH-1211 Genève 23 Switzerland t DM Database Monitoring Tools Database Developers' Workshop CERN, July 8 th, 2008 Dawid.
Distributed Time Series Database
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
DB Tuning : Chapter 10. Optimizer Center for E-Business Technology Seoul National University Seoul, Korea 이상근 Intelligent Database Systems Lab School of.
Sorting and Joining.
Query Execution Query compiler Execution engine Index/record mgr. Buffer manager Storage manager storage User/ Application Query update Query execution.
Eugene Meidinger Execution Plans
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
DB Index Expert Copyright © SoftTree Technologies, Inc.
LAB: Web-scale Data Management on a Cloud Lab 11. Query Execution Plan 2011/05/27.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
SQL Server Statistics and its relationship with Query Optimizer
Query Optimization Techniques
Operation Data Analysis Hints and Guidelines
Indexes By Adrienne Watt.
Chapter 6 - Database Implementation and Use
Query Tuning without Production Data
Scaling SQL with different approaches
Query Tuning without Production Data
Choosing Access Path The basic methods.
Ripple Joins for Online Aggregation
Bolin Ding Silu Huang* Surajit Chaudhuri Kaushik Chakrabarti Chi Wang
Physical Database Design for Relational Databases Step 3 – Step 8
Chapter 15 QUERY EXECUTION.
From 4 Minutes to 8 Seconds in an Hour (or a bit less)
SQL 101.
Examples of Physical Query Plan Alternatives
Data Lifecycle Review and Outlook
Query Optimization Techniques
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Steve Hood SimpleSQLServer.com
Chapter 4 Indexes.
CH 4 Indexes.
CH 4 Indexes.
Contents Preface I Introduction Lesson Objectives I-2
Implementation of Relational Operations
Diving into Query Execution Plans
Introduction to the Optimizer
Query Optimization Techniques
SQL Performance for DBAs
Performance Tuning ETL Process
Query Transformations
Presentation transcript:

Optimizing SQL Queries 26 January 2009 Balys Šulmanas, Vilnius University

Applications CMS Dashboard (CMS offline DB) CMS PVSS (CMS online DB) Other applications on CMS online DB DM technical meeting - 2

Typical workflow Look for the highest I/O (sometimes CPU) query on the DB Analyze execution plan Make improvements: Create or modify the indexes (most often) Redesign the query (or the views the query selects) Sometimes add some optimizer hints DM technical meeting - 3

Tools used Oracle Enterprise Manager for DB performance monitoring Oracle Enterprise Manager and Benthic for optimization DM technical meeting - 4

Query execution plan An execution plan defines how Oracle finds or writes the data It consists of steps Oracle optimizer estimates the metric called cost for each step of the execution plan and for the whole query Typically the lowest cost execution should be the fastest You can reduce the cost with proper indexing and proper queries DM technical meeting - 5

Execution plan graph (Oracle Enterprise Manager) Access predicate: "DPE"."SYS_ID"= "ELEMENTS"."SYS_ID" AND "DPE"."DPE_ID"= "ELEMENTS"."DPE_ID" AND "DPE"."DP_ID"= "ELEMENTS"."DP_ID" DM technical meeting - 6

Execution plan table (Oracle Enterprise manager) DM technical meeting - 7

Main execution plan steps Data can be retrieved from tables and indexes TABLE ACCESS BY INDEX vs. TABLE ACCESS FULL NESTED LOOPS vs. HASH JOIN Access predicates DM technical meeting - 8

TABLE ACCESS BY INDEX vs. TABLE ACCESS FULL To find a row by index is much faster then to scan all the rows to find the one you need Full table scan means that either the columns that you filter are not indexed or too many rows Sometimes Oracle optimizer overestimates the number of rows DM technical meeting - 9

NESTED LOOPS vs. HASH JOIN 2 most often used methods to join tables NESTED loops should be faster if proper indexes are set and there are not too many rows Sometimes Oracle estimates this wrong DM technical meeting - 10

Typical index related problems The columns used to filter data are not indexed The columns used to filter data are indexed, but in a wrong order Functions are being called on indexed columns DM technical meeting - 11

Recent no index example (CMS_RPC_PVSS_COND) select count(*) from elements d where d.sys_id = :1 and d.dp_id = :2 There is a foreign key set on ELEMENTS(DP_ID, SYS_ID), so the corresponding index should be created anyway, but there was no index The cost of execution plan was 266 It got 1 after the index was created DM technical meeting - 12

Columns indexed in wrong order (CMS PVSS case) 15 schemas and few hundreds of tables All those tables have DPID (which is a number foreign key) and CHANGE_DATE (Timestamp) columns The index was created on [CHANGE_DATE, DPID] (this means foreign key was not indexed) The most often used query provides a particular DPID and the range of CHANGE_DATE I dropped [CHANGE_DATE, DPID] indexes and created [DPID, CHANGE_DATE] ones As a result, the users started selecting week-long ranges instead of 3 hours-long DM technical meeting - 13

Functions on indexed columns Typically functions are called to round Timestamp (to days, hours, seconds etc.) If the column has an index, Oracle will not be able to use it To truncate a Timestamp means the same as to take an interval TS >= :1 and TS < :1 + INTERVAL '1' DAY is better than to_timestamp(TO_CHAR(TS, 'DD-MON-YY'))=:1 if TS column has an index DM technical meeting - 14

Typical SQL related problems Not using features, which would allow to increase performance, like Oracle analytic functions Oracle analytic functions have many nice features, but the most simple (and the most often to be used) are RANK()/DENSE_RANK() and KEEP FIRST/LAST functions Bugs, which result in huge result sets and high load on DB DM technical meeting - 15

No Oracle analytic functions example (CMS Dashboard) select "ServiceName","DetailedAv” from ( select a."ServiceId", "DetailedAv" from SAM_SERVICE_AVL_H1 a, (select max("TimeStamp") as t, "ServiceId" from SAM_SERVICE_AVL_H1 group by "ServiceId“) g where g."ServiceId"=a."ServiceId" and g.t=a."TimeStamp“ ) b, SAM_SERVICES s , SITE where b."ServiceId"=s."ServiceId" and "IgnoreFlag"=0 and "SiteId"="ServiceSiteId" and ( "VOName" = :site0 ) DM technical meeting - 16

Oracle analytic functions example (CMS Dashboard) select MIN("ServiceName") KEEP (DENSE_RANK LAST ORDER BY "TimeStamp") , MIN("DetailedAv") KEEP (DENSE_RANK LAST ORDER BY "TimeStamp") from SAM_SERVICE_AVL_H1 H1, SAM_SERVICES s, SITE where H1."ServiceId"=s."ServiceId" and "IgnoreFlag"=0 and "SiteId"="ServiceSiteId" and VOName" = :site0 group by H1."ServiceId" DM technical meeting - 17

Oracle hints Oracle hints are directives to the optimizer i.e. you tell Oracle SQL engine what to do Oracle was choosing FULL SCANS and HASH JOINS instead of ACCESS BY INDEX in NESTED LOOPS Sometimes Oracle overestimates the number of rows in the execution plan and then switches to FULL SCANS and HASH JOINS So I just used to add /*+USE_NL*/ hints DM technical meeting - 18

The results for CMS Dashboard Before: The scale is 0-4400. SYS$USERS are Oracle jobs from CMS Dashboard 2000 The scale is 0-1500 After: 500 DM technical meeting - 19

The results “It got X times faster” makes no sense, because it depends on amount of data A simple query with an index should be kind of logarithmic complexity and without it should be linear DM technical meeting - 20

Some notes "You should almost always index foreign keys“ (Oracle Database Concepts manual) ~70% of the issues were because of ignoring this simple recommendation DM technical meeting- 21

Conclusions Applications can profit from SQL tuning Oracle optimizer is good for most cases Particular queries still require manual work/Oracle expertise CMS Dashboard improved 4 times at that moment DM technical meeting- 22