SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SWITCH statement: required indexes in target table?


SWITCH statement: required indexes in target table?

Author
Message
WolfgangE
WolfgangE
SSC-Addicted
SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)

Group: General Forum Members
Points: 473 Visits: 792
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


sql-lover
sql-lover
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1569 Visits: 1930
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search