Av rating:
Total votes: 4
Total comments: 4


Timothy Ford
Greetings from the Alpaca Mill
06 September 2007

This is a follow-on from the article Do Alpacas dream of Farming DBAs' which you will need to read first in order to make any sense of this!

I would like to extend greetings once again to all of you from my alpaca factory.

Okay, so my lack of understanding how these creatures are manufactured will 'key you into' the fact that I never did jump the cubicle wall and head to the highlands to begin a new venture in raising Vicugna pacos. (I even had to look that up on Google.)

So Dear Readers, what saved me you may ask? Well, collectively you did. Before I get into precisely how I came to resolve my little Implement This dilemma and announce the winners of this contest that our beloved editor dreamed up, I want to thank you all for your kind critiques of my first article as well as the greater-than-anticipated participation in this little effort. Of course it was because Red-Gate dangled an iPod Shuffle in front of your nose – but I am determined to believe that I had just a little something to do with it.

When we last met, I was heading off to start my new enterprise – chucking the mouse and calling it a career thanks to the frustration of dealing with vendors whom have little knowledge or regard towards proper database structure and behavior as well as those members of management with more money than sense; hungry to sign that next contract. I outlined my latest task at hand: to delete a large sum of space with limited resources for doing so. I mentioned that I suffered through many failed attempts before coming to a solution that worked. These numerous failures wore heavy on me. I’ve been associated with Microsoft SQL Server since version 6.5, first as a Programmer, then as a Database Administrator for the last 6 years. I often tapped into both disciplines in order to accomplish many “outside-the-box” issues and thought myself a competent (if not talented) SQL Server professional. I was left questioning that assertion.

Then inspiration struck:

“What if I’m not alone in this process of trial-and-error?”

“What would other SQL professionals do?”

“Would others have been able to solve this the first time?”

I called on you to offer up your solutions and you rose to the challenge! Though I did not see a solution that specifically matched what I did to solve the matter, I did see more similarities than differences between us all. I specifically liked the brevity and simplicity of the response offered by Michael: 'Outsource HR, Delete database'

Before announcing the winners let me take a few moments to look at some the ways I managed to fail as well as the final solution.

Just Run The #$%! Vendor Script!”

I work for a medical system and, though I am not a medical practitioner, I’ve taken the crux of the Hippocratic Oath to heart: “I will prescribe regimens for the good of my patients according to my ability and my judgment and never do harm to anyone.” The script offered by the vendor to clean up orphaned records would not delete any records we desired to keep. Performance hits during the script execution would be of no concern as I had been given the latitude to remove access to the database during my maintenance. Therefore no harm would come to the database. Most-importantly, I had a valid backup.

I placed the following script into the construct of a SQL Agent job in order to take advantage of the notification feature so I would not need to periodically check on the query execution status.

DELETE  FROM tblOrphans
WHERE   FieldX NOT IN SELECT  FieldX
                        
FROM    tblParent1 ) AND FieldX NOT IN (
        
SELECT  Field X
        
FROM    tblParent2 )

Unfortunately I did not think matters through completely because I failed (as many of you suggested) to place the database in Simple Recovery mode first. At certain times, our bravado pushes us into action before we are ready. The end result was a full transaction log and disk volume. Welcome to failure number one.

“Reduce Logging and Then Just Run The #$%! Vendor Script!”

After altering the database to run under Simple Recovery mode I truncated and shrunk the database’s transaction log to a manageable size. I started the SQL job that ran the vendor’s delete script assured that this time I’d thought everything through; and then ran the server’s D drive out of space due to expansion of the tempdb.

According to your responses, this is one key issue that nobody seemed to pick up on. Attention was focused on the database we were attempting to shrink and the server. No thought was given to the SQL instance and the system databases. I have to believe the derived tables created as a result of the IN clauses are what resulted in a bloated tempdb. Having no relevant indexes on either of the parent tables did not help matters either. Figuring I was onto something moved the tempdb files to the C drive and re-ran the DELETE statement. Even with the additional space available on this volume I still ran the drive out of space.

Network Drive to the Rescue

I came to the realization that I had no choice but to bridge the fix across the network. I moved the tempdb files for the SQL instance to a network drive with over 300Gb of free space.

USE master
GO
ALTER DATABASE tempdb 
   MODIFY 
FILE (name tempdevfilename '\\networkshare\tempdb.mdf')
GO
ALTER DATABASE tempdb 
    MODIFY 
FILE (name templogfilename '\\networkshare\templog.ldf')
GO

I then executed the following code to drop the offending indexes.


DROP INDEX dbo.tblOrphans.idx1

DROP INDEX dbo.tblOrphans.idx2 

Using the newly-created space I reclaimed by dropping the indexes I created and loaded a staging table with only the records from tblOrphans that I wished to keep:

SELECT  O.*
INTO    dbo.tblOrphans_KEEP
FROM    dbo.tblOrphans O LEFT JOIN dbo.tblParent1 P1 
    
ON O.FieldX P1.FieldX 
    
LEFT JOIN dbo.tblParent2 P2 ON O.FieldX P2.FieldX
WHERE   P1.FieldX IS NOT NULL AND P2.FieldX IS NOT NULL  

I then proceeded to drop the tblOrphans table after verifying that there were no dependencies placed upon the table.

DROP TABLE tblOrphans
GO
EXEC sp_rename 'tblOrphans_KEEP''tblOrphans'

Afterwards, it was time for cleanup duty. I needed to recreate the indexes, shrink the database, and relocate the files for tempdb back to their original locations on the database server. It was also necessary to delete the files from the network drive after I restarted the SQL services thereby recreating the tempdb files on the database server.

In order to avoid this issue from occurring again I created the following script to be run as a scheduled SQL Agent job:

DELETE  FROM dbo.tblOrphans
FROM    dbo.tblOrphans O 
   
LEFT JOIN dbo.tblParent1 P1 
        
ON O.FieldX P1.FieldX 
   
LEFT JOIN dbo.tblParent2 P2 
        
ON O.FieldX P2.FieldX
WHERE   P1.FieldX IS NULL AND P2.FieldX IS NULL

So, it’s now my responsibility to choose a winner in this little contest. Out of all the responses, the one that best represented what I did belonged to Gila Monster. Though he was not the first to suggest a network drive he detailed most of the steps I took. His use of BCP would be preferable to my SELECT INTO. Mr. Gila Monster, enjoy your iPod Shuffle.

As for the Runners-Up my choices are a little less subjective. David (from South Africa, no last name specified) was the first to suggest using external storage. That was the key to the solution (thinking “outside the box” as it were.) David Buckingham was the first to mention dropping the indexes to gain space locally. He also detailed his solution quite nicely. Finally, there was the previously-mentioned Michael, who so succinctly suggested my company “outsource HR and delete the database”. If I felt I could get away with offering him the iPod I would. David, David, and Michael, please make great use of the contents of your Simple-Talk gift bags.



This article has been viewed 1646 times.
Timothy Ford

Author profile: Timothy Ford

Tim Ford is a senior database administrator with Spectrum Health in Grand Rapids, Michigan. He has been an active volunteer with The Professional Association for SQL Server (PASS) since 2002 and a certified solutions developer (MCSD) since 2001. Currently he is attempting to teach himself web development at www.ford-it.com. In his free time he usually has a camera, game controller, spatula, guitar, handlebars, book, or beer in his hands. His wife and two young sons, Austen and Trevor keep him sane when not driving him completely crazy.

Search for other articles by Timothy Ford

Rate this article:   Avg rating: from a total of 4 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Wow.
Posted by: GilaMonster (view profile)
Posted on: Friday, September 07, 2007 at 12:59 AM
Message: Wow, didn't expect that. Thanks. Can I expect an email from you shortly?

Just one thing, it's not Mr. The real name is Gail.

Subject: GilaMonster
Posted by: Big CT (not signed in)
Posted on: Friday, September 07, 2007 at 7:07 AM
Message: A female DBA named GilaMonster?

Subject: Re: GilaMonster
Posted by: GilaMonster (view profile)
Posted on: Saturday, September 08, 2007 at 2:45 PM
Message: GilaMonster is the nick I've used on forums since university days.

Surprised there's such a thing as a female DBA? Or curious about the nickname?

Honestly, I'm more of a db developer than a DBA.

Subject: Interesting ....
Posted by: FrankAu (view profile)
Posted on: Sunday, September 09, 2007 at 6:30 AM
Message: ... as on an admittedly limited sample size you .SQL dudes certainly like to work harder!

BTW I am curious that this part of several of the proffered statements will work in SQL Server ...

WHERE P1.FieldX IS NOT NULL
AND P2.FieldX IS NOT NULL

In ASE it doesn't behave as a newbie might expect but the COALESCE() function seems to squeak around this by deferring the check until both outer joins have been evaluated.

Finally I was surprised to see the comment "this is one key issue that nobody seemed to pick up on" regarding the use of space during the WIP phase. This for me was _the_ critical issue ... hence the rowcount limiter to manage log consumption. Perhaps I didn't elaborate sufficiently or explicitly state assumptions like truncate log on checkpoint being set.

Anyway I trust your server is now feeling better after its long squawk! Cheers Frank.

 









Phil Factor
Bunnikins!
 When an IT manager is selected as a victim of office politics of a large corporate, it is time for him to engage in... Read more...



 View the blog
SQL Toolbelt 2008: Predominantly an Engineering Task
 The conversion of the Red-Gate tools to be compatible with SQL Server 2008 might not seem, on first... Read more...

Audit Crosschecks
 In this short article, the second of a 2-part series, William suggests a solution, using SQL Data... Read more...

Discovering Security Uses for SQL Compare
 Much of the security of SQL Server is implemented as part of the database schema. This provides some... Read more...

XML Jumpstart Workbench
 In which Robyn and Phil decide that the best way of starting to learn XML is to jump in and take a ride... Read more...

RSS Newsfeed Workbench
 Robyn and Phil decide to build an RSS newsfeed in TSQL, using the power of SQL Server's XML.  Read more...

Beginning SQL Server 2005 Reporting Services Part 1
 Steve Joubert begins an in-depth tour of SQL Server 2005 Reporting Services with a step-by-step guide... Read more...

Ten Common Database Design Mistakes
 Database design and implementation is the cornerstone of any data centric project (read 99.9% of... Read more...

SQL Server Full Text Search Language Features
 SQL Full-text Search (SQL FTS) is an optional component of SQL Server 7 and later, which allows fast... Read more...

Beginning SQL Server 2005 Reporting Services Part 2
 Continuing his in-depth tour of SQL Server 2005 Reporting Services, Steve Joubert demonstrates the most... Read more...

Executing SSIS Packages
 Nigel Rivett demonstrates how to execute all SSIS packages in a given folder using either an SSIS... Read more...

Over 150,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk