Sql query tips or query optimization

218 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
218
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql query tips or query optimization

  1. 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. 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)

×