SlideShare a Scribd company logo
1 of 20
Download to read offline
SQL Performance Tuning Hierarchy

1
Tip # 1: Write Sensible Queries
•

IDENTIFY SQL STATEMENTS THAT ARE TAKING A LONG TIME
TO EXECUTE & STATEMENTS THAT INVOLVE THE JOINING OF
A LARGE NUMBER OF BIG TABLES AND OUTER JOINS

•

CHECK TO SEE IF THERE ARE ANY INDEXES THAT MAY HELP
PERFORMANCE

•

TRY ADDING NEW INDEXES TO THE SYSTEM TO REDUCE
EXCESSIVE FULL TABLE SCANS

2
Tip # 2: Tuning Sub queries
•

THE MAIN QUERY WILL NOT PERFORM WELL IF THE SUBQUERIES CAN’T
PERFORM WELL THEMSELVES

•

PAY ATTENTION TO CORRELATED SUBQUERIES, AS THEY TEND TO BE
VERY COSTLY AND CPU- INSENTIVE.

3
Tip 3# :Limit the Number of Tables in a Join
•

BREAK THE SQL STATEMENT INTO SMALLER STATEMENTS AND WRITING A
PL/SQL BLOCK TO REDUCE DATABASE CALLS

•

USE THE DBMS_SHARED_POOL PACKAGE TO PIN A SQL OR PL/SQL
AREA

•

PINNING PREVENTS MEMORY FRAGMENTATION AND HELPS TO RESERVE
MEMORY FOR SPECIFIC PROGRAMS.

4
5

Tip 4# :Recursive Calls Should be Kept to a
Minimum
•

LARGE AMOUNT OF RECURSIVE SQL EXECUTED BY SYS COULD
INDICATE SPACE MANAGEMENT ACTIVITIES SUCH AS EXTENT
ALLOCATIONS TAKING PLACE. THIS IS NOT SCALABLE AND IMPACTS
USER RESPONSE TIME

•

THE ORACLE TRACE UTILITY TKPROF PROVIDES INFORMATION ABOUT
RECURSIVE CALLS, LIBRARY CACHE MISSES AND ALSO THE USERNAME
OF THE INDIVIDUAL WHO EXECUTED THE SQL STATEMENT

•

TKPROF-GENERATED STATISTICS CAN
TKPROF_TABLE TO BE QUERIED LATER

BE

STORED

IN

A

TABLE
Tip 5# :Improving Parse Speed
•

USE OF BIND VARIABLES ALLOWS YOU TO REPEATEDLY USE THE SAME
STATEMENTS WHILE CHANGING THE WHERE CLAUSE CRITERIA

•

THE PARSE PHASE FOR STATEMENTS CAN BE DECREASED BY EFFICIENT
USE OF ALIASING

6
Tip 6# :Alias Usage
•

IF AN ALIAS IS NOT PRESENT, THE ENGINE MUST RESOLVE WHICH TABLES
OWN THE SPECIFIED COLUMNS

•

A SHORT ALIAS IS PARSED MORE QUICKLY THAN A LONG TABLE NAME
OR ALIAS IF POSSIBLE REDUCE THE ALIAS TO A SINGLE LETTER
•

Bad Statement
•

•

SELECT first_name, last_name, country FROM employee, countries WHERE
country_id = id AND last_name = ‘HALL’;

Good Statement
•

SELECT e.first_name, e.last_name, c.country FROM employee e, countries c
WHERE e.country_id = c.id AND e.last_name = ‘HALL’;

7
Tip 7# :Driving Tables
•

THE STRUCTURE OF THE FROM AND WHERE CLAUSES OF DML
STATEMENTS CAN BE TAILORED TO IMPROVE THE PERFORMANCE

8
Tip 8# :Caching Tables
•

FOR SMALL, FREQUENTLY USED TABLES, PERFORMANCE MAY BE
IMPROVED BY CACHING TABLES

•

NORMALLY, WHEN FULL TABLE SCANS OCCUR, THE CACHED DATA IS
PLACED ON THE LEAST RECENTLY USED (LRU) END OF THE BUFFER
CACHE

•

IF THE TABLE IS CACHED (ALTER TABLE EMPLOYEES CACHE;), THE DATA
IS PLACED ON THE MOST RECENTLY USED (MRU) END OF THE BUFFER,
AND SO IT IS LESS LIKELY TO BE PAGED OUT BEFORE IT IS RE-QUERIED

•

CACHING TABLES MAY ALTER THE CBO’S PATH THROUGH THE DATA
AND SHOULD NOT BE USED WITHOUT CAREFUL CONSIDERATION

9
Tip 9# :Avoid Using Select * Clauses

10

•

DO NOT USE THE * FEATURE BECAUSE IT IS VERY INEFFICIENT

•

THE * HAS TO BE CONVERTED TO EACH COLUMN IN TURN

•

THE SQL PARSER HANDLES ALL THE FIELD REFERENCES BY OBTAINING
THE NAMES OF VALID COLUMNS FROM THE DATA DICTIONARY AND
SUBSTITUTES THEM ON THE COMMAND LINE, WHICH IS TIME
CONSUMING.
Tip 10# :Exists vs. In
•

•
•
•

THE EXISTS FUNCTION SEARCHES FOR THE PRESENCE OF A SINGLE ROW
THAT MEETS THE STATED CRITERIA, AS OPPOSED TO THE IN STATEMENT THAT
LOOKS FOR ALL OCCURRENCES
PRODUCT - 1000 ROWS
ITEMS - 1000 ROWS
(A)
•

•

SELECT p.product_id FROM products p WHERE p.item_no IN (SELECT i.item_no
FROM items i);

(B)
•

•

11

SELECT p.product_id FROM products p WHERE EXISTS (SELECT ‘1’ FROM items I
WHERE i.item_no = p.item_no)

FOR QUERY A, ALL ROWS IN ITEMS WILL BE READ FOR EVERY ROW IN
PRODUCTS. THE EFFECT WILL BE 1,000,000 ROWS READ FROM ITEMS. IN THE
CASE OF QUERY B, A MAXIMUM OF 1 ROW FROM ITEMS WILL BE READ FOR
EACH ROW OF PRODUCTS, THUS REDUCING THE PROCESSING OVERHEAD
OF THE STATEMENT.
Tip 11# :Not Exists vs. Not In
•

In subquery statements such as the following, the NOT IN clause causes
an internal sort/ merge.

•

SELECT * FROM student WHERE student_num NOT IN (SELECT student_num
FROM class)

•

Instead, use:
•

SELECT * FROM student c WHERE NOT EXISTS (SELECT 1 FROM class a WHERE
a.student_num = c.student_num)

12
Tip 12# : In with Minus vs. Not In for Non-Indexed
13
Columns
•

In subquery statements such as the following, the NOT IN clause causes
an internal sort/ merge

•

SELECT * FROM system_user WHERE su_user_id NOT IN (SELECT ac_user
FROM account)

•

Instead, use:
•

SELECT * FROM system_user WHERE su_user_id IN (SELECT su_user_id FROM
system_user MINUS SELECT ac_user FROM account)
Tip 13# :Correlated Subqueries vs. Inline Views

14

•

Do not use code correlated subqueries, as they will adversely impact
system performance

•

Instead, use inline views (subqueries in the from clause of your select
statements), which perform orders of magnitude faster and are much
more scalable
Tip 14# :Views Usage

15

•

Consider including the view definition in the main query by including its
code without the actual view name

•

Views can cause potential performance problems when they have outer
joins (CBO goes haywire with them, even in 9I) or are considered nonmergable views by Oracle.
Tip 15# :Use Decode to Reduce Processing

16

•

Use DECODE when you want to scan same rows repetitively or join the
same table repetitively

•

SELECT count(*) , sum(sal) FROM emp WHERE deptno = 10 AND ename
LIKE ‘MILLER’;

•

SELECT count(*) , sum(sal) FROM emp WHERE deptno = 20 AND ename
LIKE ‘MILLER’;
Tip 16# : Use Union in Place of Or

17

•

In general, always consider the UNION verb instead of OR verb in the
WHERE clauses.

•

Using OR on an indexed column causes the optimizer to perform a fulltable scan rather than an indexed retrieval.
Tip 17# : Use ‘Union All’ Instead of Union

18

•

The SORT operation is very expensive in terms of CPU consumption. The
UNION operation sorts the result set to eliminate any rows that are within
the sub-queries.

•

UNION ALL includes duplicate rows and does not require a sort. Unless you
require that these duplicate rows be eliminated, use UNION ALL.
Tip 18# : Use Indexes to Improve Performance

19

•

Choose and use indexes appropriately. Indexes should have high
selectivity. Bitmapped indexes improve performance when the index has
fewer distinct values like Male or Female

•

Avoid using functions like “UPPER” or “LOWER” on the column that has an
index

•

Index partitioning should be considered if the table on which the index is
based is partitioned. Furthermore, all foreign keys must have indexes or
should form the leading part of Primary Key

•

When using 9i, you can take advantage of skip scans. Index skip scans
remove the limitation posed by column positioning, as column order does
not restrict the use of the index

•

Large indexes should be rebuilt at regular intervals to avoid data
fragmentation. The frequency of rebuilding depends on the extents of
table inserts
20

Thank You
Please visit our official website for more information
http://www.sql-programmers.com/sql-performance-tuning.aspx

More Related Content

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Sql Server performance tuning

  • 2. Tip # 1: Write Sensible Queries • IDENTIFY SQL STATEMENTS THAT ARE TAKING A LONG TIME TO EXECUTE & STATEMENTS THAT INVOLVE THE JOINING OF A LARGE NUMBER OF BIG TABLES AND OUTER JOINS • CHECK TO SEE IF THERE ARE ANY INDEXES THAT MAY HELP PERFORMANCE • TRY ADDING NEW INDEXES TO THE SYSTEM TO REDUCE EXCESSIVE FULL TABLE SCANS 2
  • 3. Tip # 2: Tuning Sub queries • THE MAIN QUERY WILL NOT PERFORM WELL IF THE SUBQUERIES CAN’T PERFORM WELL THEMSELVES • PAY ATTENTION TO CORRELATED SUBQUERIES, AS THEY TEND TO BE VERY COSTLY AND CPU- INSENTIVE. 3
  • 4. Tip 3# :Limit the Number of Tables in a Join • BREAK THE SQL STATEMENT INTO SMALLER STATEMENTS AND WRITING A PL/SQL BLOCK TO REDUCE DATABASE CALLS • USE THE DBMS_SHARED_POOL PACKAGE TO PIN A SQL OR PL/SQL AREA • PINNING PREVENTS MEMORY FRAGMENTATION AND HELPS TO RESERVE MEMORY FOR SPECIFIC PROGRAMS. 4
  • 5. 5 Tip 4# :Recursive Calls Should be Kept to a Minimum • LARGE AMOUNT OF RECURSIVE SQL EXECUTED BY SYS COULD INDICATE SPACE MANAGEMENT ACTIVITIES SUCH AS EXTENT ALLOCATIONS TAKING PLACE. THIS IS NOT SCALABLE AND IMPACTS USER RESPONSE TIME • THE ORACLE TRACE UTILITY TKPROF PROVIDES INFORMATION ABOUT RECURSIVE CALLS, LIBRARY CACHE MISSES AND ALSO THE USERNAME OF THE INDIVIDUAL WHO EXECUTED THE SQL STATEMENT • TKPROF-GENERATED STATISTICS CAN TKPROF_TABLE TO BE QUERIED LATER BE STORED IN A TABLE
  • 6. Tip 5# :Improving Parse Speed • USE OF BIND VARIABLES ALLOWS YOU TO REPEATEDLY USE THE SAME STATEMENTS WHILE CHANGING THE WHERE CLAUSE CRITERIA • THE PARSE PHASE FOR STATEMENTS CAN BE DECREASED BY EFFICIENT USE OF ALIASING 6
  • 7. Tip 6# :Alias Usage • IF AN ALIAS IS NOT PRESENT, THE ENGINE MUST RESOLVE WHICH TABLES OWN THE SPECIFIED COLUMNS • A SHORT ALIAS IS PARSED MORE QUICKLY THAN A LONG TABLE NAME OR ALIAS IF POSSIBLE REDUCE THE ALIAS TO A SINGLE LETTER • Bad Statement • • SELECT first_name, last_name, country FROM employee, countries WHERE country_id = id AND last_name = ‘HALL’; Good Statement • SELECT e.first_name, e.last_name, c.country FROM employee e, countries c WHERE e.country_id = c.id AND e.last_name = ‘HALL’; 7
  • 8. Tip 7# :Driving Tables • THE STRUCTURE OF THE FROM AND WHERE CLAUSES OF DML STATEMENTS CAN BE TAILORED TO IMPROVE THE PERFORMANCE 8
  • 9. Tip 8# :Caching Tables • FOR SMALL, FREQUENTLY USED TABLES, PERFORMANCE MAY BE IMPROVED BY CACHING TABLES • NORMALLY, WHEN FULL TABLE SCANS OCCUR, THE CACHED DATA IS PLACED ON THE LEAST RECENTLY USED (LRU) END OF THE BUFFER CACHE • IF THE TABLE IS CACHED (ALTER TABLE EMPLOYEES CACHE;), THE DATA IS PLACED ON THE MOST RECENTLY USED (MRU) END OF THE BUFFER, AND SO IT IS LESS LIKELY TO BE PAGED OUT BEFORE IT IS RE-QUERIED • CACHING TABLES MAY ALTER THE CBO’S PATH THROUGH THE DATA AND SHOULD NOT BE USED WITHOUT CAREFUL CONSIDERATION 9
  • 10. Tip 9# :Avoid Using Select * Clauses 10 • DO NOT USE THE * FEATURE BECAUSE IT IS VERY INEFFICIENT • THE * HAS TO BE CONVERTED TO EACH COLUMN IN TURN • THE SQL PARSER HANDLES ALL THE FIELD REFERENCES BY OBTAINING THE NAMES OF VALID COLUMNS FROM THE DATA DICTIONARY AND SUBSTITUTES THEM ON THE COMMAND LINE, WHICH IS TIME CONSUMING.
  • 11. Tip 10# :Exists vs. In • • • • THE EXISTS FUNCTION SEARCHES FOR THE PRESENCE OF A SINGLE ROW THAT MEETS THE STATED CRITERIA, AS OPPOSED TO THE IN STATEMENT THAT LOOKS FOR ALL OCCURRENCES PRODUCT - 1000 ROWS ITEMS - 1000 ROWS (A) • • SELECT p.product_id FROM products p WHERE p.item_no IN (SELECT i.item_no FROM items i); (B) • • 11 SELECT p.product_id FROM products p WHERE EXISTS (SELECT ‘1’ FROM items I WHERE i.item_no = p.item_no) FOR QUERY A, ALL ROWS IN ITEMS WILL BE READ FOR EVERY ROW IN PRODUCTS. THE EFFECT WILL BE 1,000,000 ROWS READ FROM ITEMS. IN THE CASE OF QUERY B, A MAXIMUM OF 1 ROW FROM ITEMS WILL BE READ FOR EACH ROW OF PRODUCTS, THUS REDUCING THE PROCESSING OVERHEAD OF THE STATEMENT.
  • 12. Tip 11# :Not Exists vs. Not In • In subquery statements such as the following, the NOT IN clause causes an internal sort/ merge. • SELECT * FROM student WHERE student_num NOT IN (SELECT student_num FROM class) • Instead, use: • SELECT * FROM student c WHERE NOT EXISTS (SELECT 1 FROM class a WHERE a.student_num = c.student_num) 12
  • 13. Tip 12# : In with Minus vs. Not In for Non-Indexed 13 Columns • In subquery statements such as the following, the NOT IN clause causes an internal sort/ merge • SELECT * FROM system_user WHERE su_user_id NOT IN (SELECT ac_user FROM account) • Instead, use: • SELECT * FROM system_user WHERE su_user_id IN (SELECT su_user_id FROM system_user MINUS SELECT ac_user FROM account)
  • 14. Tip 13# :Correlated Subqueries vs. Inline Views 14 • Do not use code correlated subqueries, as they will adversely impact system performance • Instead, use inline views (subqueries in the from clause of your select statements), which perform orders of magnitude faster and are much more scalable
  • 15. Tip 14# :Views Usage 15 • Consider including the view definition in the main query by including its code without the actual view name • Views can cause potential performance problems when they have outer joins (CBO goes haywire with them, even in 9I) or are considered nonmergable views by Oracle.
  • 16. Tip 15# :Use Decode to Reduce Processing 16 • Use DECODE when you want to scan same rows repetitively or join the same table repetitively • SELECT count(*) , sum(sal) FROM emp WHERE deptno = 10 AND ename LIKE ‘MILLER’; • SELECT count(*) , sum(sal) FROM emp WHERE deptno = 20 AND ename LIKE ‘MILLER’;
  • 17. Tip 16# : Use Union in Place of Or 17 • In general, always consider the UNION verb instead of OR verb in the WHERE clauses. • Using OR on an indexed column causes the optimizer to perform a fulltable scan rather than an indexed retrieval.
  • 18. Tip 17# : Use ‘Union All’ Instead of Union 18 • The SORT operation is very expensive in terms of CPU consumption. The UNION operation sorts the result set to eliminate any rows that are within the sub-queries. • UNION ALL includes duplicate rows and does not require a sort. Unless you require that these duplicate rows be eliminated, use UNION ALL.
  • 19. Tip 18# : Use Indexes to Improve Performance 19 • Choose and use indexes appropriately. Indexes should have high selectivity. Bitmapped indexes improve performance when the index has fewer distinct values like Male or Female • Avoid using functions like “UPPER” or “LOWER” on the column that has an index • Index partitioning should be considered if the table on which the index is based is partitioned. Furthermore, all foreign keys must have indexes or should form the leading part of Primary Key • When using 9i, you can take advantage of skip scans. Index skip scans remove the limitation posed by column positioning, as column order does not restrict the use of the index • Large indexes should be rebuilt at regular intervals to avoid data fragmentation. The frequency of rebuilding depends on the extents of table inserts
  • 20. 20 Thank You Please visit our official website for more information http://www.sql-programmers.com/sql-performance-tuning.aspx