SlideShare a Scribd company logo
8/10/13 SQL QueryTips or QueryOptimization
beginner-sql-tutorial.com/sql-tutorial-tips.htm 1/2
Beginner SQL Tutorial Tips
Here is a few SQL Tips that may save you time and trouble with your SQL table work, SQL command statements
...
Ideally, you want to get best results when writing SQL queries, minimizing errors and getting the best query
performance when needed. Here is a small list of queries tips that may help you in your SQL queries work and
that can optimized SQL for better performance.
SQL Tutorial Tips:
1) SELECT only the columns needed, avoid using SELECT *. First, for each column that you do not need every
SQL Server performs additional work to retrieve and return them to the client, and the second volume of data
exchanged between the client and SQL Server increases unnecessary.
2) SELECT only the rows needed. The less rows retrieved, the faster the query will run.
3) Prune SELECT lists. Every column that is SELECTed consumes resources for processing. There are several
areas that can be examined to determine if column selection is really necessary.
Example: WHERE (COL8 = ‘X’)
If a SELECT contains a predicate where a column is equal to one value, that column should not have to be
retrieved for each row, the value will always be ‘X’.
4) When you create a new table always create a unique clustered index belong to it, possibly it is a numeric
type.
5) Use JOIN instead of subqueries. As a programmer, subqueries are something that you can be tempted to use
and abuse. Subqueries, as show below, can be very useful:
SELECT a.id,
(SELECT MAX(created)
FROM posts
WHERE author_id = a.id)
AS latest_post FROM authors a
8/10/13 SQL QueryTips or QueryOptimization
beginner-sql-tutorial.com/sql-tutorial-tips.htm 2/2
Although subqueries are useful, they often can be replaced by a join, which is definitely faster to execute.
SELECT a.id, MAX(p.created) AS latest_post
FROM authors a
INNER JOIN posts p
ON (a.id = p.author_id)
GROUP BY a.id
6)

More Related Content

Similar to Sql query tips or query optimization (20)

PDF
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
PPTX
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
PPTX
SQL Server 2012 Best Practices
Microsoft TechNet - Belgium and Luxembourg
 
PPTX
Database optimization
EsraaAlattar1
 
PDF
Tips for Database Performance
Kesavan Munuswamy
 
PPTX
Database Performance Tuning
Arno Huetter
 
PDF
query optimization
Dimara Hakim
 
PDF
Att160 f.tmp
bishnupriya Panda
 
ODP
SQL Tunning
Dhananjay Goel
 
PPTX
Quick & Easy SQL Tips
Ike Ellis
 
PDF
Speed up sql
Kaing Menglieng
 
PPTX
Elegant and Efficient Database Design
Becky Sweger
 
PDF
10 sql tips
Yogui Osasuno
 
PPTX
Tips & Tricks SQL in the City Seattle 2014
Ike Ellis
 
PPTX
Mastering-SQL-Your-Guide-to-Database-Development.pptx
Tpoint Tech || Online Education
 
PPTX
Quick & Easy SQL Tips
Ike Ellis
 
PPTX
Query Optimization in SQL Server
Rajesh Gunasundaram
 
PDF
Zurich2007 MySQL Query Optimization
Hiệp Lê Tuấn
 
PDF
Zurich2007 MySQL Query Optimization
Hiệp Lê Tuấn
 
PDF
Sql db optimization
Nikhildas P C
 
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
SQL Server 2012 Best Practices
Microsoft TechNet - Belgium and Luxembourg
 
Database optimization
EsraaAlattar1
 
Tips for Database Performance
Kesavan Munuswamy
 
Database Performance Tuning
Arno Huetter
 
query optimization
Dimara Hakim
 
Att160 f.tmp
bishnupriya Panda
 
SQL Tunning
Dhananjay Goel
 
Quick & Easy SQL Tips
Ike Ellis
 
Speed up sql
Kaing Menglieng
 
Elegant and Efficient Database Design
Becky Sweger
 
10 sql tips
Yogui Osasuno
 
Tips & Tricks SQL in the City Seattle 2014
Ike Ellis
 
Mastering-SQL-Your-Guide-to-Database-Development.pptx
Tpoint Tech || Online Education
 
Quick & Easy SQL Tips
Ike Ellis
 
Query Optimization in SQL Server
Rajesh Gunasundaram
 
Zurich2007 MySQL Query Optimization
Hiệp Lê Tuấn
 
Zurich2007 MySQL Query Optimization
Hiệp Lê Tuấn
 
Sql db optimization
Nikhildas P C
 

More from Vivek Singh (20)

PPS
C programming session 14
Vivek Singh
 
PPS
C programming session 13
Vivek Singh
 
PPS
C programming session 11
Vivek Singh
 
PPS
C programming session 10
Vivek Singh
 
PPS
C programming session 08
Vivek Singh
 
PPS
C programming session 07
Vivek Singh
 
PPS
C programming session 05
Vivek Singh
 
PPS
C programming session 04
Vivek Singh
 
PPS
C programming session 02
Vivek Singh
 
PPS
C programming session 01
Vivek Singh
 
PPS
C programming session 16
Vivek Singh
 
PDF
Niit aptitude question paper
Vivek Singh
 
PDF
Excel shortcut and tips
Vivek Singh
 
PDF
Sql where clause
Vivek Singh
 
PDF
Sql update statement
Vivek Singh
 
PDF
Sql tutorial, tutorials sql
Vivek Singh
 
PDF
Sql subquery
Vivek Singh
 
PDF
Sql select statement
Vivek Singh
 
PDF
Sql rename
Vivek Singh
 
PDF
Sql order by clause
Vivek Singh
 
C programming session 14
Vivek Singh
 
C programming session 13
Vivek Singh
 
C programming session 11
Vivek Singh
 
C programming session 10
Vivek Singh
 
C programming session 08
Vivek Singh
 
C programming session 07
Vivek Singh
 
C programming session 05
Vivek Singh
 
C programming session 04
Vivek Singh
 
C programming session 02
Vivek Singh
 
C programming session 01
Vivek Singh
 
C programming session 16
Vivek Singh
 
Niit aptitude question paper
Vivek Singh
 
Excel shortcut and tips
Vivek Singh
 
Sql where clause
Vivek Singh
 
Sql update statement
Vivek Singh
 
Sql tutorial, tutorials sql
Vivek Singh
 
Sql subquery
Vivek Singh
 
Sql select statement
Vivek Singh
 
Sql rename
Vivek Singh
 
Sql order by clause
Vivek Singh
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Controller Request and Response in Odoo18
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Ad

Sql query tips or query optimization

  • 1. 8/10/13 SQL QueryTips or QueryOptimization beginner-sql-tutorial.com/sql-tutorial-tips.htm 1/2 Beginner SQL Tutorial Tips Here is a few SQL Tips that may save you time and trouble with your SQL table work, SQL command statements ... Ideally, you want to get best results when writing SQL queries, minimizing errors and getting the best query performance when needed. Here is a small list of queries tips that may help you in your SQL queries work and that can optimized SQL for better performance. SQL Tutorial Tips: 1) SELECT only the columns needed, avoid using SELECT *. First, for each column that you do not need every SQL Server performs additional work to retrieve and return them to the client, and the second volume of data exchanged between the client and SQL Server increases unnecessary. 2) SELECT only the rows needed. The less rows retrieved, the faster the query will run. 3) Prune SELECT lists. Every column that is SELECTed consumes resources for processing. There are several areas that can be examined to determine if column selection is really necessary. Example: WHERE (COL8 = ‘X’) If a SELECT contains a predicate where a column is equal to one value, that column should not have to be retrieved for each row, the value will always be ‘X’. 4) When you create a new table always create a unique clustered index belong to it, possibly it is a numeric type. 5) Use JOIN instead of subqueries. As a programmer, subqueries are something that you can be tempted to use and abuse. Subqueries, as show below, can be very useful: SELECT a.id, (SELECT MAX(created) FROM posts WHERE author_id = a.id) AS latest_post FROM authors a
  • 2. 8/10/13 SQL QueryTips or QueryOptimization beginner-sql-tutorial.com/sql-tutorial-tips.htm 2/2 Although subqueries are useful, they often can be replaced by a join, which is definitely faster to execute. SELECT a.id, MAX(p.created) AS latest_post FROM authors a INNER JOIN posts p ON (a.id = p.author_id) GROUP BY a.id 6)