SQLServerCentral Editorial

The Set-Based Limit

,

In my years of working with SQL Server, I have tried to avoid cursors and RBAR type solutions as much as possible. I have preached that thinking in sets and trying to solve problems with groups of data rather than trying to work with individual rows was the best way to do things. I've considered procedural solutions, which I have classified as RBAR-type writings, as inherently worse than set-based solutions.

When I saw Linchi She's "There is a Limit to Set Based Solutions", I was intrigued. Linchi has had many, many great performance based posts over the years and I always learn something from reading them. In this post, Linchi mentions that sometimes simply following the advice of always building a set-based solution leads to code that might not perform as well as a procedural solution.

I would agree with that, but I'd mention that the problem with most peoples' code is that they don't really understand what or why they write something a certain way. Someone learns that an outer join is good when you have some parents without children, and that they scan for NULLs to find those rows without matches. They might not be completely comfortable with the outer join structure, and have modified someone else's code, so they build a view on this, incorporate it into other code, perhaps even start joining views with views, and at some point performance is horrible.

The issue here isn't the set-based solution, it's a poorly built solution. It's a lack of knowledge and understanding of basic query structure and optimizer functionality, and that's OK. It's my job, and that of others in the SQL community, to try and educate others and raise the awareness about how to properly build queries, something I think Jeff Moden has done a great job doing.

Linchi also mentions that using hints is a form of procedural programming. That's an interesting idea, and I can see how it breaks the declarative model of SQL  and gets into the details. However just as we might tune a mechanical device to perform better in a particular situation, I think hints are worth using in some times and places. Just be sure you really understand what you are doing before you start sticking hints into every query you write.

Steve Jones


The Voice of the DBA Podcasts

Everyday Jones

The podcast feeds are now available at sqlservercentral.mevio.com to get better bandwidth and maybe a little more exposure :). Comments are definitely appreciated and wanted, and you can get feeds from there.

Overall RSS Feed:

or now on iTunes!

Today's podcast features music by Everyday Jones. No relation, but I stumbled on to them and really like the music. Support this great duo at www.everydayjones.com.

I really appreciate and value feedback on the podcasts. Let us know what you like, don't like, or even send in ideas for the show. If you'd like to comment, post something here. The boss will be sure to read it.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating