SWITCH statement: required indexes in target table?

  • hello,

    is there some information about which indexes have to be created on a table to which I want to switch a partition? Books online says:

    The corresponding indexes, or index partitions, must also reside in the same filegroup.

    But it does not tell me wether the target table has to have any indexes at all.

    After some testing I got the following:

    Test 1: A partitioned source table with a clustered PK. The target table, not partitioned, needs the same PK definition.

    Test 2: A partioned source table with a nonclusterd PK and an additional clustered index. In this case the target table, not partitioned, does not need any index or PK to perform the switch.

    Now I'd like to have the official rules what is neccessary for the target table. Did not find any useful on google.

    For people who like to evaluate my testing results, here is my testing skript:

    -- create a test database

    create database Test01;

    go

    USE Test01

    GO

    -- create the parition function and scheme

    CREATE PARTITION FUNCTION [PFN_EventPartitionKey](smallint) AS RANGE RIGHT FOR VALUES (1309, 1310)

    GO

    CREATE PARTITION SCHEME [PS_EventPartitionKey] AS PARTITION [PFN_EventPartitionKey] TO ([PRIMARY], [PRIMARY], [PRIMARY] )

    GO

    -- create 2 data tables with different kind of PK and clustered index

    create table dbo.PartitionTest1(

    Id bigint not null,

    PartitionKey smallint not null,

    Col1 varchar(50) null

    ) on PS_EventPartitionKey( PartitionKey )

    go

    alter table dbo.PartitionTest1

    add constraint PartitionTest1_PK

    primary key clustered( Id, PartitionKey )

    on PS_EventPartitionKey( PartitionKey )

    go

    create table dbo.PartitionTest2(

    Id bigint not null,

    PartitionKey smallint not null,

    Col1 varchar(50) null

    ) on PS_EventPartitionKey( PartitionKey )

    go

    alter table dbo.PartitionTest2

    add constraint PartitionTest2_PK

    primary key nonclustered( Id, PartitionKey )

    on PS_EventPartitionKey( PartitionKey )

    go

    create nonclustered index cix_PartitionTest2

    on dbo.PartitionTest2 ( Col1 )

    on PS_EventPartitionKey( PartitionKey )

    go

    -- now create the target tables, in which we want so switch

    create table dbo.PartitionTest1Archive(

    Id bigint not null,

    PartitionKey smallint not null,

    Col1 varchar(50) null

    ) on [PRIMARY]

    go

    --alter table dbo.PartitionTest1Archive

    --add constraint PartitionTest1Archive_PK

    --primary key clustered( Id, PartitionKey )

    --on [PRIMARY]

    --go

    -- this raises an error. we have to create the PK first (the comment here above)

    alter table dbo.PartitionTest1 switch partition 2 to dbo.PartitionTest1Archive;

    go

    create table dbo.PartitionTest2Archive(

    Id bigint not null,

    PartitionKey smallint not null,

    Col1 varchar(50) null

    ) on [PRIMARY]

    go

    -- this works

    alter table dbo.PartitionTest2 switch partition 2 to dbo.PartitionTest2Archive;

    go

  • Not sure if I understand your question correctly, but you create an Index based on the attribute that you want to use for partitioning. For instance, if you are planning to archive or move data based on order date, your Index for the partitioning should be based on the order date attribute.

    Gail has a very nice article about partitioning and query performance[/url] It may not answer your question directly but will go more in detail about the process.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply