Microsoft SQL Server Administration - Univerzita...
Transcript of Microsoft SQL Server Administration - Univerzita...
Microsoft SQL Server Administration
Partitioning
David Hokszahttp://siret.cz/hoksza
Outline• Partitioned Functions
• Partitioned Schemes
• Partitioned Tables
• Moving Data between Partitions
Partitioning Overview• Partitioning helps to manage large data volumes by
splitting data into smaller parts
o more efficient queries or modification operations (such as archiving)
o filegroups
• reasonable distribution of tables into different files
o vertical partitioning
• column-based table splitting
• less used columns can be moved to another table → manipulating less data (the same goal can be achieved by effective use of SQL querying)
o horizontal partitioning
• row-based table splitting
• partitioned views, partitioned tables and indexes (>2005, enterprise)
Horizontal Partitioning Motivation
• Faster join operation on tables distributed into
partitions based on the same criteria
• Partition-level lock operations (lock escalation)
• Administration operations can be carried out on
some partitions only (archiving, compression, …)
Horizontal Partitioning Overview
1. Build “multi-filegroup” databaseo pre-step
o can be skipped but then the advantage of partitioning decreases
2. Create partition function
3. Create partition schema
4. Create partitioned table
• Partition columno column according to which the data are partitioned
o single column (or computed)
o can have any indexable data type
Create DatabaseCREATE DATABASE test_partition_dbON PRIMARY
(NAME='test_partition_db1',
FILENAME='d:\MSSQLData\MSSQL11.NDBI039\MSSQL\Data\test_partition_db1.mdf',
SIZE = 4,MAXSIZE = 10,FILEGROWTH = 1 ),
FILEGROUP FG2(NAME = ' test_partition_db2',
FILENAME='d:\MSSQLData\MSSQL11.NDBI039\MSSQL\Data\test_partition_db2.mdf',
SIZE = 3,MAXSIZE=100,FILEGROWTH=1 ),
FILEGROUP FG3(NAME = ' test_partition_db3',
FILENAME='d:\MSSQLData\MSSQL11.NDBI039\MSSQL\Data\test_partition_db3.mdf',
SIZE = 3,MAXSIZE=100,FILEGROWTH=1 )
USE test_partition_db
GO
SELECT
name
FROM
sys.filegroups
WHERE
type = 'FG'
SELECT
name, physical_name
FROM
sys.database_files
WHERE
type_desc = 'ROWS'
Partitioning Function• Function specifying how the data are partitioned
CREATE PARTITION FUNCTION
year_partition(DATETIME)AS
RANGE RIGHT
FOR VALUES ('2010-01-01', '2011-01-01')
• RIGHT|LEFTo where the boundary value fallso RIGHT = 2010-01-01 goes to the second partitiono LEFT = 2010-01-01 goes to the first partition
ALTER PARTITION FUNCTION function() {SPLIT RANGE( boundary_value) | MERGE RANGE( boundary_value)}
Partitioning Scheme• mapping partition function onto a physical scheme
CREATE PARTITION SCHEME test_partition_scheme
AS PARTITION year_partition
TO ('PRIMARY', 'FG2', 'FG3')
• for testing purposes instead of TO (…)o ALL TO (PRIMARY)
ALTER PARTITION SCHEME scheme NEXT USED FG
Partitioned Table• Data in a table created on a partition scheme will be
transparently stored in partitions based on the partition function
CREATE TABLE test_partition_table(purchaseDate DATETIME, productNameVARCHAR(50))
ON test_partition_scheme(purchaseDate)
INSERT INTO test_partition_table(purchaseDate, productName)VALUES ('2007-03-25', 'prod1'), ('2009-10-01','prod 2'), ('2010-01-01','prod1'), ('2010-08-09', 'prod3'), ('2010-12-30', 'prod2'), ('2011-01-01', 'prod1'), ('2011-05-24','prod3')
Querying Partitions Metadata
SELECT * FROM sys.partitions
WHERE OBJECT_NAME(object_id)=
'test_partition_table'
SELECT $partition.year_partition('2014-01-
01')
SELECT * FROM test_partition_table
WHERE
$partition.year_partition(purchaseDate) = 2
Partitioning Metadata
credit: SQL Server 2008 Internals
sys.system_internals_allocation_units
sys.partition_range_values
Metadata-based Data Shifting
• metadata-basedo no data shifting, only metadata information is changed
o automatic sliding window technique
• ALTER TABLE → SWITCHo assign a table as a partition to an already existing partitioned table
o switch a partition from one partitioned table to another
o reassign a partition to form a single table
Tasks
1. Create a table for history of played songs in a radio
broadcasting. Partition the table so that each
month will be on a separate partition. Implement a
stored procedure which adds a partition on the
right side automatically (the new boundary will be
based on the data dictionary information) – store
all partitions on PRIMARY FG. Verify the existence of
the new partition.