Build your professional network on facebook via our app Go to app
 
 
 
Industry : IT Services Functional Area : Architecture
Activity:  3 comments  680 views  last activity : 07 06 2010 20:18:04 +0000
 Refer 12
Share
 
 
 
Optimization Rules of Thumb
  • Always look at the query plan first. It will show you the optimal current execution plan from the query engine's point of view. Find the most expensive part of the execution plan and start optimizing from there. However, even before that, make sure that the statistics on all tables in your query are up to date, by running the update statistics <TableName> command on all tables in your query.
  • If you see table scan, optimize. Table scan is the slowest possible way of execution. Table scan means not only that no index is used, but that there is no clustered index for this table at all. Even if you can only replace table scan with clustered index scan, it is still worth it.
  • If you see clustered index scan, find out whether it can be replaced with index seek. For that, find what conditions are applied to this table. Usually, conditions exist for two or three fields of the table. Find out the most selective condition (that is, the condition that would produce the smallest number of records if applied alone), and see whether an index on this field exists. Any index that lists this field first will qualify. If there is no such index, create it and see whether the query engine picks it up.
  • If the query engine is not picking up the existing index (that is, if it is still doing a clustered index scan), check the output list. It is possible that seek on your index is faster than clustered index scan, but involves bookmark lookup that makes the combined cost greater than use of a clustered index. Clustered index operations (scan or seek) never need bookmark lookup, since a clustered index already contains all the data. If the output list is not big, add those fields to the index, and see whether the query engine picks it up. Please remember that the combined size is more important than the number of fields. Adding three integer fields to the index is less expensive than adding one varchar field with an average data length of 20.

    Summarizing this rule, try to make your index covering, and see whether it works better than clustered index scan. Please note that it is not always possible to make the query engine pick up your index automatically. A small table or a low-selectivity index will produce clustered index scan, even if your index is covering.

  • If you see bookmark lookup, it means that your index is not covering. Try to make it covering if it makes sense (see the preceding guidelines).
  • The execution plan selected by the query engine may be not the best one. The query engine makes certain assumptions about disk subsystem and CPU cost versus IO cost. These assumptions sometimes can be incorrect. If you don't believe that the query engine's selection is the best one, run a query in the loop for 10 to 15 minutes with automatic selection, change the query to use your index (you will have to use index hint to force it), and then run it for 10 to 15 minutes again. Compare the results to see which one works better.
  • Avoid any operations on the fields, where possible. Some operations will prevent the use of the index on this field even if it exists—for example, the infamous ltrim(rtrim(FieldName)); other operations will degrade the performance. For example, instead of using the condition cast(DateField as varchar(20)) = @dateString, try to convert @dateString to an expression of datetime type first, and then compare it to DateField.
  • Please note that the query engine cost estimate does not include the cost of embedded procedure or function calls. If you compare between plain join and select from table-value functions, the latter would seem to have smaller cost, but it usually does not. In such a situation, use your own metrics to find out which query performs better.
  • When it is not possible to avoid operation on the field, use an index built on that expression. This can be done in two ways:
    • Create a calculated field based on your expression.
    • Create a view, and build an index on it.
Note    SQL Server requires certain conditions to be met in order to allow the use of calculated fields and indexed views (set quoted_identifier on, set arithabort on, and so on).
  • Indexed views are a good way to further speed up the query if you are not satisfied with the results. Indexed view is a clustered index built over the view's select list. You can also define additional indexes for the indexed view, just as you can for any regular table. Indexed views take disk space and involve some maintenance overhead (every time underlying tables change, the indexed view also has to change), but they usually provide a good boost in performance, even after all other optimization techniques are exhausted.
 
3 comments on "Optimization Rules of Thumb"
  Commented by  Bellala Gopinatha Rao, Project Manager Promax Management Consultants    | 01 03 2009 12:42:57 +0000
Good one thanks
  Commented by  Harshil Shukla, Software Developer, Gateway Technolabs Pvt. Ltd    | 12 06 2008 08:32:08 +0000
Good Article for those who wants to boost up the speed SQL Server Performance. I would like to add that also optimizing Queries by using looking execution plan also optimized usage of JOINS can help to boost the performance.
  Commented by  varsha mishra, Analytical Chemistry Manager, rfrac    | 10 30 2008 16:29:07 +0000
nice one
Add your comment on "Optimization Rules of Thumb"

Rate:
Submit
Specialist in IT Recruitment
PCB designers required , Bangalore
Hiring BIW design engineers,Bangalore
Urgent requirement for Seating designers,Bangalore location
Viewers also viewed
  THUMB RULES FOR CALUCULATING THE COST PER SQFT WILL WORK OUT THE CONSTRUCTION INDUSTRY?  
 
0 referals 14 arguments, 8644 views
India vs China
 
218 referals 7 arguments, 686 views
Dynamic Management Views   The dynamic management views (DMV’s) in SQL Server 2005 are designed...
 
11 referals 6 comments, 1828 views
more...  
Recent Knowledge (106)
Online DOCTOR- Informative too....     This site is very informative, which ever diseases you...
 
103 referals 10 comments, 237 views
Recovery of its due has been a hectic exercise for the Banks in the absence of a special...
 
1 referals 1 comments, 0 views
Yes they are going to be here.. and the first destination is Bangalore... Google cars and trikes...
 
1339 referals 12 comments, 443 views
more...  
More From Author
Anyone statement which cause some uncomfort to common man or mass of the people, nothing wrong in saying Sorry. She is good atleast said sorry. Raj Thakre, Rahul Gandhi, Digvijay Singh and many other politicians never say sorry for their mistakes.
Thanks for sharing the article. Good one
@Munshi: Please don't blame RSS for everything. The sounth India treats all north Indians as aliens. Tamilnadu people know Hindi language but feel shame to speak. They treat any others as outsiders... This all haterated are growing for the benifits of...
more...