1. 8/10/13 SQL QueryTips or QueryOptimization
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
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:
WHERE author_id = a.id)
AS latest_post FROM authors a
2. 8/10/13 SQL QueryTips or QueryOptimization
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