C3 - Code Table Partition - 04 - 10 - 2023

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PARTITION FUNCTION

SQL CODE FOR CREATE TABLE WITH PARTITION

-- Create a partition function that partitions by a date range

CREATE PARTITION FUNCTION DateRangePartitionFunction (DATE)

AS RANGE LEFT FOR VALUES ('2023-01-01', '2023-02-01', '2023-03-01');

-- Create a partitioned table and associate it with the partition function

CREATE TABLE Sales (

SalesID INT,

SaleDate DATE,

Amount DECIMAL(10, 2)

) ON DateRangePartitionFunction(SaleDate);

PARTITION SCHEME
CREATE PARTITION FUNCTION myRangePF1 (INT)

AS RANGE LEFT FOR VALUES (1, 100, 1000);

GO

CREATE PARTITION SCHEME myRangePS1

AS PARTITION myRangePF1

TO (test1fg, test2fg, test3fg, test4fg);

IMPLEMENTING PARTITIONING
1. Create the partition function
CREATE PARTITION FUNCTION PartFunc(Date)

AS RANGE LEFT

FOR VALUES('2017-01-01', '2019-01-01') ;

2. Create the partition scheme

CREATE PARTITION SCHEME PartScheme

AS PARTITION PartFunc

ALL TO ([PRIMARY]) ;

3. Creating a New Partitioned Table

CREATE TABLE dbo.Orders

OrderNumber int NOT NULL,

OrderDate date NOT NULL,

CustomerID int NOT NULL,

ProductID int NOT NULL,

Quantity int NOT NULL,

NetAmount money NOT NULL,

TaxAmount money NOT NULL,

InvoiceAddressID int NOT NULL,

DeliveryAddressID int NOT NULL,

DeliveryDate date NULL

ON PartScheme(OrderDate) ;
ALTER TABLE dbo.Orders ADD CONSTRAINT

PK_Orders PRIMARY KEY CLUSTERED

(OrderNumber,

OrderDate)

WITH(

STATISTICS_NORECOMPUTE = OFF,

IGNORE_DUP_KEY = OFF,

ALLOW_ROW_LOCKS = ON,

ALLOW_PAGE_LOCKS = ON)

ON PartScheme(OrderDate) ;

4. Partitioning an Existing Table

--Drop Clustered Index

ALTER TABLE dbo.ExistingOrders DROP CONSTRAINT

PK_ExistingOrders ;

--Re-created clustered index on PartScheme

ALTER TABLE dbo.ExistingOrders ADD CONSTRAINT

PK_ExistingOrders PRIMARY KEY CLUSTERED

(OrderNumber ASC,

OrderDate ASC)

WITH (

PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,

SORT_IN_TEMPDB = OFF,

IGNORE_DUP_KEY = OFF,

ONLINE = OFF,

ALLOW_ROW_LOCKS = ON,

ALLOW_PAGE_LOCKS = ON)

ON PartScheme(OrderDate) ;

5. Monitoring Partitioned Tables

SELECT

COUNT(*) 'Number of Rows',

$PARTITION.PartFunc(OrderDate) 'Partition'

FROM dbo.ExistingOrders

GROUP BY $PARTITION.PartFunc(OrderDate) ;

--Create new partition function

CREATE PARTITION FUNCTION PartFuncWeek(DATE)

AS RANGE LEFT

FOR VALUES ('2019-03-7','2019-03-14','2019-03-21','2019-03-28') ;

--Assess spread of rows:

COUNT(*)

'Number of Rows',

$PARTITION.PartFuncWeek(OrderDate) 'Partition'
FROM dbo.ExistingOrders

GROUP BY $PARTITION.PartFuncWeek(OrderDate) ;

IMPLEMENTING COMPRESSION – SYSTEM STORED


PROCEDURE
EXEC sp_estimate_data_compression_savings @schema_name = 'Warehouse',

@object_name = 'StockItemHoldings',

@index_id = NULL,

@partition_number = NULL,

@data_compression = 'ROW';

IMPLEMENTING COMPRESSION – PARTITIONING AND


TABLE COMPRESSION
--Implementing Row Compression on the Entire Table

ALTER TABLE ExistingOrders REBUILD WITH (DATA_COMPRESSION = ROW) ;

--Implementing Row Compression for Specific Partitions

ALTER TABLE ExistingOrders REBUILD PARTITION = 1 WITH


(DATA_COMPRESSION = ROW) ;

--Remove compression from the whole table ALTER TABLE ExistingOrders REBUILD
WITH (DATA_COMPRESSION = NONE) ;

You might also like