Monday, February 2, 2009

Rules for Optimizining SQLServer Query

Some Of the Tips to Optimiza Sqlserver Queries

  1. Table should have primary key
  2. Remove * from SELECT and use columns which are only necessary in code
  3. Remove any unnecessary joins from table
  4. Triggers should not be used if possible, incorporate the logic of trigger in stored procedure
  5. Remove any adhoc queries and use Stored Procedure instead
  6. Check if there is atleast 30% HHD is empty - it improves the performance a bit
  7. If possible move the logic of UDF to SP as well
  8. Table should have minimum of one clustered index
  9. Table should have appropriate amount of non-clustered index
  10. Non-clustered index should be created on columns of table based on query which is running
  11. Following priority order should be followed when any index is created a) WHERE clause, b) JOIN clause, c) ORDER BY clause, d) SELECT clause
  12. Do not to use Views or replace views with original source table
  13. If there is cursor used in query, see if there is any other way to avoid the usage of this (either by SELECT … INTO or INSERT … INTO, etc)