February 2009
Mon Tue Wed Thu Fri Sat Sun
« Jan   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  

Day February 13, 2009

Column Order Does Matter… Sometimes

I was debugging a query that was giving me some really strange results. Without pasting the query here, basically it does some interesting math to compute the estimated amount of greenhouse gases produced in tons of CO2, N2O, and CH4. So far this is pretty simple, right?

Well, depending on the type of location we have different possible queries and the results are merged with a union and then summed. This is still pretty standard.

Here’s where things got tricky.

The query returned results, we double checked them, and then moved it all to production. Over time there was a need to dynamically select which emissions the users wanted to see, so I changed the query to be nice and dynamic. Unfortunately, as a side effect, I introduced a bug that went completely uncaught because it didn’t actually throw an error.

Basically, my select ended up looking like this (but with real code):

  SELECT CO2,
         CO2Factor,
         N2O,
         N2OFactor,
         CH4,
         CH4Factor
    FROM MagicTable
  UNION ALL
  SELECT CO2,
         N2O,
         CH4,
         CO2Factor,
         N2OFactor,
         CH4Factor
    FROM OtherMagicTable

Can you spot the error?

Basically, since the columns weren’t in the same order, there was something goofy going on in the background and sums weren’t happening correctly and N2O was coming back with a number suspiciously similar to the CO2 number. (If you know anything about greenhouse gas emissions, this should strike you as completley wrong unless I was running a nitrogen factory or something.)

Moral of the story is: Column order can matter.

Unfortunately, I don’t actually understand the why of this and I feel rather bad since one of my primary goals is to understand why errors are occurring before I push fixes into production. In this case, customers had noticed and were complaining. Any ideas in the comments are more than welcome and might just win you a prize. (The prize is my undying thanks.)

Links for 2009.02.12

SQL Server

Update Statistics Before or After an Index Rebuild? Colin Stasiuk talks about when you should update stats in relation to rebuilding/reorganizing indexes. I’m not just linking to this because I was the catalyst for his blog post, but because there are some great things in here and I learned a lot from it.

Best Practices for installing SQL Server service packs, hotfixes, cumulative updates Beatrice Nicolini put together a great list of best practices for keeping your SQL Server installations up to date. Some of this just came intuitively, some of it I didn’t know until I read this.

Index Fragmentation Findings: Part 2, Size Matters Part two of Brent Ozar’s enlightening series on Index Fragmentation. Brent does a great job of explaining index fragmentation, what it means, and why it matters in ways that even the thickest developer turned DBA can understand.

Development

Build a Silverlight game, win $5,000 I don’t think this needs any more description – make a game, ???, win cash. Thanks to Brian H. Prince for bringing this to everyone’s attention!

Things you now know I tagged Rick Kierner in our latest goofy meme and he reciprocated by providing some great advice for developers/DBAs/whatevers on how to enhance their career. Working on a project with Rick really changed the way I looked at a lot of the things I do on a daily basis and I owe him more than he realizes.

General Stuff

How To Drag Your Butt Through That (Fill In The Blank) Book Aaron Alton gives some motivational hints on how to make it through that tedious [subject goes here] book that you’re currently struggling to get through. Now you can get fit AND get nerdy at the same time!

Found Emoticons of the First Two Decades of the 21st Century William Gibson came across this list of electrical plugs somewhere and flipped it around to make some crazy emoticons. My favorite quote from the text is “Sixth row, sixth square from right, is a very bad romantic feeling that nobody will experience until 2012.”

Classy Games (Part 2 of 2) For those of you not familiar with it, Something Awful is normally a humor web site that can best be described as “mildly not safe for work”. Normally I wouldn’t link to them (even though I’m pretty sure I have before). Every Friday they run a Photoshop contest. This week it’s a re-imagining of classic video game covers. There are some artistic gems in here.

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 226 items have been purified.