Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Partition

Author  Topic 

WoodHouse
Posting Yak Master

211 Posts

Posted - 2010-04-14 : 07:03:02
Before partition ItemId is the primary key for the table A
Partition has been done on table A using the column MMYYYY

What should be the BEST primary key?
i. MMYYYY,ItemId
ii. ItemId,MMYYYY

Which column should be the lead column in the index if it is composite?
(Note : Both are very different when you query using a single column)

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-14 : 07:36:37
Why do you want to change the primary key? This shouldn't be necessary to partition a table.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

WoodHouse
Posting Yak Master

211 Posts

Posted - 2010-04-14 : 07:49:18
Hi DBA in the making

Thanks

That is the rule of partition, ie partition column must be part of the primary key


I am not able to understand can you please explain the impact of performance.
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-14 : 08:41:25
quote:
Originally posted by WoodHouse
That is the rule of partition, ie partition column must be part of the primary key


That's not quite true. You can partition a table on a field that's not in the primary key. But to do so, the primary key must be declared using a specific filegroup. Here's an example. Here, the table is partitioned across filegroups FG1, FG2, and FG3, and the PK is stored on FG4. Notice the PK is the WidgetID (INT) field, yet the partition is on the DateAdded (DATETIME) field, which is not part of the primary key. To run this script, you'll need to create the folder 'D:\test'
USE master
GO

CREATE DATABASE PartitionTest
ON PRIMARY (
NAME = db_dat, FILENAME = 'D:\test\db.mdf', SIZE = 2MB),
FILEGROUP FG1 (
NAME = FG1_dat, FILENAME = 'D:\test\FG1.ndf', SIZE = 2MB),
FILEGROUP FG2 (
NAME = FG2_dat, FILENAME = 'D:\test\FG2.ndf', SIZE = 2MB),
FILEGROUP FG3 (
NAME = FG3_dat, FILENAME = 'D:\test\FG3.ndf', SIZE = 2MB),
FILEGROUP FG4 (
NAME = FG4_dat, FILENAME = 'D:\test\FG4.ndf', SIZE = 2MB)
LOG ON (
NAME = db_log, FILENAME = 'D:\test\db_log.ldf', SIZE = 2MB)
GO

USE PartitionTest
GO

CREATE PARTITION FUNCTION partfunc(DATETIME) AS
RANGE LEFT FOR VALUES ('20100101', '20100201')
GO

CREATE PARTITION SCHEME partscheme AS
PARTITION partfunc TO
([FG1], [FG2], [FG3])
GO

CREATE TABLE Widgets (
WidgetID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED ON [FG4],
WidgetName VARCHAR(100) NOT NULL,
DateAdded DATETIME NOT NULL
) ON partscheme(DateAdded)
GO

INSERT INTO Widgets (WidgetName, DateAdded)
SELECT 'Widget 1', '20091001' UNION ALL
SELECT 'Widget 2', '20091001' UNION ALL
SELECT 'Widget 3', '20100101' UNION ALL
SELECT 'Widget 4', '20100103' UNION ALL
SELECT 'Widget 5', '20100112' UNION ALL
SELECT 'Widget 6', '20100201' UNION ALL
SELECT 'Widget 7', '20100202' UNION ALL
SELECT 'Widget 8', '20100208'
GO

SELECT * FROM Widgets

/*

USE master
GO
DROP DATABASE PartitionTest

*/


------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

WoodHouse
Posting Yak Master

211 Posts

Posted - 2010-04-14 : 09:12:44
Hi

Thanks a Lot
Go to Top of Page
   

- Advertisement -