From 4 Minutes to 8 Seconds in an Hour (or a bit less)

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Recent-Rows-First Case Study February, 2010 ©2010 Dan Tow, All rights reserved SingingSQL Presents SingingSQL.
1 Access Lesson 3 Creating Queries Microsoft Office 2010 Introductory.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Sofia, Bulgaria | 9-10 October Concurrency Management – ADO.NET 2.0 Presented By: Sahil Malik Presented By: Sahil Malik
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Chapter 6 Database Administration
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
Module 4 Database SQL Tuning Section 3 Application Performance.
LANDESK SOFTWARE CONFIDENTIAL Tips and Tricks with Filters Jenny Lardh.
Partition Architecture Yeon JongHeum
23 Copyright © 2009, Oracle. All rights reserved. Oracle Business Intelligence Answers: Advanced Features.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Sorting and Joining.
Query Processing – Implementing Set Operations and Joins Chap. 19.
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
Same Plan Different Performance Mauro Pagano. Consultant/Developer/Analyst Oracle  Enkitec  Accenture DBPerf and SQL Tuning Training Tools (SQLT, SQLd360,
AOIT Database Design Unit 3, Lesson 9 Data Integrity Copyright © 2009–2011 National Academy Foundation. All rights reserved.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Interview Techniques When you're interviewing someone, even your mother - you have to sort of deal with you have to get some objective space from yourself.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
SQL Server Statistics and its relationship with Query Optimizer
Chris Index Feng Shui Chris
Data Virtualization Demoette… Packaged Query Single Select Option
AP CSP: Cleaning Data & Creating Summary Tables
Steve Coleman UTOUG Fall Symposium October 26, 2016
Presented by: Teererai Marange
CS 540 Database Management Systems
Creating & Editing Reports
Indices.
Data Virtualization Tutorial… Semijoin Optimization
Optimizing SQL Queries
Database Performance Tuning and Query Optimization
Better Living through Extensions
Cumulative Index to Nursing and Allied Health Literature
SQL Server May Let You Do It, But it Doesn’t Mean You Should
Purpose, Pitfalls and Performance Implications
Use Cases & User Mocks Customer Call –
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
LO Adding and subtracting with negative numbers RAG
Data Lifecycle Review and Outlook
Chapter 8 Working with Databases and MySQL
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Purpose, Pitfalls and Performance Implications
Steve Hood SimpleSQLServer.com
Workforce Mobile (Android)
Topic 1: Problem Solving
DONE Need password feature
Welcome! and download the training materials (Added a case study file that each of you will need) Let me know if you have any.
Indexing For Optimal Performance
End-user measurement combined with deep technical visibility
Introduction When searching for a new mattress, you have to make sure you know where to go to find the best one. The mattress you sleep on is going to.
Database Internals: How Indexes Work
What we’ll be doing Back to Basics Read Consistency Write Consistency.
Four Rules For Columnstore Query Performance
M1G Introduction to Database Development
Towards a better method of searching source code
Same as… Numeracy focus: Problem solving focus:
What are the popular tips to Build a Scalable Technology Stack.
Chapter 11 Database Performance Tuning and Query Optimization
10 Most Important WordPress Plugins You Must Have Website Promoters L.L.C.
Reading to Synthesize Information
Improvement Audits - Let’s Get More Bang!
Hope – your experience A. Think privately of a recent occasion when you felt hopeful of something. B. Continue thinking about: whether you realised at.
SQL Performance for DBAs
Vendor Software Lessons From Consulting Vendor Software.
The Ins and Outs of Indexes
An Introduction to Partitioning
Presentation transcript:

From 4 Minutes to 8 Seconds in an Hour (or a bit less) Liron Amitzi @amitzil https://amitzil.wordpress.com

About Me Liron Amitzi Oracle DBA since 1998 (and Oracle 7) Oracle ACE Database consultant since 2002 Recently moved to Vancouver, BC BCOUG president

Nominate yourself or someone you know: acenomination.oracle.com 500+ Technical Experts Helping Peers Globally 3 Membership Tiers Oracle ACE Director Oracle ACE Oracle ACE Associate Connect: bit.ly/OracleACEProgram oracle-ace_ww@oracle.com Facebook.com/oracleaces @oracleace Nominate yourself or someone you know: acenomination.oracle.com

This session is based on a true story table and column names have been changed to protect the innocent

What are we Talking About? This happened about 10 years ago, but is still relevant A dashboard query took about 4 minutes and timed out Spoiler alert - at the end of the process the query took 8 seconds

What does SQL Tuning Include? Understanding the application design - but why? Understanding the query logic - but why? Understanding the query code - OK I get that one Understanding what Oracle does - sounds reasonable Trying to help Oracle do something better - how?

ALERT_CURR (100 alerts per endpoint) The Design Endpoint2 Endpoint1 Endpoint3 Monitoring system Endpoints are sending many alerts After 100 alerts per endpoint old alerts are moved to ALERT_HIST ALERT_CURR (100 alerts per endpoint) Endpoint4 Endpoint5 ALERT_HIST

More About the Design Alerts were coming quickly, so they added a sequence to ensure order PK was SEQ, date and endpoint_id We couldn't change the base design (history structure) but were allowed to change anything else Partitions could be great here, but this was SE... SEQ 1 2 3 4 5 Date Endpoint … 10-OCT-18 2:00:00 1 2 10-OCT-18 2:00:02

The Query Logic Dashboard shows 300 rows Users could add predicates Common predicate on date Results ordered by SEQ App Query ALERT_CURR 300 rows? Query More rows ALERT_HIST

The Problem App Query ALERT_CURR 300 rows? Query More rows When the app could not find 300 rows in ALERT_CURR it queried ALERT_HIST In many cases it just took too long ALERT_HIST

The Query Code (Original) select * from (select * from <tab> where <filter> order by seq desc ) where rownum<=300;

The SEQ Column Was created only to make sure the order is preserved Today I would use timestamp Query often had a predicate on the date and order by SEQ We had PK (SEQ, date, endpoint_id) and a regular index (date) When we use predicate on date, what will Oracle do?

SEQ Column - Index Usage #1 Range index scan on the date index: Filter rows by the index Fetch the rows by index rowid Sort the result set Return first 300

SEQ Column - Index Usage #2 Full index scan on the primary key: Scan the entire index in descending order Check if the date is in the range Get the first 300 rows that match Fetch the rows by index rowid

Changing the PK Oracle decided to use the PK to scan the SEQ column ordered Since SEQ value is not important, we changed the PK: The old PK was SEQ, date, endpoint_id The new PK was date, SEQ, endpoint_id We also added the date to the order by That way Oracle used the index for both predicate and sort

The Query Code (New PK) select * from (select * from <tab> where <filter> order by date desc, seq desc ) where rownum<=300;

A Bug in the Logic! Wait a second! The design is based on numbers per endpoint, while the dashboard queries the latest There is a bug if one endpoint send many alerts while the others don't Dashboard might show wrong data as new data is already in ALERT_HIST

Fixing the Bug ALERT_HIST can contain alerts that are newer than some alerts in ALERT_CURR We had to query ALERT_HIST every time, which made the problem even worse!

The Query Code (Step 1) select * from (select * from (select * from alert_curr union all select * from alert_hist ) where <filter> order by date desc, seq desc ) where rownum<=300;

Amount of Rows After fixing all of this, the query was still slow... The indexes were not being used optimally The union and order by resulted in a lot of work on Oracle's side Returning 300 rows after sort requires a full sort operation Any ideas?

Amount of Rows - Solution We realized that we need 300 rows in the end That's 300 from the first table, or 300 from the second, or any combination of the two Let's limit each table to 300 rows efficiently and then take the top 300 Makes sense?

The Query Code (Step 2) select * from (select * from ((select * from (select * from alert_curr where <filter> order by date*,seq*) where rownum<=300)      union all (select * from       (select * from alert_hist where <filter> order by date*,seq*)     )     where <filter> order by date*, seq*) where rownum<=300; * - desc order

Results The query that took 4 minutes at the beginning now took about 8 seconds Index range scan was very efficient (used for both date predicate and order by) There is a single order by operation of only 600 rows

Summary A successful project and a very satisfied customer We do need to understand the logic We do need cooperation from the developers, we are not magicians Without understanding the system we could not: Find and fix the bug in the logic Change the PK (what if there was a reason for SEQ to be first?)

Q&A Liron Amitzi @amitzil https://amitzil.wordpress.com