15.partiton

Embed Size (px)

Citation preview

  • 8/18/2019 15.partiton

    1/4

    USE master;GOIF db_id('MegaCorpData') IS NOT NULL DROP DATABASE MegaCorpData;GO

    CREATE DATABASE MegaCorpDataON 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 DATABASE MegaCorpData ADD FILEGROUP hitfg1;

    ALTER DATABASE MegaCorpData ADD FILEGROUP hitfg2;ALTER DATABASE MegaCorpData ADD FILEGROUP hitfg3;ALTER DATABASE MegaCorpData ADD FILEGROUP hitfg4;

    -- add file to FG

    ALTER DATABASE MegaCorpDataADD 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 MegaCorpDataADD FILE (NAME = mchitfg3,FILENAME = 'C:\Apress\mc_hitfg3.ndf',SIZE = 1MB)TO FILEGROUP hitfg3;

    ALTER DATABASE MegaCorpDataADD FILE (NAME = mchitfg4,

    FILENAME = 'C:\Apress\mc_hitfg4.ndf',SIZE = 1MB)TO FILEGROUP hitfg4;

    ALTER DATABASE MegaCorpDataADD FILE (NAME = mchitfg5,FILENAME = 'C:\Apress\mc_hitfg.ndf', -- file has both physical & logical,file will be saved;SIZE = 1MB)TO FILEGROUP hitfg4;

  • 8/18/2019 15.partiton

    2/4

    --Now that we have filegroups with files ready to receive data, we need to create a partition function, which--determines how the table will have its data horizontally partitioned by mapping columns to partitions based--upon the value of a specified column

    USE MegaCorpData;GOCREATE 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 partition functionAS PARTITION HitsDateRange -- partition function is calledTO (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 accept 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. You can have up to 15,000 partitions,--so you can specify up to 14,999 boundaries.

  • 8/18/2019 15.partiton

    3/4

    --If there are any duplicate values, the database engine returns an error.--The first partition contains values less than the lowest specified value, andthe last partition contains values--higher than the highest specified value.

    select * from WebSiteHits

    SELECT WebSitePage,HitDate,$PARTITION.HitsDateRange (HitDate) AS [Partition] ---$PARTITION.partition_function_name(expression)FROM dbo.WebSiteHits;

    -------------15-3. Adding a Partition

    --ALTER PARTITION SCHEME and ALTER PARTITION FUNCTION statements to extend the partition 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_value ) | 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 and 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 the data in that partition intoanother partition.

    15-5. Determining Whether a Table Is Partitioned

  • 8/18/2019 15.partiton

    4/4

    SELECT p.partition_id,p.object_id,p.partition_numberFROM sys.partitions AS pWHERE p.partition_id IS NOT NULLAND p.object_id = OBJECT_ID('dbo.WebSiteHits')

    ALTER PARTITION FUNCTION HitsDateRange () MERGE RANGE ('2007-01-01T00:00:00');