Select Page

Four Things To Remember While Planning SQL Partitioning

Author: Pinal Dave | | March 23, 2015

Before covering what to remember, let’s cover the basics. What is table partitioning in SQL Server? In SQL Server a partitioned table is one that is referred to as one table but is subdivided into multiple partitions in which data is partitioned horizontally. A partitioned table has the following properties:

  • Partition Scheme: This defines which file group the partitions’ data will be stored.
  • Partition Functions: This defines how the data is partitioned (think of it as a WHERE clause, which segregates data into separate partitions.) This ensures that a group of rows are mapped together into partitions.

SQL Partition Scheme

The following is an example of testTable which is partitioned based on tranMonth:

CREATE PARTITION FUNCTION monthRangeFunction (int)

AS RANGE LEFT FOR VALUES (2, 4, 6, 8, 10, 12)

go

CREATE PARTITION SCHEME monthRangeScheme

AS PARTITION monthRangeFunction

TO (FG1, FG2, FG3, FG4, FG5, FG6, FG7)

go

When executing the above query, the data is partitioned based on the values specified for month. Here is an example of how the data is partitioned:

More details about the CREATE PARTITION SCHEME and CREATE PARTITION FUNCTION can be found on Microsoft’s Developer Network site here and here.

SQL Query Table

Benefits Of Partitioning

  • Speed: Data transfer is relatively fast in a partition compared to a table which is not partitioned.
  • Query performance: If you have queries which deal with a specific set of data, (e.g. current month) then partitioning the table will improve the performance of these types of queries.
  • Easy maintenance: Data in a partition can be easily SWITCHed , SPLIT, MERGEd as explained in these two articles.
  • Transferring Data Efficiently by Using Partition Switching
  • Alter Partition Function (Transact-SQL)

Also, you can chose to rebuild a partition of an index instead of rebuilding the entire partition.

Things To Remember

So far we’ve shown that partitioning is a great feature; but it does not fix all performance problems. Here are the few things to keep in mind while working with SQL Server partitioning:

As explained above, SWITCHING partitions is an easy approach to transfer data efficiently. But keep in mind:

  • The destination table. The partition must be created before SWTICHING the partition and it should be empty.
  • The destination and source partitions should reside on the same FILEGROUP.
  • If the partition has an identity column there is a possibility that the destination partition may have duplicate values or gaps in the identity column.
  • Both the source and destination partitions should have the same clustered and non-clustered indexes.
  • During the partition switch, a Schema Modification lock held on the table can cause a block for the users accessing the table.
  • An index is said to be aligned if the index and the table on which it is created both use the same partitioning function and columns in the same order. If there a large number of partitions defined, it is recommended to have more memory on the server. Otherwise there is a likelihood that the partitioned index may fail especially in the case of non-aligned indexes.

This is because when building the partition, SQL Server will create a sort table for each partition in the corresponding filegroup or in tempdb if the sort_in_tempdb option is specified. This requires a certain amount of memory. If the index is aligned, then the sort tables are created one at a time, thereby using small chunks of memory one at a time. However if the index is non-aligned, then the sort tables are created all at once. This drastically increases the amount of memory required for this operation.

  • If you are upgrading a database with partitions to SQL Server 2012 or above, you may notice a change in the histogram for the indexes. This is because starting SQL Server 2012, statistics are created by using the default sampling instead of scanning all the rows.
  • Use the TOP/MAX/MIN operators with care because for any queries using these operators, all the partitions must be evaluated. If the table is huge then such queries will perform slowly as it has to evaluate all the partitions before getting the desired result.

Over all, you need to evaluate partitioning based on the data in the table. Using partitioning just because it is a cool feature can have adverse effects in an OLTP environment. Partitioning is a right choice where you have a huge table to deal with as seen in Data warehousing environments.

You can read Pinal Dave’s other blogs here that cover more SQL topics. For a broader scope of database solutions, check out Datavail’s frequently updated Resources section.

How to Solve the Oracle Error ORA-12154: TNS:could not resolve the connect identifier specified

The “ORA-12154: TNS Oracle error message is very common for database administrators. Learn how to diagnose & resolve this common issue here today.

Vijay Muthu | February 4, 2021

Data Types: The Importance of Choosing the Correct Data Type

Most DBAs have struggled with the pros and cons of choosing one data type over another. This blog post discusses different situations.

Craig Mullins | October 11, 2017

How to Recover a Table from an Oracle 12c RMAN Backup

Our database experts explain how to recover and restore a table from an Oracle 12c RMAN Backup with this step-by-step blog. Read more.

Megan Elphingstone | February 2, 2017

Subscribe to Our Blog

Never miss a post! Stay up to date with the latest database, application and analytics tips and news. Delivered in a handy bi-weekly update straight to your inbox. You can unsubscribe at any time.

Work with Us

Let’s have a conversation about what you need to succeed and how we can help get you there.

CONTACT US

Work for Us

Where do you want to take your career? Explore exciting opportunities to join our team.

EXPLORE JOBS