SlideShare a Scribd company logo
1 of 25
liron@gotodba.com
@amitzil
https://gotodba.com
Liron Amitzi
 Liron Amitzi
 Oracle DBA since 1998 (and Oracle 7)
 Database consultant since 2002
 Oracle ACE
 An independent senior DB consultant
 Recently moved to Vancouver, BC
 BCOUG president
3 Membership Tiers
• Oracle ACE Director
• Oracle ACE
• Oracle ACE Associate
bit.ly/OracleACEProgram
500+ Technical
Experts Helping
Peers Globally
Connect:
Nominate yourself or someone you know: acenomination.oracle.com
@oracleace
Facebook.com/oracleaces
oracle-ace_ww@oracle.com
This session is based on a true story
table and column names have been
changed to protect the innocent
 This happened more than 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
 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?
 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)
ALERT_HIST
Endpoint1
Endpoint2
Endpoint3
Endpoint4
Endpoint5
Date Endpoint …
10-OCT-18 2:00:00 1
10-OCT-18 2:00:00 1
10-OCT-18 2:00:00 2
10-OCT-18 2:00:02 2
10-OCT-18 2:00:02 2
SEQ
1
2
3
4
5
 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...
 Dashboard shows
300 rows
 Users could add
predicates
 Common predicate on date
 Results ordered by SEQ
App ALERT_CURR
ALERT_HIST
Query
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
App ALERT_CURR
ALERT_HIST
Query
300 rows?
Query
More rows
select * from
(select *
from <tab>
where <filter>
order by seq desc
)
where rownum<=300;
 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?
Range index scan on the date index:
1. Filter rows by the index
2. Fetch the rows by index rowid
3. Sort the result set
4. Return first 300
Full index scan on the primary key:
1. Scan the entire index in descending order
2. Check if the date is in the range
3. Get the first 300 rows that match
4. Fetch the rows by index rowid
 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
select * from
(select *
from <tab>
where <filter>
order by date desc, seq desc
)
where rownum<=300;
 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
 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!
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;
 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?
 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?
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 rownum<=300)
)
where <filter> order by date*, seq*)
where rownum<=300;
* - desc order
 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
 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?)
Liron Amitzi
liron@gotodba.com
@amitzil
https://gotodba.com

More Related Content

What's hot

Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysisRiteshkiit
 
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functionsSql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functionsSeyed Ibrahim
 
MySQL Indexing
MySQL IndexingMySQL Indexing
MySQL IndexingBADR
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql MengChun Lam
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performancejkeriaki
 
Intro To TSQL - Unit 5
Intro To TSQL - Unit 5Intro To TSQL - Unit 5
Intro To TSQL - Unit 5iccma
 
Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningOSSCube
 
B+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBB+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBOvais Tariq
 
What is Link list? explained with animations
What is Link list? explained with animationsWhat is Link list? explained with animations
What is Link list? explained with animationsPratikNaik41
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysisRiteshkiit
 
esProc introduction
esProc introductionesProc introduction
esProc introductionssuser9671cc
 
The No SQL Principles and Basic Application Of Casandra Model
The No SQL Principles and Basic Application Of Casandra ModelThe No SQL Principles and Basic Application Of Casandra Model
The No SQL Principles and Basic Application Of Casandra ModelRishikese MR
 

What's hot (20)

PO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - SqlPO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - Sql
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysis
 
Sql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functionsSql server 2016: System Databases, data types, DML, json, and built-in functions
Sql server 2016: System Databases, data types, DML, json, and built-in functions
 
Introduction to (sql)
Introduction to (sql)Introduction to (sql)
Introduction to (sql)
 
MySQL Indexing
MySQL IndexingMySQL Indexing
MySQL Indexing
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
Mysql Indexing
Mysql IndexingMysql Indexing
Mysql Indexing
 
Intro To TSQL - Unit 5
Intro To TSQL - Unit 5Intro To TSQL - Unit 5
Intro To TSQL - Unit 5
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Dbms
DbmsDbms
Dbms
 
Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuning
 
ADBMS Unit-II b
ADBMS Unit-II bADBMS Unit-II b
ADBMS Unit-II b
 
B+Tree Indexes and InnoDB
B+Tree Indexes and InnoDBB+Tree Indexes and InnoDB
B+Tree Indexes and InnoDB
 
What is Link list? explained with animations
What is Link list? explained with animationsWhat is Link list? explained with animations
What is Link list? explained with animations
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysis
 
esProc introduction
esProc introductionesProc introduction
esProc introduction
 
ADBMS DATATYPES
ADBMS DATATYPESADBMS DATATYPES
ADBMS DATATYPES
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
The No SQL Principles and Basic Application Of Casandra Model
The No SQL Principles and Basic Application Of Casandra ModelThe No SQL Principles and Basic Application Of Casandra Model
The No SQL Principles and Basic Application Of Casandra Model
 

Similar to Real Life SQL Tuning - From 4 minutes to 8 seconds

How did i steal your database CSCamp2011
How did i steal your database CSCamp2011How did i steal your database CSCamp2011
How did i steal your database CSCamp2011Mostafa Siraj
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearchRyosuke Nakamura
 
Data-and-Compute-Intensive processing Use Case: Lucene Domain Index
Data-and-Compute-Intensive processing Use Case: Lucene Domain IndexData-and-Compute-Intensive processing Use Case: Lucene Domain Index
Data-and-Compute-Intensive processing Use Case: Lucene Domain IndexMarcelo Ochoa
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 
Migrate 10TB to Exadata Tips and Tricks (Presentation)
Migrate 10TB to Exadata Tips and Tricks (Presentation)Migrate 10TB to Exadata Tips and Tricks (Presentation)
Migrate 10TB to Exadata Tips and Tricks (Presentation)Amin Adatia
 
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre FeaturesLinuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre FeaturesDave Stokes
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxPythian
 
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...united global soft
 
Orcale dba Online training India
Orcale dba Online training IndiaOrcale dba Online training India
Orcale dba Online training Indiaunited global soft
 
Orcale DBA Online Training in India
Orcale DBA Online Training in IndiaOrcale DBA Online Training in India
Orcale DBA Online Training in Indiaunited global soft
 
Oracle DBA Online Training in India
Oracle DBA Online Training in IndiaOracle DBA Online Training in India
Oracle DBA Online Training in Indiaunited global soft
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluationavniS
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsTarik Essawi
 

Similar to Real Life SQL Tuning - From 4 minutes to 8 seconds (20)

How did i steal your database CSCamp2011
How did i steal your database CSCamp2011How did i steal your database CSCamp2011
How did i steal your database CSCamp2011
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
Data-and-Compute-Intensive processing Use Case: Lucene Domain Index
Data-and-Compute-Intensive processing Use Case: Lucene Domain IndexData-and-Compute-Intensive processing Use Case: Lucene Domain Index
Data-and-Compute-Intensive processing Use Case: Lucene Domain Index
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Migrate 10TB to Exadata Tips and Tricks (Presentation)
Migrate 10TB to Exadata Tips and Tricks (Presentation)Migrate 10TB to Exadata Tips and Tricks (Presentation)
Migrate 10TB to Exadata Tips and Tricks (Presentation)
 
Databases with SQLite3.pdf
Databases with SQLite3.pdfDatabases with SQLite3.pdf
Databases with SQLite3.pdf
 
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre FeaturesLinuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
Conf orm - explain
Conf orm - explainConf orm - explain
Conf orm - explain
 
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...
Oracle DBA Online Training in India,Oracle DBA Training in USA,Oracle DBA Tra...
 
Orcale dba Online training India
Orcale dba Online training IndiaOrcale dba Online training India
Orcale dba Online training India
 
Orcale DBA Online Training in India
Orcale DBA Online Training in IndiaOrcale DBA Online Training in India
Orcale DBA Online Training in India
 
Orcale dba training
Orcale dba trainingOrcale dba training
Orcale dba training
 
Orcale dba training
Orcale dba trainingOrcale dba training
Orcale dba training
 
Oracle DBA Online Training in India
Oracle DBA Online Training in IndiaOracle DBA Online Training in India
Oracle DBA Online Training in India
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
 
IR SQLite Session #1
IR SQLite Session #1IR SQLite Session #1
IR SQLite Session #1
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archs
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 

Recently uploaded

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Real Life SQL Tuning - From 4 minutes to 8 seconds

  • 2.  Liron Amitzi  Oracle DBA since 1998 (and Oracle 7)  Database consultant since 2002  Oracle ACE  An independent senior DB consultant  Recently moved to Vancouver, BC  BCOUG president
  • 3. 3 Membership Tiers • Oracle ACE Director • Oracle ACE • Oracle ACE Associate bit.ly/OracleACEProgram 500+ Technical Experts Helping Peers Globally Connect: Nominate yourself or someone you know: acenomination.oracle.com @oracleace Facebook.com/oracleaces oracle-ace_ww@oracle.com
  • 4. This session is based on a true story table and column names have been changed to protect the innocent
  • 5.  This happened more than 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
  • 6.  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?
  • 7.  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) ALERT_HIST Endpoint1 Endpoint2 Endpoint3 Endpoint4 Endpoint5
  • 8. Date Endpoint … 10-OCT-18 2:00:00 1 10-OCT-18 2:00:00 1 10-OCT-18 2:00:00 2 10-OCT-18 2:00:02 2 10-OCT-18 2:00:02 2 SEQ 1 2 3 4 5  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...
  • 9.  Dashboard shows 300 rows  Users could add predicates  Common predicate on date  Results ordered by SEQ App ALERT_CURR ALERT_HIST Query 300 rows? Query More rows
  • 10.  When the app could not find 300 rows in ALERT_CURR it queried ALERT_HIST  In many cases it just took too long App ALERT_CURR ALERT_HIST Query 300 rows? Query More rows
  • 11. select * from (select * from <tab> where <filter> order by seq desc ) where rownum<=300;
  • 12.  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?
  • 13. Range index scan on the date index: 1. Filter rows by the index 2. Fetch the rows by index rowid 3. Sort the result set 4. Return first 300
  • 14. Full index scan on the primary key: 1. Scan the entire index in descending order 2. Check if the date is in the range 3. Get the first 300 rows that match 4. Fetch the rows by index rowid
  • 15.  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
  • 16. select * from (select * from <tab> where <filter> order by date desc, seq desc ) where rownum<=300;
  • 17.  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
  • 18.  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!
  • 19. 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;
  • 20.  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?
  • 21.  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?
  • 22. 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 rownum<=300) ) where <filter> order by date*, seq*) where rownum<=300; * - desc order
  • 23.  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
  • 24.  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?)