Articles from February 2010



February PASSMN Meeting

If you’ve got the time, stop in to socialize and learn a bit about SQL Server.  Kalen Delaney will be in town teaching a class over at Benchmark Learning and has offered to join the meeting to talk about Simple Recovery Model.   Also, Barbara Rokke has offered to share  some of her experiences with partitioning in SQL Server.

Here are the meeting details:

Location:

8300 Norman Center Drive, 9th Floor, Bloomington, MN  55437

Date:

February 23, 2010

Time:

4:00 -7:00

Please click here for more meeting details and to RSVP.

Case Study: A Partitioning Strategy for a VLDB

Barbara Rokke, 3M

With our database growing rapidly from MB to TB, maintenance tasks locking users out of data access and a 7×24 data access requirement by the clients, the DBA team needed to come up with a better way of managing the large database. Partitioning was discussed however the DBA team didn’t have time to develop a manual partition strategy. With the release of SQL Server 2005, partitioning became a viable option. After discussions with Microsoft experts and reading every bit of information available on the web (only 2 websites offering valuable information at the time), the DBA team took the leap into the Partitioning world. Partitioning was installed and a two year window processing script was developed to maintain a rolling window of data.

This presentation will provide a review of the thought process and the implementation of the partitioning strategy the DBA team developed. Also a walk-through of the two year window processing to show self-maintenance partitioning will be reviewed.

What is Minimal Logging?

Kalen Delaney

There is a common misconception that Simple Recovery Model means no logging, and this is a very dangerous myth to propagate. SQL Server does log database changes in Simple Recovery, but some (not all) operations are minimally logged. In this session, I’ll discuss what exactly minimal logging means and what the benefits and dangers of Simple Recovery model are.

Please, no TRIM()?

Nose TrimA couple weeks back I was asked, “How come Microsoft has yet to put TRIM() in SQL?”

I don’t really know why there isn’t a TRIM() function in SQL Server. There is an LTRIM for function removing leading spaces. And an RTRIM function for removing trailing spaces. But there isn’t a TRIM function.

Let’s be honest, the why doesn’t matter to me – I don’t want the function to be a part of SQL Server. So Instead of figuring out why, I’m going to write about my opinions. This is a great place for them, this being my blog and all.

There are two main reasons that I am against having a TRIM function in SQL Server…

What Do You Know

First, a large majority of people tend to stick with what they know. Once you find something that works, why not stick with it until there is a reason not to use it any longer. Unfortunately this can have some unintended consequences.

Interactive PlaypenMost of the developers that have built the applications that I consult on were originally designed by application developers versus SQL Server developers. This distinction is important because a lot of these developers are then aware of the .NET or Java function for TRIM in the languages that they are using. In their cases, using it will not have a seriously negative impact on their environments.

But that “knowledge of a function” doesn’t translate well into SQL Server. In SQL Server, the use of TRIM would have a serious impact on the effectiveness of indexing being used to query the database. In case you don’t believe me, this will be demonstrated below.

What Do You Need

Secondly, how often do leading spaces need to be removed from a field? Sure, I’ve removed them while loading data into a database in an ETL process. But most often, leading space saved to a field in a database is just “bad” data that I’d rather clean up.

Are there reasons to have leading spaces in a field? Probably, but a vast majority of the time they won’t be there and they aren’t going to be needed.

By this logic, most of the time only trailing spaces will need to be removed from a field.

Considering the Two Reasons

If leading spaces don’t need to removed but people know about TRIM from their primary programming language, will they be inclined to find out if there is a function the only removes trailing spaces? Or would a person be more inclined to just use what they know. I understand the nature of laziness and pretty confident that people would end up just using TRIM().

Depending on the spaces that need to be removed can have a serious impact on performance. As an example of the performance impact let’s consider the following query.

USE AdventureWorks

SELECT * FROM Person.Contact
WHERE EmailAddress = 'gustavo0@adventure-works.com'

When executed, the query returns the following execution plan:

Trim001

You’ll notice that since there is an index on the column EmailAddress, that the Query Optimizer chooses an Index Seek on the column to find the value(s) the are being filtered. And then it looks up the rest of the columns with a Key Lookup operation.

Suppose though that the column had trailing spaces in it that needed to be removed when querying the data. To do this, you’d need to use the RTRIM() function. The query in this case would likely look like this.

USE AdventureWorks

SELECT * FROM Person.Contact
WHERE RTRIM(EmailAddress) = 'gustavo0@adventure-works.com'

And when executed the query would return the following execution plan:

Trim002

And in this case, the Query Optimizer has chosen to use an Index Scan. It can do this because SQL Server knows how the value begins and can scan for similar values within the index. And then follow-up with a Key Lookup for the rest of the values in the query.

But what we really are talking about is a function that removes the leading and the trailing spaces. To do this you would need to use the LTRIM() function in conjunction with the RTRIM() function. And now the query would look something like this:

USE AdventureWorks

SELECT * FROM Person.Contact
WHERE LTRIM(RTRIM(EmailAddress)) = 'gustavo0@adventure-works.com'

Running that query would produce the following execution plan:

Trim003

As you see now, the execution plan is quite different from the previous two executions. Now, the Query Optimizer doesn’t know the first letters of the values in the EmailAddress column. With the existing index being useless, the query doesn’t have any other good candidate to find the values from the WHERE clause. This results in the query just doing a Clustered Index Scan.

Of course, the last execution plan looks like it is simpler, but closer scrutiny of the execution would show that the IO between each of the queries has some significant differences.

--EmailAddress = 'gustavo0@adventure-works.com'Table 'Contact'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

--RTRIM(EmailAddress) = 'gustavo0@adventure-works.com'
Table 'Contact'. Scan count 1, logical reads 178, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

--LTRIM(RTRIM(EmailAddress)) = 'gustavo0@adventure-works.com'
Table 'Contact'. Scan count 1, logical reads 569, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Based on my people going with what they know, the addition of a TRIM() function would likely result in some serious performance issues with some queries. This would likely happen just as a matter of development without any thought as to the effect and difference between the performance of RTRIM(), LTRIM(), and TRIM().

Some People Want TRIM

Now my opinion isn’t the only one that is out there. Quite often developers I work with ask about the TRIM() function. And Pinal Dave (blogging at SQLAuthority) put in a Connect item regarding adding a TRIM() function. He’s had a number of posts regarding this topic and ways to get around the lack of a TRIM() function.

While I appreciate the discussions I’ve had with him, one of those time on the cursed “it depends”, I’ll have to disagree with him on adding a TRIM() function. The pain in this discovery from people just coming to SQL Server is worth the benefit of unnecessary performance death that could occur by overuse of a TRIM() function. I actually would encourage people to vote down this Connect item.

My Three Events

A while back, I noticed that Paul Randal (on twitter as @paulrandal) had started a new blog meme on three events that brought their life to where it is today.  I’ve skipped a lot the past year’s memes due to time constraints but thought I’d chime in on this one and have a go at it.

Outdoor Wedding ReceptionFirst I Was A DJ

Back when I was an extremely shy version of current myself, I had gone to a wedding.  The DJ sucked.  The musical choices were poor.  Songs were cut off in the middle of play for something new.  Not a fade to something better, but an abrupt stop and reversal of the entire tone of the moment.  And the equipment had no sound quality what-so-ever.  In fact, the DJ sucked so bad that I was convinced I could have done a better job.

As I was "armchair quarterbacking" the DJ the following week, I said that I should do that.  If she could, then I definitely could.  And the I was approached by a co-worker who asked if I started a company if I would DJ her wedding.  After thinking about it a bit, I decided to take her up on the offer.

The biggest problem after getting her money and contract was a general lack of any of the required infrastructure.  I worked out a deal with my uncle to purchase the necessary speakers, mixers, and lights.  And then I started shopping for CDs.  Within a couple months, I was approaching nearly 1,000 CDs and had a new problem.

How do I make music  lists without a lot of work?  How should I catalog the music?  How will I find the next song to play when the current song is playing?

That led me to take a look at the programs on my computer and I found one called… Access 2.0. I wasn’t really sure what it was, but it seemed that it could do what I needed.  I sat down and after hundreds of hours of tinkering over the years, I built a fairly robust database that I could use to manage and organize music for my business.

And I was officially a database hack.  I will qualify, I was a wedding DJ.  I did not have two turntables.  But I did have a microphone.

The Credit Card trapThen I Sucked At Credit Analysis

When  I was a database hack, I hadn’t actually believed anyone could make money or have a career with computer-machines.  So instead, I pretended to go to college and worked as a server.  Somewhere in there I decided to pick up a temp job and started at a bank working on entering credit card applications.  Oh yeah!  Data entry.

I rocked at the data entry and was soon moved off of that to reviewing individual credit cards.  And then off to corporate credit using  the internet to try and research the background and news happening with the bank’s customers.  One of the greatest lessons I learned at this point was that almost everything was on the internet, it was just a matter of submitting the right query.

Doing good at your job can have it’s curses.  I was soon asked to move to another group to do credit analysis within the corporate credit department.  I didn’t have a background in finances but I was young and willing to do the work in front of me.  Long and the short of it, I sucked at credit analysis.  I was so clueless as to what I was doing and trying to fake it while I tried to learn what I needed to know.  I was in over my head and ready to quit.

One of my old managers recognized this and remembered when I was stellar and offered to bring me over to her group rather than my current manager letting me go.  My desire to quit had been a little bit slower than the current managers desire to fire me.  Fortunately, though, before either happened I was moved to a merchant fraud risk group.

The nice thing about this group is that I eventually got a chance to highlight my database hack skills.  They had an Access database that they used for uncovering merchant fraud.  More specifically, they had an Access database that 100% mimicked a mainframe application and was as user friendly.  They even managed to make it uglier than its predecessor.
One thing led to another and when I left this department, I was a SQL Server developer.  The Access database now had a SQL Server backend.  And I was heading to a data warehousing group to write DTS packages.

Divorce Cakes a_005I Got Divorced

Throughout my time at the bank, I had a number of life changing events that occurred.  Some of the highlights were getting married and having four children.  And some of the lows were my daughter and my best friend from my college years passing away.  I worked a lot and didn’t pay attention to my spouse or children nearly as much as I should have.  And really wasn’t having a good time with life.  All of this culminated in getting divorced.

For many divorce is a low point, and I’ll admit to having some bad days after it started.  But as a whole it was one of the best decisions of my life.  And really lead to a lot of success, happiness, and a chance to be a better father to my children.

It gave me an opportunity to re-think my life and career.  This was when I decided that I should control my career and not just let it happen to me, which is when I started blogging and started a new job at Digineer as a consultant.  I also decided to stop putting off things that I wanted to do and live my life in a way that I was satisfied with every day and what I did with it.

Since then I’ve had a lot of fun with my kids.  Enjoyed life and where it is going.  And I can once again count my ex-wife as a very good friend.  Life is good.