Excuse the Mess!!!

I’ve bought the Word Press theme Headway and I’ve started working with it to re-do my blog. Headway has so many options and features it’s gonna take me some time to get my head around it. And it’s so much fun, I could easily spend hours testing out all of the features on Headway! Please excuse the mess!!

Just one word for SQL Cruise – WOW!

Words cannot fully describe how awesome SQL Cruise 2010 was.  For me this has been an excellent experience, from getting a chance to connect with other SQL pros to the great sessions put on by Tim Ford (BlogTwitter) and Brent Ozar (BlogTwitter).  With only 14 “Cruisers” in attendance, it is a completely different training experience than you’ll get at a SQL Saturday or the PASS Summit. We had lots of time to connect with fellow SQL geeks and time for fun too!  Check out the pics at SQL Cruise on Flicker.

Each day of the cruise was sponsored by a different company.  And they each gave away a netbook to one lucky cruiser!  Here are the lucky winners:

Monday – Rebecca Mitchell (BlogTwitter) won the SQL Sentry “Search the Ship Team-Building Event”

Tuesday – Karen Lopez (BlogTwitter) won the Quest “Through Your Laptop Bag”

Wednesday – Yanni Smith (BlogTwitter) won the Redgate “Show Us Your Hairy Execution Plans” contest.

Thursday – Erin Stellato (BlogTwitter) won the MSSQLTips.com “SQL Server Poolside Trivia” contest.

Congrats to these lucky gals!  And yes, all the winners were ladies.  Sorry guys, better luck next time!

The technical sessions included a very informative session by Tim on DMV’s (check out his DMV Starter Pack) and a great T-SQL tuning session by Brent. Brent & Tim put on a outstanding event, thanks for all of your time and hard work!  And thanks to Gareth Swanepoel (Twitter) and his lovely wife Jen (Twitter)for hosting the SQLBBQ Sunday night before the cruise started!

One thing I want to note here – I paid out of my own pocket to go to this.  And took vacation time too.  And it was worth every penny (including the overpriced drinks on the ship!!).

I highly recommend SQL Cruise to my fellow members of the SQL Server community.  If you get a chance to go, do it!

Don’t forget the basics when query tuning!

The world of performance tuning, specifically query tuning, is quite vast, with entire books devoted to the subject.  And those books come with pages and pages of dense and complex technical material.  But in your quest to figure out how to get more performance from that slower-than-molasses stored procedure, don’t forget some basic steps:

  1. Define Success. Before you do anything, decide what the goal is?  A 50% reduction in time needed.  75%?  What??  And once you have met the goal stated for success, stop!  I’m sure you have other tasks on your to-do list.  Nobody in your company is going to care if you make it faster then needed. When the customer/end user is happy, move on.
  2. Do no harm.  Whatever you do, don’t make the problem worst then it is.  For example don’t throw a bunch of indexes into your database in an attempt to fix one procedure, without understanding the rest of the queries in your system.  It is better to start off with a review of the code first.  Changing the code will only affect that procedure where as creating or dropping indexes will surely affect other queries on your system.  And please fully test all changes you do make! The last thing you want to do is introduce logical errors into your code.
  3. Document your changes.  Oh yes, the dreaded need for documentation.  I know all of the excuses for not documenting a code change…you’re too busy…nobody will read it…nobody will care.  Trust me, the new developer who comes in behind you will be so grateful if you do this.  And if you need to work with the same code again, you’ll be happy if you document your changes, because you will probably forget what you did 6 months later!  So, write down what you changed before you forget!

Documentation, Just Do It

For the past 6 weeks I’ve been attending a civilian “Boot Camp”, an fitness program designed to help you lose weight and get in shape.  With this program comes the requirement to  write down everything you eat in a food journal.  Oh, how I have hated keeping this food journal, it is harder then the daily exercise sessions.  Why do I want to remember what I ate last week if a lot of it was junk?…but that’s just the point.  If you write your food choices down you are more likely to pick healthy food, not junk.  Keeping track of my eating habits in a journal has given me insight into my eating habits and how they affect my health.

So what does this have to do with documenting our databases?  Well, in my experience the worst databases I’ve come across have the least amount of documentation, in some cases, none.  If you are throwing hacks into your database, you just want to purge the whole event from your mind, not write it down.  We don’t want to remember the hack we had to put into the database or the fact we really don’t like the all the other “junk” that the last developer/DBA put into the poor database either.

But like your diet, the worse the database is, the more it needs documentation.   Like the improvements in our bodily health, improvements in our SQL Servers will be much easier to come by if we take the time to document what we’ve got, as junky as it might be.  But don’t try to document the whole thing at once.  My food journal only asks for what you ate at your last meal or snack, not your food choices for the past month.  A little bit at time makes it more likely that you will actually do it.  So document your databases as you go, a small portion at a time.  That way it won’t feel like such a chore.

I’m going on a SQLCruise!!!

About 2 weeks ago I heard of a SQL Server training event held on a cruise ship.  Huh?  What?  A cruise ship….my curiosity led me to SQLCruise.com to read all about it.  The more I read the more excited I got about going, so I consulted the checkbook and the Hubby, got the green light to go, and signed up!

Just 78 days until we depart.  I can’t wait!

Time to wake up the Blog with some odds and ends

Gee, where did the month of April go to…I look up and here is May.  Lots of work, SQL Saturday, Jury Duty…some fun and not so fun times.

The first ever SQL Saturday to come to SoCal was filled with lots of great education.  Andrew Karcher ( Blog | Twitter ) and his gang did a bang up job putting in a lot of hard work in a very short time to bring this event to the SoCal SQL Server Community.  I can’t wait for the next one!

I did manage to look at my feed reader a few times.  Here are the best links of April:

Rock Stars, Normal People, and You by Brent Ozar  – Quite possibly the most encouraging thing I’ve read in years.  Excellent example of hard work, long hours and some risking taking paying off big time.

The April Linchpin Session – By Seth Godin – Seth is brilliant.  Not much more to add here.

24 hours of PASS – more free training, yea!

T-SQL Tuesday #005 – Reporting – The Round-Up By Aaron Nelson – So much good reporting stuff in here, I can’t list it all

And on the subject of jury duty, well, I’d just rather forget all about it.

Have a happy May 2010!

The two things you need to know about outer joins

Today I’ll discuss two very important items to keep in mind when coding with an outer join.  These issues apply to both the left and right outer join.

Join order matters – with an inner join, the join order does not affect the results set.  The SELECT statement returns the same number of rows regardless of the order in which the tables are listed.  But that is not the case with an outer join.  Here is an example from the AdventureWorks sample database.

First I’ll join the Production.Product table to the Production.ProductInventory table with a left join so I can list all the products(including those without any inventory) and their corresponding quantities.

SELECT     p.Name as ProductName,
        inv.Quantity
FROM Production.Product p
LEFT JOIN Production.ProductInventory inv
    ON p.ProductID = inv.ProductId

But after reviewing the output, I realize that the location name is needed to make this query complete, so I’ll add the location table to the query with an inner join:

SELECT     p.Name as ProductName,
        inv.Quantity
FROM Production.Product p
LEFT JOIN Production.ProductInventory inv
    ON p.ProductID = inv.ProductId
INNER JOIN Production.Location l
    ON l.LocationID = inv.LocationID
ORDER BY p.Name desc

But now something is wrong with the code, because the result set is missing the products without inventory.  Why?

That’s because using an inner join to add the location table filters out the products with no quantity.  Since they have no inventory, they do not have a row in the ProductInventory table and hence nothing to join to in the ON l.LocationID = inv.LocationID clause.  Since I want to keep the rows where inv.Quantity is null, let’s change this to a left join:

SELECT     p.Name as ProductName,
        inv.Quantity
FROM Production.Product p
LEFT JOIN Production.ProductInventory inv
    ON p.ProductID = inv.ProductId
LEFT JOIN Production.Location l
    ON l.LocationID = inv.LocationID

And that works!  All of our products (even if they do not have any inventory) are in the result set.

WHERE clause vs. ON clause

The WHERE clause is typically used to restrict the results of a query.  But in the case of an outer join, using the WHERE clause can give you the wrong results.  Here I want to see all the products and the total quantity of each product currently in work.  So I left join the Production.Product table to the Production.WorkOrder table to see all 504 products and the quantity in work.  Quantity is zero for products not currently in production.

SELECT    p.Name,
        COUNT(wo.WorkOrderID)
FROM Production.Product p
LEFT JOIN Production.WorkOrder wo
    ON p.productID = wo.ProductID
GROUP BY p.Name

But I don’t want to see all work orders, just the most recent ones.  So I try to limit the query to the most recent work orders by putting the StartDate in the WHERE clause:

SELECT    p.Name,
        COUNT(wo.WorkOrderID)
FROM Production.Product p
LEFT JOIN Production.WorkOrder wo
    ON p.productID = wo.ProductID
WHERE wo.StartDate >= ’1/1/2004′
GROUP BY p.Name

But now I have lost my products not currently in production.  So now what?  I’ll move the StartDate restriction to the ON clause, so it will be part of the join:

SELECT    p.name,
        COUNT(wo.WorkOrderID)
FROM Production.Product p
LEFT JOIN Production.WorkOrder wo
    ON p.productID = wo.ProductID
        AND StartDate >= ’1/1/2004′
GROUP BY p.Name

Now I will return all 504 products, with the quantities for the most recent work orders.  So why does using the ON clause work and not the WHERE clause?  There are two reasons.  First the ON clause is applied as part of the join and the WHERE clause is applied after the join.  Secondly, the WHERE clause is applied to the right side of the left outer join, the WorkOrder table, the table we only want rows from if there is a match.

So, don’t be afraid to use an outer join, just be aware of the gotcha’s when coding a left/right join.

Get rid of those old Server and Login Names in the “Connect” Dialog Box

Too many Server and Login Names cluttering up you “Connect to Database Engine” dialog box in 2008?  Do you want it to look like this again:

image

Do a search on “SqlStudio.bin”.  It will be under …\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin.  Close down SSMS, rename the file and start up SSMS again.  Now those boxes are nice and clean for you!

For SQL Server 2005 directions, go here

Review of Microsoft SQL Server Reporting Services – Chapters 8 to 12

This is the final part to my review of this book.  Part one is located here and part two is located here.

Chapter 8 – This is the final chapter on report development.  Several advanced concepts such as using .NET code in expressions to customize report rendering and behavior (good stuff to know!), using subreports, using .NET assemblies, multivalued parameters, and report navigation.  This chapter is chock full of practice reports for you to create.  The one I liked the most was the report that showed how easy it is to put dynamic columns in your report with the Matrix template.  Many a time I wished I had that for reports I build at work using previous versions of RS.

Chapter 9  – This chapter discusses the various rendering formats that you can export your reports to.  A good discussion of what will/won’t export from your report to another format.  Easy, short chapter.

Chapter 10 & 11 – In this part of the book you will learn all about working with the Report Manager.  How to upload reports, set security, schedule reports to be run, is all discussed in these two chapters.  I’ll be honest, I just skimmed these chapters since I work in Report Manager several times a week.  I will come back to review the part on Data Driven subscriptions, that’s new to me.  If you need to administer the reports you create, make sure to read these chapters, if this is new material for you.

Chapter 12 – The final chapter. Whew!  All about customizing Reporting Services.  With a part on editing the RSReportServer.Config file (be careful!!!) and a short list of best practices.

All in all a good book.  Best for those folks just starting out with Reporting Services.  For those who all ready have some experience with RS, it can be a little tedious at times to go through exercises that include tasks that you already have done many times.  Here is the publisher’s web site for the book:  Microsoft SQL Server Reporting Services 2008.

Happy Reading!

Review of Microsoft SQL Server Reporting Services – Chapters 6 & 7

I’m continuing to make my way through this book, although far more slowly then I would like.  If you missed the first part of my review you can find it here.

Chapter 6 – This chapter focuses on using graphics such as charts and images in reports.  This chapter provided a nice overview of graphics, and like the previous chapter, the directions were easy to follow.  I’m not sure if I will use graphics to build reports in my current job, none of the reports we have in production include charts and we have very little use of images.

Chapter 7 – This chapter is called “Kicking it Up a Notch:  Intermediate Reporting” and that is a perfect title for it.  The chapter starts with showing how to create a template to use in future reports.  Much time is spent on improving the presentation of reports and the chapter introduces concepts like totals in reports, grouping, sorting, parameters, and using stored procedures instead of SQL queries.  Lots of meat here and it took me a bit of time to build all the reports in this chapter.

And I can’t remember any typos, not a one!  The only criticism I have is this:  stored procedures should be introduced earlier, maybe even in chapter 5.  Embedded SQL in reports (or applications) is not a good thing.

So far, a very good book, well worth my money.