Learn more about SQL Server tools


Latest from MSSQLTips

Handling Large SQL Server Tables with Data Partitioning

MSSQLTips author Greg Robidoux By:   |   Read Comments (8)   |   Related Tips: 1 | 2 | 3 | 4 | 5 | 6 | More > Partitioning

With the increasing use of SQL Server to handle all aspects of the organization as well as the increased use of storing more and more data in your databases there comes a time when tables get so large it is very difficult to perform maintenance tasks or the time to perform these maintenance tasks is just not available.  In the past, one way of getting around this issue was to partition very large tables into smaller tables and then use views to handle the data manipulation.  With SQL Server 2005 a new feature has been added that handles this data partitioning for you automatically, so the ability to create and manipulate data in partitioned tables is much simpler.

In SQL Server 2005 a new feature called data partitioning offers built-in data partitioning that handles the movement of data to specific underlying objects while presenting you with only one object to manage from the database layer.  The picture below shows how a table may look when it is partitioned.  To the DBA and to the end user it looks like there is only one table, but based on the partition scheme the underling data will be stored in a different partitions and not in one large table.  This makes all of the existing code you have in place work without any changes and you get the advantage of having smaller objects to manage and maintain.

(source: SQL Server 2005 books online)

To create a partitioned table there are a few steps that need to be done:

  1. Create additional filegroups if you want to spread the partition over multiple filegroups. 
  2. Create a Partition Function
  3. Create a Partition Scheme
  4. Create the table using the Partition Scheme
Step Command Notes
1   This is not necessary, you can still use just one filegroup even if you partition the data.
AS RANGE LEFT FOR VALUES (10000, 100000, 1000000) ;
This creates a range of values for the partition. This will create four partitions:
  • values <= 10,000
  • values > 10,000 and <= 100,000
  • values > 100,000 and <= 1,000,000
  • values > then 1,000,000
TO ( flg1, flg2, flg3, flg4 ) ;
This creates the partition scheme to determine where each of the partitions will reside.  In this example we are spreading it over four filegroups:
  • values <= 10,000 (flg1)
  • values > 10,000 and <= 100,000 (flg2)
  • values > 100,000 and <= 1,000,000 (flg3)
  • values > then 1,000,000 (flg4)
4 CREATE TABLE partTable (col1 int, col2 char(10))
ON partScheme1 (col1) ;
This creates the table using the partition scheme partScheme1 that was created in step 2.  The column col1 is used to determine what data gets placed in which partition/filegroup.

After the table has been setup as a partitioned table, when you enter data into the table SQL Server will handle the placement of the data into the correct partition automatically for you.

So, based on the above setup if we run the below commands the data will be placed in the appropriate partition as shown below.

Command Data place in partition / filegroup
INSERT INTO partTable (col1, col2) VALUES (25, 'Test1') 1 / flg1
INSERT INTO partTable (col1, col2) VALUES (1234, 'Test1') 1 / flg1
INSERT INTO partTable (col1, col2) VALUES (10243, 'Test1') 2 / flg2
INSERT INTO partTable (col1, col2) VALUES (25000000, 'Test1') 4 / flg4
INSERT INTO partTable (col1, col2) VALUES (-2523, 'Test1') 1/ flg1

To determine what exists in each partition you can run the following command:

SELECT $PARTITION.partRange1(col1) AS Partition, COUNT(*) AS [COUNT]
FROM dbo.partTable
GROUP BY $PARTITION.partRange1(col1)
ORDER BY Partition ;

Here is the result from running the above query on our simple test of record inserts.

In addition to determining the number of rows that are in each of the partitions we can also see how fragmented each of these partitions are.  By using the DMV sys.dm_db_index_physical_stats we can get this information

SELECT object_id, partition_number, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(),OBJECT_ID(N'dbo.partTable'), NULL , NULL, NULL);

Based on this results from sys.dm_db_index_physical_stats, you can rebuild an index for a particular partition.  Here is an example of the code that could be used to rebuild index IX_COL1 only on partition #4.

ON dbo.PartTable
REBUILD Partition = 4;

As you can see this is a great enhancement to SQL Server.  The only downside is that it only exists in the Enterprise and Developer editions.

Next Steps

Last Update: 3/15/2007

About the author
MSSQLTips author Greg Robidoux
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.

View all my tips

print tip Print  
Become a paid author

Learn more about SQL Server tools

Post a comment or let the author know this tip helped you.

       All comments are reviewed, so stay on subject or we may delete your comment.

*Name   *Email Notify for updates

       Note: your email address is not published. Required fields are marked with an asterisk (*)

Get free SQL tips:

*Enter Code refresh code     

Tuesday, January 28, 2014 - 6:09:50 AM - Nirav Kothari Read The Tip

 thx for sharing the partition concept.

Tuesday, April 23, 2013 - 10:07:48 AM - Greg Robidoux Read The Tip

@murali - are you exporting data from SQL Server out to a text file?  Not exactly sure what you need to do.

Tuesday, April 23, 2013 - 7:57:51 AM - murali Read The Tip


I want to retrieve the data which is in bulk [1 lakh rows] from sqlserver 2008 R2. When i am retrieving the data it is taking so much time......How can i handle this? any other ways to crack this issue?




Thanks in advance.

K. Murali Krishna.

Monday, September 10, 2012 - 8:44:16 AM - Greg Robidoux Read The Tip

@Daniel - yes you could use this approach to handle very large tables and archive older data very quickly.  Just not that you will need the Enterprise version of SQL Server.


See this tip as well: http://www.mssqltips.com/sqlservertip/1406/switching-data-in-and-out-of-a-sql-server-2005-data-partition/


Sunday, September 09, 2012 - 4:56:46 PM - Daniel Alvarado Read The Tip

Hi Greg.

This partion table could works for a table with increments about 5 millon records per day?

We're having problems with this detail table, so I'm looking for the best option to manage this table.


PS: Actually the table have 31 millon records and growing every day.

Monday, July 02, 2012 - 10:30:22 AM - Greg Robidoux Read The Tip

Kranthi - see if this tip helps: http://www.mssqltips.com/sqlservertip/1406/switching-data-in-and-out-of-a-sql-server-2005-data-partition/


Sunday, July 01, 2012 - 12:47:20 AM - kranthi Read The Tip

Hi Greg

How to partition a table which has data in it.

Sunday, July 01, 2012 - 12:45:11 AM - kranthi Read The Tip

Very good explanation Greg, this post made me understand database partitioning very easily.


More SQL Server Solutions



Get Free SQL Tips










BI Professionals


Q and A

Today's Tip







Tip Categories

Search By TipID



First Timer?


Free T-shirt



User Groups

Author of the Year

More Info








Copyright (c) 2006-2015 Edgewood Solutions, LLC All rights reserved
Some names and products listed are the registered trademarks of their respective owners.