My.ADVISOR.com Sign-In
ID
Password

Member Center / Sign-Up
Go to Article
Advanced Search 

ADVISOR ANSWERS

Move a Table to a Different File Group

Learn how to handle both disk space and filegroups in SQL Server.

By Richard Campbell, technical editor


Q: We've been having problems with disk space on our SQL Server. I've been deleting old, unimportant data, but that isn't enough. I set up additional hard drive space on a separate drive, and I know how to create a new filegroup for the database to use. Is there a way to move existing tables there?

-- Mark McGibney, London, England

A: There are two issues here: One is the disk space issue, and the other relates to using filegroups. The two aren't the same thing. If all you want to do is increase available disk space for your database, you can do that without creating new filegroups. The real purpose of filegroups is to create logical relationships between multiple database files (likely on separate hard drives). You can administer these relationships together. All databases have at least one filegroup -- the primary filegroup -- which is also the default.

So to deal with your primary problem of getting more disk space for your database, you can create a new secondary data file on your hard drive (it'll have the extension .NDF, as opposed to the primary data file with the .MDF extension). When you create the new file, it automatically goes into the primary filegroup and is available for all tables to use.

Deliberately distributing tables across multiple drives is a pretty straightforward performance trick. Of course it only makes sense if you're using the table in such a way that it's likely to need multiple drives; you need to access different areas of the table simultaneously for there to be any benefit. Also you can get about the same benefit using a RAID array (whether 0, 1, or 5) without logically complicating your database. I'm prone to the RAID 5 solution myself; it's cost-effective, simple to operate, and provides extra data protection for the database, because you can have one hard drive fail and the system continues to function normally (albeit with a lot of beeping about the failed drive).

But suppose you don't want all tables distributed to all the database files? Perhaps one particular table (or tables) is the primary bottleneck of your application, and you want to focus on a solution for just that one part of the database. That's where filegroups come in. You can create a new filegroup, assign database files to it, then assign tables to it, and only the tables in that filegroup have access to your distributed database file structure.

It's easy to create a table in a specific filegroup. At the end of the CREATE TABLE statement, add an ON clause, specifying the filegroup, like this:

CREATE TABLE foo (mycol1 int) ON myFileGroup

But what if you have an existing table? The easiest trick I know to move a table from one filegroup to another is to create (or recreate) a clustered index on the table with the ON clause. A clustered index is the physical order of the table itself, unlike unclustered indexes, which are like their own little tables that contain only the columns you specify for the index plus a RID (row identifier). So when you create a clustered index on a new filegroup, it forces SQL Server to move the entire table to the new filegroup.

This is no more arduous than creating a clustered index on the default filegroup, but that doesn't trivialize it. When you create a clustered index, you're essentially rewriting the entire table in a new order. If there are many millions of rows, expect it to take awhile. And it's in your best interest to do a backup of the database before doing such serious operations.

As far as unclustered indexes go, you can improve performance by putting unclustered indexes in a separate filegroup from the actual table. That presumes the two filegroups don't share any hard drives (and preferably don't share any controllers either) and that your queries involve both the table and the index at the same time. In that case, because SQL Server can search both the index and table simultaneously using separate drives, it runs faster.

In SQL Server 2005 we get much smarter filegroups with the advent of partitioning schemes. In partitioning schemes, you can specify what parts of a table go into what filegroups. This lends a lot more control to using multiple drives across multiple filegroups, as well as being able to restrict what tables use multiple filegroups.

I've had situations where using multiple filegroups has worked out great, but you have to do your research first. Use SQL Profiler to study your database's behavior and lots of query analysis to show what the queries do. When you find a situation where the velocity and frequency is high and the query plan makes sense, you benefit greatly from using multiple filegroups.

But it can go the other way too. Using lots of separate database files and filegroups can create so many parallel disk threads that the cost of handling the threads outweighs the benefits of distributing data. Just be careful out there -- take your time and test thoroughly before going filegroup crazy. This is another reason why investing in more advanced hardware -- such as high-performance RAID controllers -- makes a lot of sense: there's very little downside to them and you still have the logical organizational options as well.

This Advisor Tip was originally published in the August 2005 issue of Advisor Guide to Microsoft Access. Subscribers can get more Advisor Tips and how-to advice on succeeding with Microsoft Access technology at MSAccess.AdvisorGuide.com. 

Move a Table to a Different File Group

No reader comments ... yet.

    What do YOU think about this topic? Share your advice and thoughts using this form.

    Your Name

    REQUIRED : PUBLIC

    Your E-Mail

    REQUIRED : PRIVATE

    Job, Company

    OPTIONAL : PUBLIC

    City, State, Country

    OPTIONAL : PUBLIC

    Your Web Site

    OPTIONAL : PUBLIC

    Your Comment

    Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


    Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

    Richard CampbellRichard Campbell is president of Campbell & Associates in Vancouver, British Columbia. He has more than 20 years experience working with computers, and consults with numerous companies including Johnson & Johnson, Barnes&Noble.com, and ADP. In addition to consulting on development issues, Richard speaks at Advisor DevCon and other technical conferences around the world. He has been writing for Advisor publications since 1996. Richard is a technical editor of Advisor Guide to Microsoft Access Magazine, and an Advisor technical board member of Advisor Summit.

    Printer-friendly
    page layout

    Keyword Tags: Database, Database Development, Database Management, File Management, Microsoft, Microsoft SQL Server, Storage

    ADVISORAMA
    Everyone has talent; what is rare is the courage to follow the talent to the dark place where it leads.
    -- Erica Jong, author

    ARTICLE INFO

    DataBased Advisor

    Web Edition: 2005 Week 29, Doc #16671

    Print Edition: August 2005, Page 8

    FREE ACCESS FREE ACCESS

    This article appeared in e-newsletter MICROSOFT ACCESS ADVISOR UPDATE. Click to get free e-newsletter subscriptions.

    SUBSCRIPTION STATUS
    You are not signed-in. If you are a subscriber to this publication, sign-in above to access locked articles. To subscribe or renew go to www.AdvisorStore.com.

    Subscribe to DATABASED.ADVISOR.com

    Get it all -- every current and past ADVISOR tech/business publication, now all-in-one subscription, with new articles and a huge reference library packed with expert advice, how-to and downloads. Subscribe now to get it all.

    DataBased.Advisor.com

    Subscribe to FileMaker Advisor Magazine

    Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.

    FileMaker.Advisor.com

    Subscribe to Advisor Basics of FileMaker Pro

    Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!

    FileMaker.AdvisorBasics.com

    Showcase Your Smarts

    Submit your tips, techniques and advice and let Advisor promote your business and build your career. Show the world what you know!

    AdvisorTips.com

    Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
    Portions copyright ©1983-2008 Advisor Media, Inc. All Rights Reserved.
    Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
    ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, Inc. in the United States and/or other countries.
    Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
    Hosted by Prominic.NET Website powered by
    LOTUS SOFTWARE
    CAMPR166 posted 04/30/2007 modified 01/09/2009 03:36:40 AM ztdbms/ztdbms
    domino-144.advisor.com my.advisor.com 01/09/2009 12:50:39 PM