Table Partitioning in SQL Server: A Magic Solution for Better Performance? (Pragmatic Works)

CathrineWilhelmsen 10,122 views 58 slides Mar 26, 2015
Slide 1
Slide 1 of 58
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58

About This Presentation

Recording available on Pragmatic Works' website: http://pragmaticworks.com/Training/FreeTraining/ViewWebinar/WebinarID/1743

Table Partitioning in SQL Server: A Magic Solution for Better Performance? (Presented during Pragmatic Works' Free Training on the T's on March 26th, 2015)


Slide Content

Table Partitioning in SQL Server
A Magic Solution for Better Performance?

Cathrine Wilhelmsen
@cathrinew
cathrinewilhelmsen.net
Data Warehouse Architect
Business Intelligence Developer

You?
New to table partitioning
"Everything is slow"
Today?
Basic Introduction
What, Why & How

A Magic Solution for Better Performance?
Spoiler Alert!

A Magic Solution for Better Performance?
Implementing table partitioning is not a trivial task
and can actually cause worse performance...

A Magic Solution for Better Performance?
...but don't worry, I made plenty of mistakes
so you can avoid them 

Enterprise Edition only

What?
Partition Key
Partition Function
Partition Scheme
Why?
Backup & Restore
Maintenance
Load & Archive
How?
Partition Elimination
Switch, Split & Merge
Sliding Windows

Table Partitioning Basics

What is a partitioned table?
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
Data is physically stored in groups
of rows called partitions
Each partition can be accessed
and maintained separately
Partitioning is not visible to users,
it behaves like one logical table

Partition Key
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
Data is partitioned based on a
single column, the Partition Key
The Partition Keyshould always be
used as a filter in queries
This ensures Partition Elimination:
only relevant partitions are accessed

Partition Function
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
The Partition Functiondefines how to
partition the data
It specifies boundary values, the points
between two partitions
It specifies if the boundary value
belongs to its left(upper) partition or
its right(lower) partition

Partition Function: Range Left and Range Right
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...

Partition Function: Range Left and Range Right
Range Left means the boundary value is
the last value in the left partition
CREATEPARTITIONFUNCTION
pfLeft(INT) AS RANGELEFT
FORVALUES (20,30,40);
Range Right means the boundary value
is the first value in the right partition
CREATEPARTITIONFUNCTION
pfRight(INT) AS RANGERIGHT
FORVALUES (20,30,40);
...2021-30 41...31-40 ...1920-39 40...30-39
20 30 40 20 30 40

Partition Scheme
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
The Partition Scheme maps logical
partitions to physical filegroups
Filegroups?
Data files on one or more disks
Backed up and restored individually
Can be Read-Only

Map all partitions to one filegroup
FILEGROUP
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...

Map partitions to separate filegroups
FILEGROUP1
(Read-Only)
FILEGROUP2
(Read-Only)
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
FILEGROUP3
FILEGROUP4

How are partitions mapped to filegroups?
...2021-30 41...31-40 ...1920-39 40...30-39
20 30 40 20 30 40
The partition function specified the boundary values and partitions:

How are partitions mapped to filegroups?
CREATEPARTITIONSCHEME
psLeftASPARTITIONpfLeft
TO (FG1,FG2,FG3,FG4);
CREATEPARTITIONSCHEME
psRightASPARTITIONpfRight
TO (FG1,FG2,FG3,FG4);
...2021-30 41...31-40 ...1920-39 40...30-39
20 30 40 20 30 40
The partition scheme uses the partition function...

How are partitions mapped to filegroups?
CREATEPARTITIONSCHEME
psLeftASPARTITIONpfLeft
TO (FG1,FG2,FG3,FG4);
CREATEPARTITIONSCHEME
psRightASPARTITIONpfRight
TO (FG1,FG2,FG3,FG4);
...2021-30 41...31-40 ...1920-39 40...30-39
20 30 40 20 30 40
FG1 FG2 FG4FG3 FG1 FG2 FG4FG3
...to map each partition to filegroups:

Filegroups
Partition Scheme
Partitioned Table
2012-01-01 ... ...
2012-12-31 ... ...
2013-01-01 ... ...
2013-12-31 ... ...
2014-01-01 ... ...
2014-12-31 ... ...
2015-01-01 ... ...
2015-12-31 ... ...
A partitioned table is created on
a partition scheme instead of
directly on a filegroup
The partition scheme uses the
partition key to store rows in the
correct partition and filegroup
based on the definition specified
in the partition function

Table Partitioning Basics

Why Table Partitioning?

Partition Elimination
SELECTCOUNT(*) FROMTable
WHEREYear =2012;
SELECTCOUNT(*) FROMTable;

Partition Elimination
SELECTCOUNT(*) FROMTable
WHEREYear =2012;
SELECTCOUNT(*) FROMTable;

Backup & Restore Partitions
Filegroups can be backed up
and restored individually
If each partition is mapped to
a separate filegroup, partitions
with the most critical data can
be restored first

Backup & Restore Partitions
Filegroups can be read-only
If each partition is mapped
to a separate filegroup,
partitions with historical,
unchanging data can be
excluded from regular
backups

Index Maintenance per Partition
Rebuild and reorganize indexes per
partition
Rebuild indexes onlineper partition
was introduced in SQL Server 2014
Set data compression per
partition
ALTERINDEXIndexName
ONTableName
REBUILDPARTITION=2
WITH (ONLINE=ON);

Statistics Maintenance per Partition
Update statistics on specific
partitions instead of scanning
and updating the whole table
UPDATESTATISTICS
TableName(StatisticsName)
WITHRESAMPLE
ONPARTITIONS(3,5);
CREATESTATISTICS
StatisticsNameON
TableName(ColumnName)
WITHINCREMENTAL =ON;
Incremental Statistics was
introduced in SQL Server 2014

Load & Archive: Partition Switching
Partitions can be switched between tables,
called switching inor switching out
Partition switching is a metadata operation
that updates the location of the data, no
data is physically moved
Extremely fast compared to inserting
into or deleting from a large table
2013-01-01......
2013-12-31......
2014-01-01......
2014-12-31......
2015-01-01......
2015-12-31......
2012-01-01......
2012-12-31......
ALTERTABLETable1
SWITCH PARTITION5
TOTable2 PARTITION5;
SELECT
$PARTITION.pf(2014);

Load & Archive: Switch out
2012-01-01 ... ...
2012-12-31 ... ...
2013-01-01 ... ...
2013-12-31 ... ...
2014-01-01 ... ...
2014-12-31 ... ...
2015-01-01 ... ...
2015-12-31 ... ...
Called switch out when you move data out of a table (archive)

Load & Archive: Switch out
2013-01-01 ... ...
2013-12-31 ... ...
2014-01-01 ... ...
2014-12-31 ... ...
2015-01-01 ... ...
2015-12-31 ... ...
2012-01-01 ... ...
2012-12-31 ... ...
Called switch out when you move data out of a table (archive)

Load & Archive: Switch in
2013-01-01 ... ...
2013-12-31 ... ...
2014-01-01 ... ...
2014-12-31 ... ...
2015-01-01 ... ...
2015-12-31 ... ...
2016-01-01 ... ...
2016-12-31 ... ...
Called switch inwhen you move data into a table (load)

Load & Archive: Switch in
2013-01-01 ... ...
2013-12-31 ... ...
2014-01-01 ... ...
2014-12-31 ... ...
2015-01-01 ... ...
2015-12-31 ... ...
2016-01-01 ... ...
2016-12-31 ... ...
Called switch inwhen you move data into a table (load)

Sliding Windows
The Sliding Windows technique automates data loading, data archiving
and partition management
It keeps a certain number of partitionsby continuously switching out
the oldest data and switching in new data
2013-01-01......
2013-12-31......
2014-01-01......
2014-12-31......
2012-01-01......
2012-12-31......
2015-01-01......
2015-12-31......

Sliding Windows: Split & Merge
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
Partitions are not actually added or
removed, they are splitor merged
Be careful!
Splitting and merging partitions
cancause data movement!!

Sliding Windows: Split & Merge
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
Splitone partition in two by
adding a new boundary value
ALTERPARTITIONFUNCTIONpf()
SPLITRANGE ('2013-06-01');
Data movement will occur if there is data
on both sides of the new boundary value!

Sliding Windows: Split & Merge
2012-01-01... ...
2012-12-31... ...
2013-01-01... ...
2013-12-31... ...
2014-01-01... ...
2014-12-31... ...
2015-01-01... ...
2015-12-31... ...
Mergetwo partitions to one by
removing a boundary value
ALTERPARTITIONFUNCTIONpf()
MERGERANGE ('2014-01-01');
Data movement will occur if there is data
on both sides of the old boundary value!

Sliding Windows Steps
1.Add new filegroup and file
2.Create switch out (archive) table
3.Create switch in (load) table and load it with new data
4.Splitto add new partition
5.Switch in new partition
6.Switch outold partition
7.Mergeto remove old partition
8.Delete switch out and switch in tables
9.Delete old file and filegroup

Sliding Windows Demo

Alternative Sliding Windows Steps
1.Add new filegroup and file
2.Create switch out (archive) table
3.Switch out old partition
4.Mergeto remove old partition
5.Create switch in (load) table and load it with new data
6.Splitto add new partition
7.Switch in new partition
8.Delete switch out and switch in tables
9.Delete old file and filegroup

Why Table Partitioning?

Case Study: What Went Wrong?

Background: Financial Data Warehouse
Periodic Snapshot Fact Tables
Daily, Weekly and Monthly Balances
SQL Server, DB2, Oracle, .csv, .txt
Loaded at different times during the day
1 2 3 4 5 6 7
Pension Insurance Bank Fund

First version
Dailyfact table:
Keep 1 day per source
1 2
Pension
6 7
Bank Fund
Monthlyfact table:
Keep 36 months per source
3 4 5
Insurance

Second version
Dailyfact table:
Keep 1 day per source
1 2
Pension
Monthly / Weeklyfact table:
Keep 36 months for some sources
Keep 106 weeks for some sources
3 4 5
Insurance
!
6 7
Bank Fund

Third version
Dailyfact table:
Keep 1 day per source
1 2
Pension
Monthly / Weekly / Dailyfact table:
Keep 36 months for some sources
Keep 106 weeks for some sources
Keep 7 days for some sources
3 4 5 6 7
Insurance Bank Fund
!!

"Everything is slow"

"Let's try partitioning"

Daily fact table partitioning
1 ... ...
2 ... ...
3 ... ...
4 ... ...
5 ... ...
6 ... ...
7 ... ...
Partition Key: source number
One partition per source
Helped with deadlock issues
Easy to switch data in and out

Monthly / Weekly / Daily fact table partitioning
12011 ... ...
12012 ... ...
12013 ... ...
12014 ... ...
19990 ... ...
19991 ... ...
19992 ... ...
Partition Key: Source number + Period type
Period type:
YYYY for monthly data
9991 for weekly data
9992 for daily data
Helped with deadlock issues, but…

Monthly / Weekly / Daily fact table partitioning
12011 ... ...
12012 ... ...
12013 ... ...
12014 ... ...
19990 ... ...
19991 ... ...
19992 ... ...
Partition key was difficult to remember
and was not used in queries
Data had to be inserted into and
deleted from partitions with existing
data, instead of switching partitions in
and out

What went wrong?
"The usual suspects": Conflicting priorities, changing
requirements and high pressure to deliver on time
Fact tables were expanded, notchanged, when new
sources and requirements were implemented
Partitioning was implemented on fact tables with
multiple grains, instead of correcting the fact tables first

What did we learn?

test, test, test, test

References and Resources: Table Partitioning
Brent Ozar Unlimited: SQL Server Table Partitioning Resources
brentozar.com/sql/table-partitioning-resources/
Bradley Ball: Partitioning in SQL Server 2012
pragmaticworks.com/Training/FreeTraining/ViewWebinar/WebinarID/541

References and Resources: Further Reading
Partial Backupsin SQL Server 2014
msdn.microsoft.com/en-us/library/ms191539.aspx
Piecemeal Restoresin SQL Server 2014
msdn.microsoft.com/en-us/library/ms177425.aspx
Benjamin Nevarez: SQL Server 2014 Incremental Statistics
benjaminnevarez.com/2015/02/2014-incremental-statistics/

Thank you!
@cathrinew
cathrinewilhelmsen.net
no.linkedin.com/in/cathrinewilhelmsen
[email protected]