SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Slow execution in dynamic SQL with OR clause


Slow execution in dynamic SQL with OR clause

Author
Message
Casper101
Casper101
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 1268
I have a stored procedure which accepts parameters and then builds the query and WHERE clause dynamically based on these parameters.
The performance is fine but now I have added another LEFT JOIN to a third table - the LEFT JOIN does not decrease performance - what does decrease performance is the added
OR clause on this table.

For example:
This section is fast:
SET @SQLWhere = @SQLWhere + ' OR (Table1.Col1 =' + QUOTENAME(@Value,'''') + ')'

But extending it like this makes it really slow:
SET @SQLWhere = @SQLWhere + ' OR (Table1.Col1 = '+ QUOTENAME(@Value,'''') + ' OR Table2.Col1 = '+ QUOTENAME(@Value,'''')+')'

The added OR section to Table2.Col1 slows this down from 4 seconds to over a minute.....

any ideas on how I can improve this?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87317 Visits: 45272
Probably missing indexes. OR requires different indexes to AND, without the query executes as a scan usually.

http://sqlinthewild.co.za/index.php/2011/05/03/indexing-for-ors/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search