Transact-SQL Optimization Tips

来源:互联网 发布:giga365 新域名 编辑:程序博客网 时间:2024/05/17 15:06

Transact-SQL Optimization Tips

  • Use views and stored procedures instead of heavy-duty queries.
    This can reduce network traffic, because your client will send toserver only stored procedure or view name (perhaps with someparameters) instead of large heavy-duty queries text. This can be usedto facilitate permission management also, because you can restrict useraccess to table columns they should not see.
  • Try to use constraints instead of triggers, whenever possible.
    Constraints are much more efficient than triggers and can boostperformance. So, you should use constraints instead of triggers,whenever possible.
  • Use table variables instead of temporary tables.
    Table variables require less locking and logging resources thantemporary tables, so table variables should be used whenever possible.The table variables are available in SQL Server 2000 only.
  • Try to use UNION ALL statement instead of UNION, whenever possible.
    The UNION ALL statement is much faster than UNION,because UNION ALL statement does not look for duplicate rows, and UNIONstatement does look for duplicate rows, whether or not they exist.
  • Try to avoid using the DISTINCT clause, whenever possible.
    Because using the DISTINCT clause will result in some performancedegradation, you should use this clause only when it is necessary.
  • Try to avoid using SQL Server cursors, whenever possible.
    SQL Server cursors can result in some performance degradation incomparison with select statements. Try to use correlated sub-query orderived tables, if you need to perform row-by-row operations.
  • Try to avoid the HAVING clause, whenever possible.
    The HAVING clause is used to restrict the result set returned bythe GROUP BY clause. When you use GROUP BY with the HAVING clause, theGROUP BY clause divides the rows into sets of grouped rows andaggregates their values, and then the HAVING clause eliminatesundesired aggregated groups. In many cases, you can write your selectstatement so, that it will contain only WHERE and GROUP BY clauseswithout HAVING clause. This can improve the performance of your query.
  • If you need to return the total table's row count, you can use alternative way instead of SELECT COUNT(*) statement.
    Because SELECT COUNT(*) statement make a full table scan to returnthe total table's row count, it can take very many time for the largetable. There is another way to determine the total row count in atable. You can use sysindexes system table, in this case. There is ROWScolumn in the sysindexes table. This column contains the total rowcount for each table in your database. So, you can use the followingselect statement instead of SELECT COUNT(*): SELECT rows FROMsysindexes WHERE id = OBJECT_ID('table_name') AND indid <=1
  • Include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement.
    This can reduce network traffic, because your client will notreceive the message indicating the number of rows affected by a T-SQLstatement.
  • Try to restrict the queries result set by using the WHERE clause.
    This can results in good performance benefits, because SQL Serverwill return to client only particular rows, not all rows from thetable(s). This can reduce network traffic and boost the overallperformance of the query.
  • Use the select statements with TOP keyword or the SET ROWCOUNT statement, if you need to return only the first n rows.
    This can improve performance of your queries, because the smallerresult set will be returned. This can also reduce the traffic betweenthe server and the clients.


Index Optimization tips

  • Everyindex increases the time consumption to perform INSERTS, UPDATES andDELETES, so the number of indexes should not be more. Better to usemaximum 4-5 indexes on one table. If you have read-only table, then thenumber of indexes may be more.
  • Try to create indexes on columns that have integer values rather than character values.
  • Ifyou create a composite (multi-column) index, the order of the columnsin the key are very important. Try to order the columns in the key asto enhance selectivity, with the most selective columns to the leftmostof the key.
  • Ifyou want to join several tables, try to create surrogate integer keysfor this purpose and create indexes on their columns.
  • Create surrogate integer primary key if your table will not have many insert operations.
  • Clusteredindexes are more preferable than nonclustered, if you need to select bya range of values or you need to sort results set with GROUP BY orORDER BY.
原创粉丝点击