Insights Create a Table Partition in SQL Server 2012

Create a Table Partition in SQL Server 2012

Partitioning a SQL Table is a great way to organize your data for faster data retrieval.  Partitions do exactly that, break down the Table into organized pieces based on criteria from your columns.

Why partition?

Primarily, performance reasons.  Because the table is split up, data can be retrieved in a faster and more organized way when the query optimizer sees that partition.  WHERE clause BIT data type = 1 would search through only that partition.  This cuts back seeks and scans dramatically

When should I partition?

Partitioning sounds great but partitions are a rather large change to a table and can have implications on design and even make performance worse.  Partitioning is best used with an extremely high level of records in the data.  Before partitioning, ask yourself these questions:

  • What query hints am I using?
  • Have I optimized all of my queries?
  • How much data is in this table?
  • Am I using indexing correctly?
  • Are my statistics updated?
  • Have I done all I can to improve query performance?

I you have gone through these questions and have done all you can to increase performance, than it is probably time to create a partition.

How to Create a Partition

Before you begin, ensure that you have a File Group dedicated to each of your partitions as well as a file in each partition group.  The data needs to go somewhere so fulfill this requirement first.

  1. Open SQL Server and right click on the table you want to partition.
  2. Go to ‘Storage’.
  3. Click on ‘Create Partition’.
  4. Click ‘Next’ on the Wizard Page.
  5. Choose a column that you want to partition by.Partition-Table-2.JPG
  6. Name your new Partition Function.Partition-Table-3.JPG
  7. Name your new Partition Scheme.  If one exists, chose the Partition Scheme that your partition should be used by.
    Partition-Table-4.JPG
  8. Decide between a Left or Right boundary.  If you choose a Left boundary, your data will have a greater than or equals to condition for storing values in that partition or not.  A Right boundary will be strictly a greater than condition.  Example, Left boundary <= 32 Vacation Hours will put records with 32 hours or greater into the partition.Partition-Table-5.JPG
  9. Choose a File Group to partition to.  Ensure you have more than one File Group to partition to, 1 for your conditional boundary records and another for your non conditional records.
  10. Click ‘Next’.
  11. Choose to either script out your selections to a code, run your partitioning immediately, or schedule out the partition in the future.  Let’s choose Run immediately for now.Partition-Table-6.JPG
  12. Click ‘Next’.
  13. Click ‘Finish’.
  14. Your partition will run and you’re done!Partition-Table-7.JPG