Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SWITCH statement: required indexes in target table? Expand / Collapse
Author
Message
Posted Monday, September 30, 2013 12:43 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:31 AM
Points: 194, Visits: 717
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

Post #1499855
Posted Monday, September 30, 2013 11:31 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, July 24, 2014 10:24 AM
Points: 373, Visits: 1,235
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 It may not answer your question directly but will go more in detail about the process.
Post #1500126
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse