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


T-SQL Code Optimizers


T-SQL Code Optimizers

Author
Message
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86897 Visits: 45266
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, 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


RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14674 Visits: 9518
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."
EdVassie
EdVassie
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5769 Visits: 3865
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 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005. 14 Mar 2017: now over 40,000 downloads.Disclaimer: All information provided is a personal opinion that may not match reality.Quote: When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist. - Archbishop Hélder Câmara
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12337 Visits: 8546
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"!! BigGrin

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
LOVESSS
LOVESSS
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
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.
Jonathan AC Roberts
Jonathan AC Roberts
SSC Eights!
SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)SSC Eights! (857 reputation)

Group: General Forum Members
Points: 857 Visits: 1905
Can you show use the before and after SQL so we can see how well it did?
rchantler
rchantler
SSC Veteran
SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)

Group: General Forum Members
Points: 252 Visits: 582
The 3rd party tool used was TOAD; neither freeware nor shareware but they do offer a free trial for 30 days.
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14674 Visits: 9518
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."
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12337 Visits: 8546
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 on googles mail service
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85471 Visits: 41081
They must've improved it a lot... I didn't like Toad because it kept croaking on me Wink

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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