SlideShare a Scribd company logo
8/10/13 SQL QueryTuning or QueryOptimization
beginner-sql-tutorial.com/sql-query-tuning.htm 1/5
SQL Tuning or SQL Optimization
Sql Statements are used to retrieve data from the database. We can get same results by writing different sql
queries. But use of the best query is important when performance is considered. So you need to sql query
tuning based on the requirement. Here is the list of queries which we use reqularly and how these sql queries
can be optimized for better performance.
SQL Tuning/SQL Optimization Techniques:
1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;
2) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use
HAVING clause for any other purposes.
For Example: Write the query as
SELECT subject, count(subject)
FROM student_details
WHERE subject != 'Science'
AND subject != 'Maths'
GROUP BY subject;
Instead of:
8/10/13 SQL QueryTuning or QueryOptimization
beginner-sql-tutorial.com/sql-query-tuning.htm 2/5
SELECT subject, count(subject)
FROM student_details
GROUP BY subject
HAVING subject!= 'Vancouver' AND subject!= 'Toronto';
3) Sometimes you may have more than one subqueries in your main query. Try to minimize the number of
subquery block in your query.
For Example: Write the query as
SELECT name
FROM employee
WHERE (salary, age ) = (SELECT MAX (salary), MAX (age)
FROM employee_details)
AND dept = 'Electronics';
Instead of:
SELECT name
FROM employee
WHERE salary = (SELECT MAX(salary) FROM employee_details)
AND age = (SELECT MAX(age) FROM employee_details)
AND emp_dept = 'Electronics';
4) Use operator EXISTS, IN and table joins appropriately in your query.
a) Usually IN has the slowest performance.
b) IN is efficient when most of the filter criteria is in the sub-query.
c) EXISTS is efficient when most of the filter criteria is in the main query.
For Example: Write the query as
Select * from product p
where EXISTS (select * from order_items o
where o.product_id = p.product_id)
Instead of:
Select * from product p
where product_id IN
(select product_id from order_items
5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as
SELECT d.dept_id, d.dept
FROM dept d
8/10/13 SQL QueryTuning or QueryOptimization
beginner-sql-tutorial.com/sql-query-tuning.htm 3/5
WHERE EXISTS ( SELECT 'X' FROM employee e WHERE e.dept = d.dept);
Instead of:
SELECT DISTINCT d.dept_id, d.dept
FROM dept d,employee e
WHERE e.dept = e.dept;
6) Try to use UNION ALL in place of UNION.
For Example: Write the query as
SELECT id, first_name
FROM student_details_class10
UNION ALL
SELECT id, first_name
FROM sports_team;
Instead of:
SELECT id, first_name, subject
FROM student_details_class10
UNION
SELECT id, first_name
FROM sports_team;
7) Be careful while using conditions in WHERE clause.
For Example: Write the query as
SELECT id, first_name, age FROM student_details WHERE age > 10;
Instead of:
SELECT id, first_name, age FROM student_details WHERE age != 10;
Write the query as
SELECT id, first_name, age
FROM student_details
WHERE first_name LIKE 'Chan%';
Instead of:
SELECT id, first_name, age
FROM student_details
WHERE SUBSTR(first_name,1,3) = 'Cha';
8/10/13 SQL QueryTuning or QueryOptimization
beginner-sql-tutorial.com/sql-query-tuning.htm 4/5
Write the query as
SELECT id, first_name, age
FROM student_details
WHERE first_name LIKE NVL ( :name, '%');
Instead of:
SELECT id, first_name, age
FROM student_details
WHERE first_name = NVL ( :name, first_name);
Write the query as
SELECT product_id, product_name
FROM product
WHERE unit_price BETWEEN MAX(unit_price) and MIN(unit_price)
Instead of:
SELECT product_id, product_name
FROM product
WHERE unit_price >= MAX(unit_price)
and unit_price <= MIN(unit_price)
Write the query as
SELECT id, name, salary
FROM employee
WHERE dept = 'Electronics'
AND location = 'Bangalore';
Instead of:
SELECT id, name, salary
FROM employee
WHERE dept || location= 'ElectronicsBangalore';
Use non-column expression on one side of the query because it will be processed earlier.
Write the query as
SELECT id, name, salary
FROM employee
WHERE salary < 25000;
Instead of:
SELECT id, name, salary
FROM employee
8/10/13 SQL QueryTuning or QueryOptimization
beginner-sql-tutorial.com/sql-query-tuning.htm 5/5
WHERE salary + 10000 < 35000;
Write the query as
SELECT id, first_name, age
FROM student_details
WHERE age > 10;
Instead of:
SELECT id, first_name, age
FROM student_details
WHERE age NOT = 10;
8) Use DECODE to avoid the scanning of same rows or joining the same table repetitively. DECODE can also be
made used in place of GROUP BY or ORDER BY clause.
For Example: Write the query as
SELECT id FROM employee
WHERE name LIKE 'Ramesh%'
and location = 'Bangalore';
Instead of:
SELECT DECODE(location,'Bangalore',id,NULL) id FROM employee
WHERE name LIKE 'Ramesh%';
9) To store large binary objects, first place them in the file system and add the file path in the database.
10) To write queries which provide efficient performance follow the general SQL standard rules.
a) Use single case for all SQL verbs
b) Begin all SQL verbs on a new line
c) Separate all words with a single space
d) Right or left aligning verbs within the initial SQL verb

More Related Content

What's hot

Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
Balqees Al.Mubarak
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
Salman Memon
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
arpit bhadoriya
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
DataminingTools Inc
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
Salman Memon
 
How to replace an XML Configuration file with a Java Configuration file in a ...
How to replace an XML Configuration file with a Java Configuration file in a ...How to replace an XML Configuration file with a Java Configuration file in a ...
How to replace an XML Configuration file with a Java Configuration file in a ...
Jayasree Perilakkalam
 
1. Pointer Basic
1. Pointer Basic1. Pointer Basic
1. Pointer Basic
Gem WeBlog
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data Base
Salman Memon
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
Salman Memon
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Salman Memon
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrankRodenbaugh
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
Salman Memon
 
View Inheritance in Odoo 15
View Inheritance in Odoo 15View Inheritance in Odoo 15
View Inheritance in Odoo 15
Celine George
 
Manipulating Data Oracle Data base
Manipulating Data Oracle Data baseManipulating Data Oracle Data base
Manipulating Data Oracle Data base
Salman Memon
 
Select Clause
Select ClauseSelect Clause
Select Clause
Dhirendra Chauhan
 

What's hot (20)

Les10
Les10Les10
Les10
 
Les08
Les08Les08
Les08
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Les05
Les05Les05
Les05
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
How to replace an XML Configuration file with a Java Configuration file in a ...
How to replace an XML Configuration file with a Java Configuration file in a ...How to replace an XML Configuration file with a Java Configuration file in a ...
How to replace an XML Configuration file with a Java Configuration file in a ...
 
1. Pointer Basic
1. Pointer Basic1. Pointer Basic
1. Pointer Basic
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data Base
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
View Inheritance in Odoo 15
View Inheritance in Odoo 15View Inheritance in Odoo 15
View Inheritance in Odoo 15
 
Manipulating Data Oracle Data base
Manipulating Data Oracle Data baseManipulating Data Oracle Data base
Manipulating Data Oracle Data base
 
Select Clause
Select ClauseSelect Clause
Select Clause
 

Viewers also liked

Lect 08 materialized view
Lect 08 materialized viewLect 08 materialized view
Lect 08 materialized view
Bilal khan
 
Cloud architectural patterns and Microsoft Azure tools
Cloud architectural patterns and Microsoft Azure toolsCloud architectural patterns and Microsoft Azure tools
Cloud architectural patterns and Microsoft Azure tools
Pushkar Chivate
 
PostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active RecordPostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active Record
David Roberts
 
OracleMaterializedviews & Hierarchical Data
OracleMaterializedviews & Hierarchical DataOracleMaterializedviews & Hierarchical Data
OracleMaterializedviews & Hierarchical Data
Amit Sahoo
 
Part 20 comments on mv
Part 20 comments on mvPart 20 comments on mv
Part 20 comments on mv
Girija Muscut
 

Viewers also liked (6)

Lect 08 materialized view
Lect 08 materialized viewLect 08 materialized view
Lect 08 materialized view
 
Cloud architectural patterns and Microsoft Azure tools
Cloud architectural patterns and Microsoft Azure toolsCloud architectural patterns and Microsoft Azure tools
Cloud architectural patterns and Microsoft Azure tools
 
PostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active RecordPostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active Record
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
OracleMaterializedviews & Hierarchical Data
OracleMaterializedviews & Hierarchical DataOracleMaterializedviews & Hierarchical Data
OracleMaterializedviews & Hierarchical Data
 
Part 20 comments on mv
Part 20 comments on mvPart 20 comments on mv
Part 20 comments on mv
 

Similar to Sql query tuning or query optimization

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
Stored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sqlStored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sql
nishantdavid9
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
bishnupriya Panda
 
75864 sql
75864 sql75864 sql
75864 sql
bansalaman80
 
Chapter08
Chapter08Chapter08
Chapter08
sasa_eldoby
 
Linq
LinqLinq
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
oracle content
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
Pongsakorn U-chupala
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
Dhananjay Goel
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Sql performance vesl technologies
Sql performance vesl technologiesSql performance vesl technologies
Sql performance vesl technologies
Suresh Mishra
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
Madhusha15
 

Similar to Sql query tuning or query optimization (20)

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Stored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sqlStored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sql
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
75864 sql
75864 sql75864 sql
75864 sql
 
Chapter08
Chapter08Chapter08
Chapter08
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
Linq
LinqLinq
Linq
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
Les18
Les18Les18
Les18
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql performance vesl technologies
Sql performance vesl technologiesSql performance vesl technologies
Sql performance vesl technologies
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 

More from Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clauseVivek Singh
 

More from Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 

Recently uploaded

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

Sql query tuning or query optimization

  • 1. 8/10/13 SQL QueryTuning or QueryOptimization beginner-sql-tutorial.com/sql-query-tuning.htm 1/5 SQL Tuning or SQL Optimization Sql Statements are used to retrieve data from the database. We can get same results by writing different sql queries. But use of the best query is important when performance is considered. So you need to sql query tuning based on the requirement. Here is the list of queries which we use reqularly and how these sql queries can be optimized for better performance. SQL Tuning/SQL Optimization Techniques: 1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'. For Example: Write the query as SELECT id, first_name, last_name, age, subject FROM student_details; Instead of: SELECT * FROM student_details; 2) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use HAVING clause for any other purposes. For Example: Write the query as SELECT subject, count(subject) FROM student_details WHERE subject != 'Science' AND subject != 'Maths' GROUP BY subject; Instead of:
  • 2. 8/10/13 SQL QueryTuning or QueryOptimization beginner-sql-tutorial.com/sql-query-tuning.htm 2/5 SELECT subject, count(subject) FROM student_details GROUP BY subject HAVING subject!= 'Vancouver' AND subject!= 'Toronto'; 3) Sometimes you may have more than one subqueries in your main query. Try to minimize the number of subquery block in your query. For Example: Write the query as SELECT name FROM employee WHERE (salary, age ) = (SELECT MAX (salary), MAX (age) FROM employee_details) AND dept = 'Electronics'; Instead of: SELECT name FROM employee WHERE salary = (SELECT MAX(salary) FROM employee_details) AND age = (SELECT MAX(age) FROM employee_details) AND emp_dept = 'Electronics'; 4) Use operator EXISTS, IN and table joins appropriately in your query. a) Usually IN has the slowest performance. b) IN is efficient when most of the filter criteria is in the sub-query. c) EXISTS is efficient when most of the filter criteria is in the main query. For Example: Write the query as Select * from product p where EXISTS (select * from order_items o where o.product_id = p.product_id) Instead of: Select * from product p where product_id IN (select product_id from order_items 5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship. For Example: Write the query as SELECT d.dept_id, d.dept FROM dept d
  • 3. 8/10/13 SQL QueryTuning or QueryOptimization beginner-sql-tutorial.com/sql-query-tuning.htm 3/5 WHERE EXISTS ( SELECT 'X' FROM employee e WHERE e.dept = d.dept); Instead of: SELECT DISTINCT d.dept_id, d.dept FROM dept d,employee e WHERE e.dept = e.dept; 6) Try to use UNION ALL in place of UNION. For Example: Write the query as SELECT id, first_name FROM student_details_class10 UNION ALL SELECT id, first_name FROM sports_team; Instead of: SELECT id, first_name, subject FROM student_details_class10 UNION SELECT id, first_name FROM sports_team; 7) Be careful while using conditions in WHERE clause. For Example: Write the query as SELECT id, first_name, age FROM student_details WHERE age > 10; Instead of: SELECT id, first_name, age FROM student_details WHERE age != 10; Write the query as SELECT id, first_name, age FROM student_details WHERE first_name LIKE 'Chan%'; Instead of: SELECT id, first_name, age FROM student_details WHERE SUBSTR(first_name,1,3) = 'Cha';
  • 4. 8/10/13 SQL QueryTuning or QueryOptimization beginner-sql-tutorial.com/sql-query-tuning.htm 4/5 Write the query as SELECT id, first_name, age FROM student_details WHERE first_name LIKE NVL ( :name, '%'); Instead of: SELECT id, first_name, age FROM student_details WHERE first_name = NVL ( :name, first_name); Write the query as SELECT product_id, product_name FROM product WHERE unit_price BETWEEN MAX(unit_price) and MIN(unit_price) Instead of: SELECT product_id, product_name FROM product WHERE unit_price >= MAX(unit_price) and unit_price <= MIN(unit_price) Write the query as SELECT id, name, salary FROM employee WHERE dept = 'Electronics' AND location = 'Bangalore'; Instead of: SELECT id, name, salary FROM employee WHERE dept || location= 'ElectronicsBangalore'; Use non-column expression on one side of the query because it will be processed earlier. Write the query as SELECT id, name, salary FROM employee WHERE salary < 25000; Instead of: SELECT id, name, salary FROM employee
  • 5. 8/10/13 SQL QueryTuning or QueryOptimization beginner-sql-tutorial.com/sql-query-tuning.htm 5/5 WHERE salary + 10000 < 35000; Write the query as SELECT id, first_name, age FROM student_details WHERE age > 10; Instead of: SELECT id, first_name, age FROM student_details WHERE age NOT = 10; 8) Use DECODE to avoid the scanning of same rows or joining the same table repetitively. DECODE can also be made used in place of GROUP BY or ORDER BY clause. For Example: Write the query as SELECT id FROM employee WHERE name LIKE 'Ramesh%' and location = 'Bangalore'; Instead of: SELECT DECODE(location,'Bangalore',id,NULL) id FROM employee WHERE name LIKE 'Ramesh%'; 9) To store large binary objects, first place them in the file system and add the file path in the database. 10) To write queries which provide efficient performance follow the general SQL standard rules. a) Use single case for all SQL verbs b) Begin all SQL verbs on a new line c) Separate all words with a single space d) Right or left aligning verbs within the initial SQL verb