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 ««123»»

T-SQL Code Optimizers Expand / Collapse
Author
Message
Posted Saturday, November 01, 2008 2:45 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: Yesterday @ 11:47 AM
Points: 41,525, Visits: 34,442
clive (10/31/2008)
What is the preferred option, with(nolock) or with(readpast).


Depends. Do you want to read dirty data or skip locked rows entirely. Better option would be to use neither and make sure that your queries are efficient and indexes appropriate. That will reduce the amount of locks taken and needed

What is the preferred option, if exists or if @@rowcount.


Depends what you're doing. However, checking for existence is usually faster than counting all the rows to see if the count is > 1. Especially if there are a lot of rows

What is the preferred option, temporary table or derived table.


Depends. Test both and see which is faster. Sometimes doing a query all in one is fastest. Sometimes putting intermediary pieces into a temp table is fastest

What is the preferred option, static or dynamic query.


Not sure what you mean here

What is the preferred option, top or set rowcount.


Top, as Rowcount is partially deprecated in SQL 2005 and 2008



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 #595408
Posted Saturday, November 01, 2008 8:29 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
While I agree with everything that Jeff and Gail said, I wanted to add to a couple of these:

clive (10/31/2008)
What is the preferred option, if exists or if @@rowcount.
If @@ROWCOUNT is already set then you should use it. Otherwise, use EXISTS() as Gail pointed out.

What is the preferred option, temporary table or derived table.
The general thinking is to prefer derived tables because they leave the optimizer with more freedom to optimize. However, experience also shows that the more complex the query, the less likely the optimizer is going to make the best choices. Consequently, for significantly complex queries, converting one or more derived tables to temporary tables serves both as a way to force certain decisions on the optimizer and as a way to simplify the query, thus allowing the optimizer to make better decisions with what remains. The catch here is that if you convert the wrong derived table to a temporary table, then you end up forcing a bad choice on the optimizer, thus making the overall performance worse instead of better.

What is the preferred option, static or dynamic query.
Static is preferred because it is safer, easier to read and write and (marginally) faster. However, some parametrized queries cannot be done as static SQL and many more can only be done in a terribly inefficient way. In these cases you should use dynamic SQL instead, but with great caution to avoid the possibility of Injection.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #595447
Posted Monday, November 10, 2008 4:37 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:21 AM
Points: 2,797, Visits: 3,079
A lot of the comments I have seen on this thread warn about the pitfalls of adding hints to T-SQL code. I agree that if you try to optimise the code manually then you need to be very cautious about adding hints, as very few people have the skills to use the right hint for the given circumstance. I have seen various optimising legends that adding this or that tweak will improve performance, long after the need to make the tweak disappeared due to improvements in the SQL Optimiser.

If you have a tool that optimises T-SQL DML code by adding hints, it is only as good as its own optimiser. Some products may have a very good optimiser (e.g. IBM Data Joiner) that is able to produce a better access path than the SQL Server optimiser in some situations. However, you should treat the resulting DML as valid for your current release of SQL Server only. If you move to a new SQL Server version, any optimised code should be regenerated as hints that were good in the old version may be counter-productive in the new SQL Server version.

Ultimately very few organisations make extensive use of SQL optimisers, due to the implied need to refactor the resulting SQL whenever the database software is upgraded. Most organisations prefer code stability to extacting ultimate performance, but in the right situation a SQL optimiser can deliver a business advantage by speeding up a critical query.

I guess the above is a long way of saying 'it depends'.


Original author: SQL Server FineBuild 1-click install and best practice configuration of SQL Server 2014, 2012, 2008 R2, 2008 and 2005. 25 March 2014: now over 28,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Concept: "Pizza Apartheid" - the discrimination that separates those who earn enough in one day to buy a pizza if they want one, from those who can not.
Post #599742
Posted Monday, November 10, 2008 8:24 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 8:43 PM
Points: 4,128, Visits: 5,836
EdVassie (11/10/2008)

.
.
.
I guess the above is a long way of saying 'it depends'.


No need to 'guess' there! That was indeed a LONG way of saying "it depends"!! :D


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #599910
Posted Tuesday, November 11, 2008 3:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 28, 2011 1:50 AM
Points: 26, Visits: 25
Hey, what is the third party tool that you have used. Is it a freeware or a shareware ? Can you share the link.

Post #600527
Posted Tuesday, November 11, 2008 10:18 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, April 14, 2014 5:08 AM
Points: 331, Visits: 1,356
Can you show use the before and after SQL so we can see how well it did?
Post #600800
Posted Tuesday, November 11, 2008 12:21 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 2:10 PM
Points: 109, Visits: 348
The 3rd party tool used was TOAD; neither freeware nor shareware but they do offer a free trial for 30 days.
Post #600877
Posted Tuesday, November 11, 2008 1:05 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
Toad has a Query Optimizer? Huh, I didn't know that...

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #600891
Posted Tuesday, November 11, 2008 1:27 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 8:43 PM
Points: 4,128, Visits: 5,836
rbarryyoung (11/11/2008)
Toad has a Query Optimizer? Huh, I didn't know that...


IIRC they purchased it from a company that developed it years ago. I tested the product out some way back when. VERY slick stuff even then!


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #600902
Posted Tuesday, November 11, 2008 9:07 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:19 AM
Points: 35,956, Visits: 30,245
They must've improved it a lot... I didn't like Toad because it kept croaking on me ;)

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #601097
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse