By: Brady Upton | Last Updated: 2013-01-09 | Comments (9) | Partitioning
SQL Server Partitioning can be a bit confusing and can lead some companies in a different direction as far as archiving data. Writing the T-SQL code to create partition functions, partition schemes, and then altering the table to use the partition can be overwhelming for some DBA's. SQL Server 2008 introduced a table partitioning wizard in SQL Server Management Studio that helps make this task easier. In this tip, I'll show you how to use this wizard. Let's jump in.
Based on my experience, it seems as if SQL Server table partitioning is not commonly used and I'm not sure if it's because of the complexity and management of partitions or if companies simply don't have the knowledge of partitioning data. There are a few tips that introduce you to partitioning that you can find here. This tip will focus on the SQL Server Partitioning wizard as opposed to the ins and outs of partitioning.
To start the wizard, right click on the table you want to partition in SQL Server Management Studio and select Storage, Create Partition. In this example, I'm using AdventureWorks2012.Production.TransactionHistory.
On the Select a Partitioning Column screen as shown below you will need to select the column that you want to use to partition your table. The most common column used is a date column since most tables are being archived by date. In this example, I'll choose TransactionDate so I'll partition my table based on the date when the transaction was entered into this table.
Other options include:
Collocate this table to the selected partition table: Displays related data to join with the column being partitioned.
Storage Align Non Unique Indexes and Unique Indexes with an Indexed Partition Column: Aligns all indexes of the table being partitioned with the same partition scheme. If you do not select this option, you may place indexes independently of the columns they point to.
The next screen, Select a Partition Function, is where you will create the partition function. This will create a function that maps the rows of the table or index into partitions based on the values of the TransactionDate column. In this example, I'll name the function TransactionHistoryFunction.
The next screen, Select a Partition Scheme, is where you will create the partition scheme. This will create a scheme that maps the partitions of the Production.TransactionHistory table to different filegroups. In this example, I'll name the scheme TransactionHistoryScheme.
The next screen, Map Partitions, is where you will map your partitions.
The range and values are as follows:
Filegroup: Filegroup used for the partition.
Boundary: Used for entering range values up to a specified value. Left boundary is based on Value <= Boundary and Right boundary is based on Value < Boundary.
RowCount: Read-only columns that display required space and are determined only when the Estimate Storage button is clicked.
Required Space: Read-only columns that display required space and are determined only when the Estimate Storage button is clicked.
Available Space: Read-only columns that display available space and are determined only when the Estimate Storage button is clicked.
Estimate Storage: When selected, this option determines the rowcount, required, and available space.
If you click the Set Boundaries... button the wizard will generate boundaries for you. This is a nice feature because it's easy to change and doesn't involve any complexity.
Click OK on the Set Boundary Values screen and you will notice it separates the boundaries into 3 different rows based on years. I have already created 3 new filegroups that I will change to match each boundary.
Another cool feature of this wizard is the Estimate Storage button. If you click this button you can see the rowcount, required space, and available space as shown below.
The next screen is the Output screen. This screen allows you to generate the T-SQL, run immediately, or create a schedule to execute. In this example, I'll run immediately because we are working on a sample database rather than in production.
The final screen is the Summary screen that allows you to review your selections. Click the Finish button to partition your table.
Once the table is partitioned, you can run the following query to verify the filegroup name, partition scheme, partition function, and number of rows in each filegroup.
SELECT f.name AS FG, sps.name AS PartitionScheme, spf.name as PartitionFunction, p.rows FROM sys.partitions p JOIN sys.destination_data_spaces dds ON p.partition_number = dds.destination_id JOIN sys.partition_schemes sps ON dds.partition_scheme_id = sps.data_space_id JOIN sys.partition_functions spf ON sps.function_id = spf.function_id JOIN sys.filegroups f ON dds.data_space_id = f.data_space_id WHERE OBJECT_NAME(OBJECT_ID) = 'TransactionHistory' and index_id = 0
- To see more tips regarding partitioning click here
- Once the partition is created, you can go back to the table and select Storage, Manage Partition and this will bring up a new wizard that will allow you to create staging tables, switch out partitions, switch in partitions, and manage partitioned data in a sliding window scenario.
Last Updated: 2013-01-09
About the author
View all my tips