In this post I have compiled all the SQL Server Query performance
improvement tips which I used all the time or the ones I found on the net. As
every SQL query performance improvement article says, there are no hard and fast rules
as such. Still keep using some basic techniques would definitely help us to
some extent.
1)
Dirty Reads - add WITH(NOLOCK) for reads
When
select data from a database table we could read from the table without locking it(it does not need to wait for other processes to release the lock).
(Use READ UNCOMMITTED). But there is a risk of reading an uncommitted row that
is subsequently rolled back.
SELECT Column1,Column2
FROM [Table1] WITH(NOLOCK) WHERE Column1='somevalue'
2)
Use intermediate result set
This option might not be apt for all the cases. But we could use it wherever possible.
3)
Use char,varchar instead of nchar,nvarchar
4)
Avoid cursors
5) Use inline expression instead of user defined
function
6) Parameter sniffing
To avoid this we need to use local parameters especially in complex search queries.
7) Use db schema name before sp names
8) Choosing between temporary table and table variable
9)
Avoid select *
10)
De-normalization if appropriate
In some complex queries, joining multiple huge tables would likely give a
performance issue which is quite unavoidable. But in any case, if we are to join to some big
table from which we need only one or two columns and if avoiding the join would
give any performance improvement, then we can redundantly save the particular column
values in some other relevant table which we use in the join.
11) Proper indexing
Indexing is a very important
factor in SQL query performance. This
should be discussed as a separate topic I believe.
12)
Not use high length data type as a primary key
13) Use IF EXISTS instead of SELECT COUNT(*) – this is
not tested
14) Use BETWEEN instead of IN –this is not tested
15) Avoid functions in where clause
16) Use sort only when it's needed
17) Give priority to frequently used values in IN
clause –This is not tested
18) Use full text search
to search char or varchar columns–not tested
19)
Use EXISTS instead of IN – Having said that
“EXISTS” works faster than “IN” operator. But this is not tested.
Hope this helps.
Hope this helps.