|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, April 05, 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 37,648,
Visits: 29,900
|
|
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, April 05, 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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 37,648,
Visits: 29,900
|
|
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
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, January 22, 2013 12:08 PM
Points: 371,
Visits: 794
|
|
Can you try changing the order in your WHERE clause to 'WHERE (@num1 = 0 or colOne = @num1)'?
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 37,648,
Visits: 29,900
|
|
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
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, January 22, 2013 12:08 PM
Points: 371,
Visits: 794
|
|
| I remember reading in one of the blogs that SQL Server does short-circuit evaluation, but it was not specified under what conditions.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 37,648,
Visits: 29,900
|
|
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
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, January 22, 2013 12:08 PM
Points: 371,
Visits: 794
|
|
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.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: 2 days ago @ 4:59 AM
Points: 242,
Visits: 879
|
|
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 :)
 FREE DOWNLOAD www.sqlcopilot.com
|
|
|
|