Thursday, September 23, 2010

MS SQL Server Query Execution plan - Tweeks

If you have some parameters for a MS SQL server Stored procedure most of the time to be empty, then make sure that you give the empty string check first then the value check in the OR part for ex. ((@FromDate = '' and @ToDate = '') OR (VT.TransactionDate BETWEEN @FromDate AND @ToDate )) will make the execution plan to choose the clustered index of the table, rather than choosing the non-clustered index of the table, if one exists. it would choose the non-clustered index if given like ex. ((VT.TransactionDate BETWEEN @FromDate AND @ToDate) or (@FromDate = '' and @ToDate = ''))

No comments:

Post a Comment