Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

Odd Construct in a WHERE clause Expand / Collapse
Author
Message
Posted Wednesday, September 3, 2008 1:18 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 5, 2012 9:52 AM
Points: 86, Visits: 103
Couple of things that can help to speed it up....

Declare the GETDATE() as a variable and call it out... Helps with processing time.

Add "WITH (NOLOCK)" to each and every table in your join statments. If you can call out an actual index this can help.

Change this line
LEFT JOIN (SELECT * FROM ORDER_DEAL) od1 ON TITLE.title_no = od1.order_no AND TITLE.company_id = od1.company_id
To
LEFT JOIN ORDER_DEAL AS od1 WITH (NOLOCK) ON TITLE.title_no = od1.order_no AND TITLE.company_id = od1.company_id

Move "OR" query to last part of where clause
AND (TITLE.sub_no = @sub_no OR @sub_no = 0 ) -- all subscrober (0) or one subscriber

New code can be improved
AND TITLE.title_code NOT BETWEEN 123 and 134 -- SAFE products
AND TITLE.title_code NOT BETWEEN 135 and 138 -- SAFE Products *** this was the new code ***
No need for second line just make it
AND TITLE.title_code NOT BETWEEN 123 and 138 -- SAFE products


Is this a large table
(SELECT * FROM ORDER_DEAL WHERE product_id = 'P' AND product_sub_code = 123)

Because you could think about writing this to a temp table and then referencing it in the actual query
Post #563342
Posted Wednesday, September 3, 2008 1:23 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 42,962, Visits: 36,115
First thing is that there's absolutely no need for the temp table. Just do the select straight with the order by included.

Which tables is it scanning and what do the indexes on that table look like?

While it's running, look in sysprocesses and see if there's a last wait type. If so, what is it?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #563344
Posted Wednesday, September 3, 2008 1:24 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 5, 2012 9:52 AM
Points: 86, Visits: 103
GilaMonster (9/3/2008)
First thing is that there's absolutely no need for the temp table. Just do the select straight with the order by included.


Good catch, Gila, completly went right over that code.
Post #563348
Posted Thursday, September 4, 2008 2:35 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 42,962, Visits: 36,115
parackson (9/3/2008)

Add "WITH (NOLOCK)" to each and every table in your join statments. If you can call out an actual index this can help.


Just bear in mind that NOLOCK means dirty reads, so if the data's changing a lot, you may read something you don't want to. There's also the chance of missing rows completely or reading rows twice under certain circumstances.

Nolock essentially means to SQL "I don't care if my result set is slightly inaccurate."

If the data is not changing a lot, then you are probably not running into lock waits and hence nolock won't help much. Nolock is not a silver bullet to be added to every select statement without careful consideration.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #563628
Posted Thursday, September 4, 2008 10:05 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 5, 2013 12:19 PM
Points: 371, Visits: 797
Can you try changing the order in your WHERE clause to 'WHERE (@num1 = 0 or colOne = @num1)'?


Post #563958
Posted Thursday, September 4, 2008 11:40 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 42,962, Visits: 36,115
Glen Sidelnikov (9/4/2008)
Can you try changing the order in your WHERE clause to 'WHERE (@num1 = 0 or colOne = @num1)'?


Won't make a difference. Order of clauses in the WHERE are unimportant. SQL doesn't do short-circuit evaluation and the optimiser's free to evaluate the order of expressions however it likes, providing the meaning is the same



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #564037
Posted Thursday, September 4, 2008 11:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 5, 2013 12:19 PM
Points: 371, Visits: 797
I remember reading in one of the blogs that SQL Server does short-circuit evaluation, but it was not specified under what conditions.
Post #564046
Posted Thursday, September 4, 2008 12:40 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 42,962, Visits: 36,115
It can 'short-circuit' but not on the basis of where the expressions are in the where clause, rather which indexes are used and which expressions those indexes can 'evaluate'

If we say have a query with a where clause
WHERE B > 8 and A=16
and there's a nonclustered index on A that the optimiser chooses to use, then the B > 8 will only be evaluated on the rows that the index returns, ie the rows where A=16

You could call that a form of short-circuit. I prefer not to as the phrase brings to mind what C# and other languages do, which is a position-based short-circuit.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #564089
Posted Thursday, September 4, 2008 1:00 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 5, 2013 12:19 PM
Points: 371, Visits: 797
Gail,

would it be then proper to consider that on a table with search on an unindexed column the

'WHERE @param = something or field = @param" and
"WHERE field = @param or @param = something"

will be returning the same elapsed time and CPU time in server execution times?
I doubt. I can run some tests, but would assume that the second where clause should have CPU time and server execution higher than first.
Post #564106
Posted Thursday, September 4, 2008 1:14 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 4:51 AM
Points: 283, Visits: 1,115
I have written a brief description of this exact problem here:

OR Condition Performance

I have lost count of the number of times I've seen this, and at first sight it looks like a good idea. But the fact is, it won't use an index, so an alternative like dynamic SQL has got to be considered.

I haven't tried the COALESCE solution. I can feel a test coming on :)



Check Your SQL Servers Quickly and Easily
www.sqlcopilot.com
Post #564116
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse