Sie sind auf Seite 1von 4

USE master;

GO
IF db_id('MegaCorpData') IS NOT NULL DROP DATABASE MegaCorpData;
GO
CREATE DATABASE MegaCorpData
ON PRIMARY
(NAME = 'MegaCorpData',
FILENAME = 'C:\Apress\MegaCorpData.MDF',
SIZE = 5MB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1MB)
LOG ON
(NAME = 'MegaCorpData_Log',
FILENAME = 'C:\Apress\MegaCorpData.LDF',
SIZE = 3MB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1MB);
GO
ALTER
ALTER
ALTER
ALTER

DATABASE
DATABASE
DATABASE
DATABASE

MegaCorpData
MegaCorpData
MegaCorpData
MegaCorpData

ADD
ADD
ADD
ADD

FILEGROUP
FILEGROUP
FILEGROUP
FILEGROUP

hitfg1;
hitfg2;
hitfg3;
hitfg4;

-- add file to FG
ALTER DATABASE MegaCorpData
ADD FILE (NAME = mchitfg1,
FILENAME = 'C:\Apress\mc_hitfg1.ndf',
SIZE = 1MB)
TO FILEGROUP hitfg1;
ALTER DATABASE MegaCorpData
ADD FILE (NAME = mchitfg2,
FILENAME = 'C:\Apress\mc_hitfg2.ndf',
SIZE = 1MB)
TO FILEGROUP hitfg2;
ALTER DATABASE MegaCorpData
ADD FILE (NAME = mchitfg3,
FILENAME = 'C:\Apress\mc_hitfg3.ndf',
SIZE = 1MB)
TO FILEGROUP hitfg3;
ALTER DATABASE MegaCorpData
ADD FILE (NAME = mchitfg4,
FILENAME = 'C:\Apress\mc_hitfg4.ndf',
SIZE = 1MB)
TO FILEGROUP hitfg4;
ALTER DATABASE MegaCorpData
ADD FILE (NAME = mchitfg5,
FILENAME = 'C:\Apress\mc_hitfg.ndf', -- file has both physical & logical,file wi
ll be saved;
SIZE = 1MB)
TO FILEGROUP hitfg4;

--Now that we have filegroups with files ready to receive data, we need to creat
e a partition function, which
--determines how the table will have its data horizontally partitioned by mappin
g columns to partitions based
--upon the value of a specified column
USE MegaCorpData;
GO
CREATE PARTITION FUNCTION HitsDateRange (datetime)
AS RANGE LEFT FOR VALUES ('2006-01-01T00:00:00', '2007-01-01T00:00:00', '2008-01
-01T00:00:00');
CREATE PARTITION SCHEME HitDateRangeScheme --- partion scheme created for the pa
rtition function
AS PARTITION HitsDateRange
-- partition function is called
TO (hitfg1, hitfg2, hitfg3, hitfg4);
-- assigne to the FG
--create partion function
--create partion scheme with PF to assign FG to distriibute data
CREATE TABLE dbo.WebSiteHits (
WebSiteHitID BIGINT NOT NULL IDENTITY(1, 1),
WebSitePage VARCHAR(255) NOT NULL,
HitDate DATETIME NOT NULL,
CONSTRAINT PK_WebSiteHits PRIMARY KEY CLUSTERED (WebSiteHitId, HitDate)
)
ON [HitDateRangeScheme] (HitDate);
-------15-2. Locating Data in a Partition
--Utilize the $PARTITION function to return the partition that a row is stored i
n.
INSERT dbo.WebSiteHits
(WebSitePage, HitDate)
VALUES
('Home Page', '2007-10-22T00:00:00'),
('Home Page', '2006-10-02T00:00:00'),
('Sales Page', '2008-05-09T00:00:00'),
('Sales Page', '2000-03-04T00:00:00');
--1.we have created partion for the column,HitDate to different FG's.
--2.Data for the particular column get sorted if we dont give value in orer,whil
e adding data
--3.while inserting values,table accept values from left to right thought it acc
ept identity
--If the values are not specified in order, the database engine sorts the values
, creates the function, and returns a warning
--that the values were not provided in order.
--The number of values that you choose amounts to a total of n + 1 partitions. Y
ou can have up to 15,000 partitions,
--so you can specify up to 14,999 boundaries.

--If there are any duplicate values, the database engine returns an error.
--The first partition contains values less than the lowest specified value, and
the last partition contains values
--higher than the highest specified value.
select * from WebSiteHits
SELECT WebSitePage,
HitDate,
$PARTITION.HitsDateRange (HitDate) AS [Partition] ---$PARTITION.partition_functi
on_name(expression)
FROM dbo.WebSiteHits;
-------------15-3. Adding a Partition
--ALTER PARTITION SCHEME and ALTER PARTITION FUNCTION statements to extend the p
artition onto a
--new or existing filegroup and to create the new partition.
--ALTER PARTITION SCHEME partition_scheme_name NEXT USED [ filegroup_name ]
ALTER PARTITION SCHEME HitDateRangeScheme NEXT USED [PRIMARY];
go

--ALTER PARTITION FUNCTION partition_function_name() {SPLIT RANGE ( boundary_val


ue ) | MERGE RANGE ( boundary_value ) }
ALTER PARTITION FUNCTION HitsDateRange () SPLIT RANGE ('2009-01-01T00:00:00');
GO
INSERT dbo.WebSiteHits
(WebSitePage, HitDate)
VALUES ('Sales Page', '2009-03-04T00:00:00');
SELECT WebSitePage, HitDate,
$PARTITION.HitsDateRange (HitDate) AS [Partition]
FROM dbo.WebSiteHits;
SPLIT RANGE is used to create a new partition by defining a new boundary value;
MERGE RANGE is used to remove an existing partition at the specified boundary an
d to move any existing records to another partition.
15-4. Removing a Partition
Utilize the ALTER PARTITION FUNCTION statement to remove a partition and merge t
he data in that partition into
another partition.
15-5. Determining Whether a Table Is Partitioned

SELECT p.partition_id,
p.object_id,
p.partition_number
FROM sys.partitions AS p
WHERE p.partition_id IS NOT NULL
AND p.object_id = OBJECT_ID('dbo.WebSiteHits')
ALTER PARTITION FUNCTION HitsDateRange () MERGE RANGE ('2007-01-01T00:00:00');

Das könnte Ihnen auch gefallen