Archive for September, 2009

Free Training in October at the Quest Connect vConference

Wednesday, September 30th, 2009

Quest Connect is back… and bigger than ever!  Quest Connect 2009 will take place October 21 – 22, beginning at 9 a.m. British Summer Time/4 a.m. Eastern/1 a.m. Pacific and will accommodate all time zones. This is a 24-hour virtual event covering topics across Quest Software’s entire business.  The event is free and you won’t even need to leave their offices to access educational and product content.  Sign up for it at http://www.quest.com/ConnectKevinKlineBlog.

Visit our booth during Quest Connect and our industry experts, like me and Brent Ozar, will field your toughest questions.  Better yet, attend one of our informative webcasts to help you better manage your SQL Server environment:

  • Top 10 SQL Server Administration Mistakes—How to Avoid Problems That Sap Speed from Your Databases (This is my round table session with Jason Strate, Colin Stasiuk, and Mike Walsh.)
  • The Top 5 Things You Can Do with Litespeed for SQL Server to be a Backup and Recovery Hero
  • Understanding Execution Plans
  • A Sysadmin’s Guide to SQL Server–How Windows Administrators Can Manage SQL Server with Confidence

Attend a webcast and be entered into a drawing for a Netbook or Bose® SoundDock® Portable Digital Music System!  Other event sessions and information booths cover these topics:

* Active Directory
* Oracle
* Identity and Access Management
* Monitoring
* SharePoint
* Virtualization
* SQL Server
* Public Sector
* Unified Communications
* The Experts Conference (TEC) 2010
* Compliance
* Professional services and support
* Migration (AD, Exchange, SharePoint)
* Vizioncore
* Notes Migration
* ScriptLogic

Sponsors include many of our key partners:  Microsoft, Dell, NetApp, and ePlus.

The day will be filled with 17 live webcasts with Q&A as well as 50 on-demand webcasts that attendees can view at yourleisure.

Registration is live and we currently have over 1560 registrations so far! I hope to see you there.

-Kevin

Twitter at kekline

See my content at http://KevinEKline.com/

Customized test feed for SSP

Wednesday, September 30th, 2009

SSIS – Performing An UPSERT

Wednesday, September 30th, 2009

For those of you who may be wondering what the heck an UPSERT is, an UPSERT is an UPDATE and INSERT into a given table. Typically, an UPSERT operation requires two separate transactions. Usually, the first transaction issued is the UPDATE and then the INSERT is issued shortly after. In this post I will be extending the package I created, in my previous post, http://jahaines.blogspot.com/2009/09/ssis-only-inserting-rows-that-do-not.html. Once your SSIS package resembles the package in the link, you can proceed with the next step. If you want to skip the prior post, you can download the package at the bottom on this post.

The first item of interest is the Lookup transformation. First I will drag an OLE DB Command onto the canvas and connect it to the Lookup transformation. A dialog box should appear. When the dialog box appears choose the Lookup Match Output, in the Output dropdown list. An OLE DB Command, is a parameterized TSQL statement that can issued against a given data set. Before I start configuring the transformation, I would like to point out that this method is extremely easy to setup, but has a major drawback. The OLE DB Command has to be issued for each row returned by the Input, so this type of process works recursively or iteratively. I will demonstrate another method later in this post. With that out of the way, I will start configuring the transformation. First, I will build an UPDATE command using parameters for each column and the predicate.

My canvas currently looks like the screenshot below.

image

The next step is to configure the OLE DB Command transformation. Double-click the OLE DB Command transformation. In the Connection Manager dropdown list, choose the database where you created dbo.SSIS_Import. Click the “Component Properties” tab. Within the “Component Properties” tab, you will need to click the ellipses next to the “SQL Command” property. Paste the code presented below into the box and click ok.

UPDATE dbo.SSIS_Import
SET 
     SomeInt = ?,
     SomeChar = ?
FROM dbo.SSIS_Import import
WHERE SomeId = ?

The code above is a pretty simplistic UPDATE statement. The key thing to note is the “?”. Each of the ‘?” will be given a parameter value in SSIS. The parameters are dynamically named in the order they appear in the command. Next, I will be mapping these parameters to my input columns. Click the “Column Mappings” tab. Align the input columns to each parameter. My column mapping is shown below. Once complete, click “Ok.”

image

That it!!! We have successfully created an UPSERT operation using SSIS… but wait what is the catch? The catch is the UPDATE will operate like a cursor processing one UPDATE command at a time, which can be a nightmare from a performance standpoint. You may be wondering how we can make this process set based. There is not much you can do in SSIS alone, so I will need to find other means. The best way to make this process more scalable is to leverage SQL Server and SSIS.

The first step is to drop the OLE DB command I just created. I will then drag a OLE DB Destination to the canvas and connect it to the Lookup Match Output. My canvas looks like below.

image

Next, I will switch gears and write some TSQL code. I will need to open SSMS and connect to the database where dbo.SSIS_Import exists. Firstly, I have to create a view. I will not use the view to select data, but will use the view as a intermediate object to insert data.

CREATE VIEW vw_SSIS_Import
AS
SELECT [SomeId],[SomeInt],[SomeChar]
FROM dbo.[SSIS_Import]
GO

Now that my view is created, I am going to create an INSTEAD of trigger, on my view. The instead of trigger will allow me to use my OLE DB Destination to bulk insert from SSIS. The bulk insert from SSIS gives me a mechanism to pass the rows for UPDATE, as a set of data. Once I have the rows in a set, the trigger can efficiently UPDATE the data.

CREATE TRIGGER trg_UPdate_SSIS_Import
ON dbo.vw_SSIS_Import
INSTEAD OF INSERT
AS 
BEGIN
 
    UPDATE import
    SET 
        SomeInt = i.SomeInt,
        SomeChar = i.SomeChar
    FROM dbo.SSIS_Import import
    INNER JOIN inserted i
        ON i.SomeId = import.SomeId
 
END
GO

In the code above, I am using efficient TSQL to update all rows that are inserted into the view, from the SSIS package. I am now ready to configure the OLE DB Destination. Double-click the OLE DB Destination to launch the configure dialog box. Make sure your package is using the right connection manager and choose the view. Click the “Column Mappings” tab and then click '”Ok.” Right-click the OLE DB Destination and choose properties. In the “Fast Load Options”, type “FIRE_TRIGGERS.” This allows the SSIS insert to fire the INSTEAD OF trigger, on the view. That’s it we are done!!! The view/INSTEAD OF TRIGGER method is extremely easy to configure and implement; however, it does require that additional database objects to be created.

There you have it. I have demonstrated two methods to UPSERT data, using SSIS. The first method relies on the OLE DB Command, which has the limitation of having to run for each and every row. The latter option is a more scalable solution that requires the creation of a view and trigger. Both methods have pros and cons and I leave it to you to determine which is best for your environment.

Until next time, Happy Coding.

PS: If you want to do all of the work in SQL, you can allow the trigger to perform the UPDATE and INSERT. This method reduces the complexity of the SSIS package because you only need a single source and destination, with no Lookup.

Download SSIS Package:

Note: The package was created in BIDS 2008, which is not compatible with BIDS 2005.

MidnightDBA News – Podcasts, new DBAs@, and Snacks

Wednesday, September 30th, 2009

MidnightDBA is in iTunes!!  We're getting all the videos published in batches. So far we have something like a couple dozen videos out there, and we'll catch up in very short order.

I also just published a three-part DBAs @ Midnight video from this week: Working From Home (Part 1, Part 2, Part 3).  ALERT!!!! We had a lot of fun making this video but we covered some topics we're passionate about, so in a couple patches the language gets a little rough.  There's a normal amount of cursing but I do manage to slip up and drop an F-bomb once.  So if you're offended easily then perhaps you should skip this one.  I try not to do that, but sometimes it happens.  Anyway, this one is a great time where we talk about the use of DBs in various customer service companies and how nobody uses them to their fullest.  As well we also talk about office dress codes, hot chicks, and other stuff.  Our daughter makes an appearance as well.  So yeah, big fun. 

Here are a few short-term projects we'll have ready for you:

  • We're going to start offering short snippets of DBAs @ Midnight videos as Midnight Snacks.  Think of this as teasers, or highlights...it'll be both.
  • We haven't forgotten that we talked our Tshirts up a couple of weeks back.  We're on it!
  • I'm going to get a section together for Sean's (and eventually, my) webcast and lecture materials.  You know, so you can come and download notes, leave feedback, that sort of thing.
  • In a few weeks we'll have alerts for new videos!  You'll be able to manage your subscription based on what topics you're interested in.  Rockin...

 

-Jen McCown, http://www.MidnightDBA.com

Recovering a deleted SQL Job

Wednesday, September 30th, 2009

A user wanted to recover a SQL job that had been deleted from the agent. We do backups of MSDB, of course, but how would you actually recover the job definition?

Thanks to ServerFault and user squillman, we used the following technique. Restore a copy of MSDB (we used MSDB_old, in this case). Then run the following script:

DECLARE @JobID UNIQUEIDENTIFIER
SELECT @JobID = job_id FROM msdb_old.dbo.sysjobs WHERE NAME='My Lost Job'

INSERT msdb.dbo.sysjobs
SELECT * FROM msdb_old.dbo.sysjobs
WHERE job_id=@JobID

INSERT msdb.dbo.sysjobsteps
SELECT * FROM msdb_old.dbo.sysjobsteps
WHERE job_id=@JobID

SET IDENTITY_INSERT msdb.dbo.sysjobhistory ON
INSERT msdb.dbo.sysjobhistory
    (instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
     [message],run_status,run_date,run_time,run_duration,operator_id_emailed,
     operator_id_netsent,operator_id_paged,retries_attempted,[server])
SELECT
    instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
    [message],run_status,run_date,run_time,run_duration,operator_id_emailed,
    operator_id_netsent,operator_id_paged,retries_attempted,[server]
FROM msdb_old.dbo.sysjobhistory
WHERE job_id=@JobID
SET IDENTITY_INSERT msdb.dbo.sysjobhistory OFF

INSERT msdb.dbo.sysjobschedules
SELECT * FROM msdb_old.dbo.sysjobschedules
WHERE job_id=@JobID

Worked like a charm! Thanks, squillman!
Note that you’d have to alter this script some if you were trying to retrieve an old version of a still existing job (because your job_id would conflict).

SQLSaturday 25 October 10, 2009 – Final Stretch!

Wednesday, September 30th, 2009

Less than two weeks until SQLSaturday 25, and I have lots of stuff to wrap up; however, there’s been some exciting stuff happening in the last couple of weeks.

Attendance is up!

Attendance – Planning to Attend: 95  (as of today)

New Sponsor!

Interworks, Inc has really stepped up to the plate and become a Gold Sponsor for us!  YAY!  The more I hear about this company, the more I like about them.

Student Sessions!

Way back when I agreed to take on this project with Gainesville State College and IESA, it was our intent to make this a student-centered conference as much as possible; unfortunately, things haven’t gone according to plan.  However, we do have two student presentations (and we’re waiting on a few more):

Patrick Taylor: ArcIMS and SQL Server

Jeremy Rylee: Feature Data Object

We’ve also got a full roster of other sessions, which you can view here:

Start Time DBA Development GIS/BI/Special
8:30 AM Trevor Barkhouse Audrey Hammonds Michael Stark
  Deadlock Detection, Troubleshooting, & Prevention The T-SQL Trifecta: Enhancements for Everyone SQL Server 2008 Spatial Data
9:45 AM Neal Waterstreet Robert Cain Sudhanshu Panda
  SQL Server Backups Introduction to SQL Server Integration Services SQL Application in Geographic Information Systems
11:00 AM Trevor Barkhouse Julie Smith Mark Tabladillo
  Leveraging SQLDiag for Efficient Troubleshooting Cool Tricks to Pull from your SSIS Hat— Data Mining Beyond Adventure Works
1:30 PM Stuart Ainsworth Sergey Barskiy Dan Murray
  The Social DBA: Resources for Career Building CLR integration Data Visualization and High Value Low Cost BI
2:45 PM Jeff Ammons Robert Cain College Students
  Data Persistence in a Web Hosted World SQL Server Full Text Searching – An Introduction. Student Showcase Session 1
4:00 PM Chris Semerjian & Allison Ainsworth Cliff Jacobson More College Students
  GIS Certificate Program at IESA DBA vs ORM: Strategies for Data in a Code Focused Student Showcase Session 2

 

And, here comes the pain…

Stuff left to do includes:

  • Follow up with volunteers at IESA to make sure food is ready
  • Speaker dinner arrangements
  • Make sure Speaker shirts are on their way
  • Write my presentation – D’oh!
  • Make sure signage is done properly
  • Remember to breathe

Monitoring Log File Growth using PerfMon

Wednesday, September 30th, 2009

There are lots of ways to monitor data file and log file growth, but my favorite method is to set up a perpetual Counter Log in Windows Perfmon. The SQLServer:Databases object contains the following counters (among others):

  • Data File(s) Size (KB)
  • Log File(s) Size (KB)
  • Log File(s) Used Size (KB)
  • Log Growths
  • Log Shrinks
  • Percent Log Used

No idea why there isn’t a “Data File(s) Used Size (KB)”. You’ll have to use other methods for that.

I prefer to log this to a CSV file, so I can just whip it open in Excel and make a few graphs (ok, nice graphs take a long time, actually…). This lets you see what databases are responsible for using different amounts of log space over time, and helps you determine an ideal “permanent” log size for each database, so you don’t have 100gb set aside for a log that never uses more than 1gb.

Graph Sample

Make sure to capture data more often than you run log backups (if you backup logs every hour, then capture this data every 15 minutes, for example). That way you can see how much log space is actually used between log backups, and possibly make some adjustments to your log backup schedule.

To make sure your perfmon counter restarts automatically when the server gets rebooted, set the START TIME to some time in the past, and set STOP AFTER to 9999 days.

Also, check out Paul Randal’s awesome video attached to his Technet article to see these counters in action!

SQL Lab Adventure: Phase I

Wednesday, September 30th, 2009

It seems that I may be able to free some time from my schedule. To fully utilize this “spare” time, I am planning to go all the way to tech blogging! My plan to build a home-brewed test SQL Server lab is already in place. I have a couple of spare machines (physical) and a spare nice,  shiny 24″ LCD Monitor, plus my ever reliable laptop.

I want to be of help to the SQL Server Community. I want to contribute to the community in little ways. What I would specifically want to do is run some test lab for third party tools  (like Litespeed from Quest Software, SQL Data Generator from Red Gate, etc) and blog about the results. Book reviews would be awesome, too, if I have books to review.

I have no particular audience. Basically my audience is anybody that drops by my blog. And I just hope these people would pick something useful for their own benefits.

I am not an expert in SQL Server so I need inputs from the community to make this effort successful. If you need help in testing such tools, please let me know. I am quite new to testing stuff so I may need inputs on what to look for and stuff.

If you are a vendor and would want me to look into testing your tool in my sql lab, just contact me. It helps if you would send the product spec in advance. Remember this is just a test lab. Not a Real, Production server.

Let me make it clear that I am NOT doing this to gain monetary benefits. I will not NOT accept *anything* in return (with the exception of  job offer, of course). My purpose is for me to gain more knowledge and polish whatever skills I already have and help the community while I am at it. My quest is to find the ultimate path to perfecting solid SQL Server skills.

You can contact me via:

Call or leave a message: (562) 989-5406

eMail: mribunal [at] verizon [dot] net

Twitter: @MarlonRibunal

Thank you all for your support.

Tagged: SQL Server, sql server lab, testing tools, tools review

The Office

Wednesday, September 30th, 2009

I'm a big fan of The Office (and I won't apologize for liking the American series better); I loved Office Space and News Radio. I was even a fan of 9 to 5 as a kid. They're all classic comedy, sure - but you knew this was coming - there's an awful lot of truth to them.  I've worked for bosses sporting near comedic egos, complacency, and density. I interned with a Ryan clone, and Samir and Michael could be any of a dozen developers at four different gigs. I've never worked with anyone quite as funny as Jim, but fair is fair - he has a team of writers behind him. 

As much as I like offices, there are of course downsides.  I think one of the biggest disadvantages, for me, is the paranoia.  You really never know what others are thinking, and I've found more often than not if you're paranoid, you're right, and if you're not, you're oblivious.  (Or is that the paranoia talking?)  I've had close friends complain to the boss over little nothings, had job climates change in the blink of an eye.  The worst case I've seen personally is of a guy Sean worked with - let's call him Betty! - for a year.  When it came down to a layoff where either Sean or Betty was going to get the axe, Betty fabricated a misdeed with Sean's name on it, and got him fired.  That's a good piece of work there, Betty.

I'm not jaded, I'm really not.  And I haven't had any bad experiences in the last three or so offices I've been in (in fact I very much like the current gig, and my stint at MS).  I'm just saying that for me, that little worry is an irritating presence in the workplace.  It's strange, too, because I've been in the industry long enough to understand that, as much as you may love a job / office / environment, it's still just a job.  Even in this economy, if this one evaporates, another one waits.

So, to sum up: offices are funny. And you should be paranoid. But not really, because life is a cookie.

-Jen McCown, http://www.midnightdba.com

There was that one time we thought we poisoned our sexist boss, and we hid him in the trunk, and, well, it was all wacky hijinks...

Book Review: SQL Tuning by Dan Tow

Wednesday, September 30th, 2009

DanTowCover

About a year ago, as I was digging deeper into SQL tuning issues, I came across this StackOverflow question about SQL books and resources. One of the recommendations was for Dan Tow’s book, “SQL Tuning”. The Amazon reviews were fairly glowing, so I made the purchase, and have since had the opportunity to read and apply its ideas. Here are my thoughts.

Overview

SQL Tuning takes a very unique approach to the problem of optimizing SQL queries. Most of the time, books and articles on SQL server performance outline a method that looks something like:

  1. Identify slow queries
  2. Read and understand the SQL execution plan
  3. ??
  4. Profit!

Not really, of course, but its very easy to focus on the first two steps of this process, because it much easier to define concrete methods for them. Step 3 frequently ends up being mostly trial-and-error. Update stats. Play with indexes. Update stats WITH FULLSCAN. Change the logic in your WHERE clause. Hey, did you try updating stats yet??

Even if you do have an idea for an execution plan that might be better than the current one, how do you know if it is the best? How do you know you aren’t still leaving huge potential improvements on the table? How do you know when it’s good enough?

Dan Tow’s book teaches a concrete, repeatable methodology for determining the optimal execution plan for a query (or at least a very-near optimal solution), then shows you how to modify your query to use that new plan. The technique is really database agnostic, but he gives good specific methods for applying his ideas in MS SQL, Oracle, and DB2.

Technique

Without giving away too much, Dan’s technique involves creating a visual diagram of the query (based on joins) and with some fairly easy-to-follow rules, helps you determine:

  • Optimal table join order
  • Optimal indexes on each table

Its based primarily on the theory that you want SQL to retrieve the most selective tables first, so that it is handling the minimum amount of data possible during the entire process, as opposed to joining huge tables and only filtering them out at the end. This idea really is common sense, but for those who like to see the theory behind the technique, he gives a lot of good support to his arguments.

My Thoughts

The method made a lot of sense to me, but did it work? The first day I tried to apply his methods, I was able to take a very long running query (90 minutes or more), and get it down to under 2 minutes!! Can’t argue with that!

Even more than that, though, this book has helped me much better understand what is going on during the execution of a query, and how to bend it to my will (evil laugh). The right index for a table, for example, will depend on where in the query the table is used. Is it the first table retrieved? Is it retrieved in a join?

A few caveats:

  • This was published in 2004, so doesn’t contain any of the new features of SQL 2005 or 2008. SQL 2005 Plan Guides, for example, would probably be preferred to using query hints to actually control the plan behavior.
  • Continue monitoring your application. Hard-coding query hints could become a detriment instead of a help if your database contents change radically enough.
  • Some of the methods can get fairly complex (calculating the proper weight of subqueries, etc).

Overall, though, I thought it was an excellent read, and very worthwhile.

Resources