Transact-SQL optimization tips
- UseViews and stored proceduresInstead of heavy-duty queries.
This can reduce network traffic, because your client will send
Server only stored procedure or view name (perhaps with some
Parameters) instead of large heavy-duty queries text. This can be used
To facilitate permission management also, because you can restrict user
Access to table columns they shocould not see.
- Try to useConstraintsInstead of triggers, whenever possible.
Constraints are much more efficient than triggers and can boost
Performance. So, you shoshould Use constraints instead of triggers,
Whenever possible.
- UseTable VariablesInstead of temporary tables.
Table variables require less locking and logging resources
Temporary tables, so table variables shocould be used whenever possible.
The table variables are available in SQL Server 2000 only.
- Try to useUnion all statementInstead Union , Whenever possible.
The Union all statement is much faster Union ,
Because Union all statement does not look for duplicate rows, and Union
Statement does look for duplicate rows, whether or not they exist.
- TryAvoid using the distinctClause, whenever possible.
Because using the distinct clause will result in some performance
Degradation, you shocould use this clause only when it is necessary.
- TryAvoid using SQL Server cursors, Whenever possible.
SQL Server cursors can result in some performance degradation in
Comparison with select statements. Try to use correlated sub-query or
Derived tables, if you need to perform row-by-row operations.
- TryAvoid the having clause, Whenever possible.
The having clause is used to restrict the result set returned
The group by clause. When you use group by with the having clause,
Group by clause divides the rows into sets of grouped rows and
Aggregates their values, and then the having clause eliminates
Undesired aggregated groups. In your cases, you can write your select
Statement so, that it will contain only where and group by clses
Without 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 return
The total table's row count, it can take very processing time for the large
Table. There is another way to determine the total row count in
Table. You can use sysindexes system table, in this case. There is rows
Column in The sysindexes table. This column contains the total row
Count for each table in your database. So, you can use the following
Select statement instead of select count (*): Select rows from
Sysindexes where id = object_id ('table _ name') and indid <= 1
- IncludeSet Nocount On Statement into your stored proceduresTo stop the message indicating the number of rows affected by a T-SQL statement.
This can reduce network traffic, because your client will not
Receive the message indicating the number of rows affected by a T-SQL
Statement.
- Try to restrict the queries result setUsing the whereClause.
This can results in good performance benefits, because SQL Server
Will return to client only maid, not all rows from
Table (s). This can reduce network traffic and boost the overall
Performance of the query.
- Use the select statementsTop keyword or the set rowcountStatement, if you need to return only the first n rows.
This can improve performance of your queries, because the smaller
Result set will be returned. This can also reduce the traffic
The server and the clients.
Index optimization tips
- Every
Index increases the time consumption to perform inserts, updates and
Deletes, so the number of indexes shocould not be more. Better to use
Maximum 4-5 indexes on one table. If you have read-only table, then
Number of indexes may be more.
- Try to create indexes on columns that have integer values rather than character values.
- If
You create a composite (Multi-column) index, the order of the columns
In the key are very important. Try to order the columns in the key
To enhance selectivity, with the most selective columns to the leftmost
Of the key.
- If
You want to join several tables, try to create surrogate integer keys
For this purpose and create indexes on their columns.
- Create surrogate integer primary key if your table will not have insert operations.
- Clustered
Indexes are more preferable than nonclustered, if you need to select
A range of values or you need to sort results set with group by or
Order.