A step-by-step guide on how to convert an existing large database table in SQL Server into a partitioned table

Step 1: Create a Large Sample Table 12 Let’s start by creating a large sample table with 10 million records. We’ll use the GetNums function to generate the test data:

sql

-- Drop the table if it already exists
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'LargeTable')
  DROP TABLE LargeTable;

-- Create the non-partitioned table
CREATE TABLE LargeTable (
  ID INT IDENTITY(1,1) PRIMARY KEY,
  SalesDate DATE,
  Quantity INT
) ON [PRIMARY];

-- Insert 10 million rows of test data
INSERT INTO LargeTable(SalesDate, Quantity)
SELECT DATEADD(DAY,dates.n-1,'2020-01-01') AS SalesDate, qty.n AS Quantity
FROM GetNums(DATEDIFF(DD,'2020-01-01','2021-01-01')) dates
CROSS JOIN GetNums(1000) AS qty;

This creates a table named LargeTable with 10 million rows of test data.

Step 2: Measure Performance of the Non-Partitioned Table 9 Before we partition the table, let’s measure the performance of some common queries on the non-partitioned table. We’ll use the SET STATISTICS TIME ON command to capture the execution time.

sql

SET STATISTICS TIME ON;

-- Query the entire table
SELECT COUNT(*) FROM LargeTable;

-- Query a specific date range
SELECT COUNT(*) FROM LargeTable WHERE SalesDate BETWEEN '2020-01-01' AND '2020-01-31';

SET STATISTICS TIME OFF;

Note the execution times for these queries. We’ll compare them to the partitioned table later.

Step 3: Create the Partition Function and Partition Scheme 12 Next, we’ll create a partition function and partition scheme to define how the table will be partitioned. In this example, we’ll partition the table by month:

sql

-- Create the partition function
CREATE PARTITION FUNCTION pfSales(DATE)
AS RANGE RIGHT FOR VALUES 
('2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01',
 '2020-07-01', '2020-08-01', '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01');

-- Create the partition scheme
CREATE PARTITION SCHEME psSales
AS PARTITION pfSales ALL TO ([PRIMARY]);

This will create 12 partitions, one for each month of the year 2020.

Step 4: Convert the Table to a Partitioned Table 12 Now, we can convert the LargeTable to a partitioned table using the partition scheme we just created:

sql

-- Convert the table to a partitioned table
ALTER TABLE LargeTable REBUILD PARTITION = ALL
ON psSales(SalesDate);

This will convert the LargeTable to a partitioned table using the psSales partition scheme.

Step 5: Measure Performance of the Partitioned Table 9 Let’s run the same queries as before and compare the execution times:

sql

SET STATISTICS TIME ON;

-- Query the entire table
SELECT COUNT(*) FROM LargeTable;

-- Query a specific date range
SELECT COUNT(*) FROM LargeTable WHERE SalesDate BETWEEN '2020-01-01' AND '2020-01-31';

SET STATISTICS TIME OFF;

Compare the execution times for the non-partitioned and partitioned tables. You should see a significant improvement in performance for the partitioned table, especially for queries that target a specific date range.

Step 6: Analyze the Cost and Benefits 10 Partitioning the table can provide several benefits, such as:

  • Improved query performance, especially for date range queries
  • Easier data management and maintenance (e.g., partition switching for archiving old data)
  • Reduced storage costs by allowing you to store older data on cheaper storage

To quantify the benefits, you can measure the following:

  • Query execution times (as we did above)
  • Storage space used by the partitioned and non-partitioned tables
  • Time and effort required for maintenance tasks (e.g., archiving old data)

The specific cost savings and performance improvements will depend on the size and usage patterns of your database, but in general, partitioning a large table can significantly improve the overall performance and manageability of your SQL Server environment.

In summary, this step-by-step guide demonstrates how to convert an existing large database table in SQL Server into a partitioned table, including the creation of a sample table, measuring performance before and after partitioning, and analyzing the cost and benefits. By following these steps, you can effectively partition your large tables and optimize the performance and maintenance of your SQL Server database.