Site sponsored by: Idera The gold standard of SQL Server performance monitoring & diagnostics.
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

System Data Collection Reports
Recover Data Using Database Snapshots
Analyze and Fix Index Fragmentation in SQL Server 2008
Powerful Geographical Visualisations made easy with SQL 2008 Spatial (Part 2) ...

More     
 
Latest FAQ's

How to alter a User Defined Data Type?
How to unzip a File in SSIS?
How to view previous query plans?
ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> performance tuning >> How to Optimize the Use of the ...

How to Optimize the Use of the "OR" Clause When Used with Parameters

By : Preethiviraj Kulasingham
Jul 20, 2005

Page 2 / 2

Once this issue was identified, I created another procedure with a different approach, to return the same data.

Create Procedure select_Proc2
@Key1 int=0,
@Key2 int=0
As
BEGIN
IF @Key1 <>0 and @Key2<>0
Select key3
From Or_Table
Where Key1 =@Key1 AND
Key2 =@Key2
ELSE
IF @Key1<>0
Select key3
From Or_Table
Where Key1 =@Key1
Else
Select key3
From Or_Table
Where Key2 =@Key2
END
GO

This causes a change in the execution plan.

The tests were also carried out with an increase data load (a 5x increase on key2 data), as created by the script below.

-- Insert Additional Test Data
Declare @Key1 int, @Key2 Int
Set @Key1 =1
While @Key1<100
BEGIN
Set @Key2 =21
While @Key2<100
BEGIN
Insert Or_Table ( Key1, key2, Key3)
Values(@Key1, @key2, 'Data '+Convert(varchar, @Key1) +', '+Convert(varchar, @Key2) )
Set @Key2 =@Key2+1
END
Set @key1= @Key1+1
END

The resulting change in the execution plan is drastic.

In comparing the two plans, it becomes clear that with the latter stored procedure, SQL Server is in a position to recompile the stored procedure and thereby decide on what index should be used.

In summary, when using parameter validations with “OR” clauses in combining two conditions, be aware of the potential issues that could arise. This does not mean that the “OR” clause does not have any positive uses; it is definitely handy to simplify and reduce code, in maintaining readability, and can be used in different scenarios. If the WHERE clause in question is SARG-able, (that means the WHERE clause can use indexes) the “OR” clause will be useful. A condition like (Key1 =@Value1 OR Key1 = @Value2) is far better than re-writing the entire code with an IF condition per key. You can also use the OR clause with parameters against non SARG-able fields, as the lack of indexes will mean no additional processing overhead will occur.

The performance of the stored procedure can be optimized yet further. As SQL Server keeps only one execution plan per stored procedure, that means IF conditions may force SQL Server to re-create another execution plan when different parameter values are used. In the scenario above, creating separate stored procedures for each IF condition would result in three distinct execution plans which will reside in memory and can be re-used. Unfortunately, this approach would result in changes to code, in the consuming application, and is not practical.


<< Prev Page         








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved