table partitioning in sql server: a magic solution for better performance? (pragmatic works)

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

Upload: cathrine-wilhelmsen

Post on 17-Jul-2015

854 views

Category:

Data & Analytics


1 download

TRANSCRIPT

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

Table Partitioning in SQL ServerA Magic Solution for Better Performance?

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

Cathrine Wilhelmsen

@cathrinew

cathrinewilhelmsen.netData Warehouse Architect

Business Intelligence Developer

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

You?New to table partitioning

"Everything is slow"

Today?Basic Introduction

What, Why & How

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

A Magic Solution for Better Performance?

Spoiler Alert!

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

A Magic Solution for Better Performance?

Implementing table partitioning is not a trivial task

and can actually cause worse performance...

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

A Magic Solution for Better Performance?

...but don't worry, I made plenty of mistakes

so you can avoid them

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

Enterprise Edition only

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

What?Partition Key

Partition Function

Partition SchemeWhy?

Backup & Restore

Maintenance

Load & ArchiveHow?

Partition Elimination

Switch, Split & Merge

Sliding Windows

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

Table Partitioning Basics

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

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

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

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 Key should always be used as a filter in queries

This ensures Partition Elimination:only relevant partitions are accessed

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

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 Function defines 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

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

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 ... ...

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

Partition Function: Range Left and Range Right

Range Left means the boundary value is the last value in the left partition

CREATE PARTITION FUNCTIONpfLeft (INT) AS RANGE LEFTFOR VALUES (20,30,40);

Range Right means the boundary value is the first value in the right partition

CREATE PARTITION FUNCTIONpfRight (INT) AS RANGE RIGHT

FOR VALUES (20,30,40);

...20 21-30 41...31-40 ...19 20-39 40...30-39

20 30 40 20 30 40

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

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

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

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 ... ...

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

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

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

How are partitions mapped to filegroups?

...20 21-30 41...31-40 ...19 20-39 40...30-39

20 30 40 20 30 40

The partition function specified the boundary values and partitions:

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

How are partitions mapped to filegroups?

CREATE PARTITION SCHEMEpsLeft AS PARTITION pfLeftTO (FG1, FG2, FG3, FG4);

CREATE PARTITION SCHEMEpsRight AS PARTITION pfRight

TO (FG1, FG2, FG3, FG4);

...20 21-30 41...31-40 ...19 20-39 40...30-39

20 30 40 20 30 40

The partition scheme uses the partition function...

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

How are partitions mapped to filegroups?

CREATE PARTITION SCHEMEpsLeft AS PARTITION pfLeftTO (FG1, FG2, FG3, FG4);

CREATE PARTITION SCHEMEpsRight AS PARTITION pfRight

TO (FG1, FG2, FG3, FG4);

...20 21-30 41...31-40 ...19 20-39 40...30-39

20 30 40 20 30 40

FG1 FG2 FG4FG3 FG1 FG2 FG4FG3

...to map each partition to filegroups:

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

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

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

Table Partitioning Basics

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

Why Table Partitioning?

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

Partition Elimination

SELECT COUNT(*) FROM TableWHERE Year = 2012;

SELECT COUNT(*) FROM Table;

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

Partition Elimination

SELECT COUNT(*) FROM TableWHERE Year = 2012;

SELECT COUNT(*) FROM Table;

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

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

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

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

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

Index Maintenance per Partition

Rebuild and reorganize indexes per partition

Rebuild indexes online per partition was introduced in SQL Server 2014

Set data compression per partition

ALTER INDEX IndexNameON TableNameREBUILD PARTITION = 2WITH (ONLINE = ON);

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

Statistics Maintenance per Partition

Update statistics on specific partitions instead of scanning and updating the whole table

UPDATE STATISTICSTableName (StatisticsName)WITH RESAMPLEON PARTITIONS (3,5);

CREATE STATISTICSStatisticsName ONTableName (ColumnName)WITH INCREMENTAL = ON;

Incremental Statistics was introduced in SQL Server 2014

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

Load & Archive: Partition Switching

Partitions can be switched between tables, called switching in or 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 ... ...

ALTER TABLE Table1 SWITCH PARTITION 5

TO Table2 PARTITION 5;

SELECT$PARTITION.pf(2014);

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

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)

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

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)

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

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 in when you move data into a table (load)

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

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 in when you move data into a table (load)

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

Sliding Windows

The Sliding Windows technique automates data loading, data archiving and partition management

It keeps a certain number of partitions by 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 ... ...

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

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 split or merged

Be careful!Splitting and merging partitionscan cause data movement!!

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

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 ... ...

Split one partition in two by adding a new boundary value

ALTER PARTITION FUNCTION pf ()

SPLIT RANGE ('2013-06-01');

Data movement will occur if there is data on both sides of the new boundary value!

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

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 ... ...

Merge two partitions to one by removing a boundary value

ALTER PARTITION FUNCTION pf ()

MERGE RANGE ('2014-01-01');

Data movement will occur if there is data on both sides of the old boundary value!

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

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. Split to add new partition

5. Switch in new partition

6. Switch out old partition

7. Merge to remove old partition

8. Delete switch out and switch in tables

9. Delete old file and filegroup

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

Sliding Windows Demo

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

Alternative Sliding Windows Steps

1. Add new filegroup and file

2. Create switch out (archive) table

3. Switch out old partition

4. Merge to remove old partition

5. Create switch in (load) table and load it with new data

6. Split to add new partition

7. Switch in new partition

8. Delete switch out and switch in tables

9. Delete old file and filegroup

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

Why Table Partitioning?

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

Case Study: What Went Wrong?

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

Background: Financial Data Warehouse

Periodic Snapshot Fact TablesDaily, Weekly and Monthly Balances

SQL Server, DB2, Oracle, .csv, .txtLoaded at different times during the day

1 2 3 4 5 6 7

Pension Insurance Bank Fund

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

First version

Daily fact table:Keep 1 day per source

1 2

Pension

6 7

Bank Fund

Monthly fact table: Keep 36 months per source

3 4 5

Insurance

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

Second version

Daily fact table:Keep 1 day per source

1 2

Pension

Monthly / Weekly fact table:Keep 36 months for some sourcesKeep 106 weeks for some sources

3 4 5

Insurance

!

6 7

Bank Fund

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

Third version

Daily fact table:Keep 1 day per source

1 2

Pension

Monthly / Weekly / Daily fact table:Keep 36 months for some sourcesKeep 106 weeks for some sources

Keep 7 days for some sources

3 4 5 6 7

Insurance Bank Fund

!!

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

"Everything is slow"

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

"Let's try partitioning"

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

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

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

Monthly / Weekly / Daily fact table partitioning

12011 ... ...

12012 ... ...

12013 ... ...

12014 ... ...

19990 ... ...

19991 ... ...

19992 ... ...

Partition Key: Source number + Period type

Period type:YYYY for monthly data9991 for weekly data9992 for daily data

Helped with deadlock issues, but…

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

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

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

What went wrong?

"The usual suspects": Conflicting priorities, changing requirements and high pressure to deliver on time

Fact tables were expanded, not changed, when new sources and requirements were implemented

Partitioning was implemented on fact tables with multiple grains, instead of correcting the fact tables first

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

What did we learn?

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

test, test, test, test

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

References and Resources: Table Partitioning

Brent Ozar Unlimited: SQL Server Table Partitioning Resourcesbrentozar.com/sql/table-partitioning-resources/

Bradley Ball: Partitioning in SQL Server 2012pragmaticworks.com/Training/FreeTraining/ViewWebinar/WebinarID/541

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

References and Resources: Further Reading

Partial Backups in SQL Server 2014msdn.microsoft.com/en-us/library/ms191539.aspx

Piecemeal Restores in SQL Server 2014msdn.microsoft.com/en-us/library/ms177425.aspx

Benjamin Nevarez: SQL Server 2014 Incremental Statisticsbenjaminnevarez.com/2015/02/2014-incremental-statistics/

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

Thank you!

@cathrinew

cathrinewilhelmsen.net

no.linkedin.com/in/cathrinewilhelmsen

[email protected]