Introduction to Partitioning in SQL Server Remember Legos? It’s kind of like that...
A Little About Me @SQLServerNerd Event Chair Forums Administrator
Have you ever wanted to…/secrets of heavy hitter servers Truncate just part of a table Migrate data from one table into another in milliseconds Spread your table over different tiers of storage. Perform maintenance on just part of a table Access just part of a table/index for less IO Have locks escalate to just a segment of a table instead of a table lock
What is partitioning? A way to break tables down into smaller chunks for manageability and performance
You mean it will help me to… Reduce IO impact via partition elimination Improve join operations for equi-joins when both tables are partitioned on the same column and joined on the partitioned column Move data in and out of tables really fast
Horizontal partitioning VS vertical partitioning Vertical partitioning is splitting out extra columns into their own table(s) Horizontal partitioning is when we break up our table based on rows This is what we traditionally mean when using the word partition This is what we will talk about today
What techniques will we cover. We’ll start with the much simpler partitioned view We’ll spend a lot more time on table partitioning.
An Oldie but a goodie: The partitioned view Separate tables with a view that brings them together Check constraints on the underlying tables allow the optimizer to perform “partition elimination”
Demo: Partitioned views
Table Partitioning Overview Introduced in SQL Server 2005 Its and enterprise only feature Invisible to queries More complex than partitioned views
How is it implemented in SQL Server Every table has at least one partition These partitions exist in files The files are part of a filegroup Each partition is like a mini table, with its own IAM chain. In other words it’s a separate “object” in the database It’s the combination of all partitions that form the actual table
How is it implemented in SQL Server Partitions also allow another level of locking called a partition lock. This can be amazingly beneficial
Data Files These are the actual files on disk with an mdf or ndf extension All objects in a database are stored in one or more files If multiple files exist within a file group then data is spread across them with a round robin algorithm, with preference given to the smallest file
Filegroups Provide a “Container” to group data files into Objects in the database are created against a filegroup
Partition Functions Are used to assign rows to partitions Can only work on one column known as the partition key Range left and Range right determine where the boundry value falls
Partition Schemes Are used to place partitions into one or more filegroups They specify the destination of the data ALL TO allows you to have all partitions exist in one filegroup Partitioned tables are built not on a single filegroup but on a partition scheme
Aligned Indexes Indexes can be partitioned as well If an index is created on the same partition scheme as its parent table, it is said to be aligned. This allows for maintenance on just one part of the index
Demo: Example partitioned table/files/filegroups Creating a partitioned table Verifying our partitions: sys.dm_db_partition_stats/sys.partitions
Partition management Overview We can move chunks of data in and out of tables really fast using partitions as long as the data is all in the same filegroup We can switch data in, or out, from another table in milliseconds regardless of size. This is possible because the switch is really only a metadata one. The tables must have the same design however We can merge partitions together, or split them apart, during our loads. Again this is a metadata operation
Splitting partitions up We can split partitions up using the split command The split command is part of the alter partition function statement We split by defining a new range
Merging partitions together. The merge command allows you to combine partitions into one partition This is done by merging our ranges It is part of the alter partition function statement
Moving partitions between tables We can use the switch command to move partitions between tables Indexes must be identical on both tables The tables must also share the same constraints The tables must be part of the same filegroup Both tables must exist already Transfer into a partitioned table, requires that the destination partition exist and be empty Non partitioned tables must be empty
DEMO: Switch