Link to home
Start Free TrialLog in
Avatar of dbaSQL
dbaSQLFlag for United States of America

asked on

Partitioned table/index definition

v2008, i've got a partioned structure in place.  it's been in place for a while, actually, but i've made many changes as it has evolved....  long story short,

-we are partitioning by day (intake of 100M records daily)
-initially, i had created my function using the right range with 367 partitons.  (365 for the days within the year, one before, one after)  my partition key is 'TimeField', datetime2
-there are three tables, each on the scheme, with clustered PK on the scheme, of TimeField,OrderID

so, this was all well and good.  I have migrated a TON of data from the v2000 non-partitioned version of the same database into 08.  working to get things together such that we can officially upgrade.  

last week, however, i found this:  http://msdn.microsoft.com/en-us/library/dd578580.aspx
Within which i noted reference to using the persisted computed column as my partition key.  i decided this was more to my gain, as the 'hot' data is always current day.  it's not time frames within the current day.  it is just today.  or yesterday, or the day in question,  in entirety.

I created a new partiton scheme, also daily, 367 partitions, only the partionkey is 'PartitionDate', which is a computed column, based on the 'TimeField' that I receive with each insertion, this is in place on each of the tables.

They give me TimeField, my PartitionDate is CONVERT(DATE,TimeField,112) PERSISTED.

Again, all well and good... in retrospect, however, I believe it is not do-able.  The storage of the data, good, but possibly not the retrieval thereof.  Nobody is going to look at PartitionDate.  They're going to go in by TimeField, which is a datetime2.

I had created the table as defined below, dropped the PK and pumped a TON of data into place.  I then recreated the PK just fine.  I then attempted the aligned index on 'TimeField' such that the applications actually looking at the data can do so efficiently.   Of course, my index creation failed with this:

Msg 7726, Level 16, State 1, Line 2
Partition column 'TimeField' has a data type of datetime2(7) which is different from the partition function 'functionname' parameter data type date.

And I am smacked in the face with the reminder that the aligned indexes must reference the partionkey, and the datatype of my new one isn't going to work in relation to the TimeField in my tables.

Not a big deal -- I didn't drop the old stuff, I just created a new scheme/function/tables alongside.  Now I'm curious simply about the design of these entities.  

OrderID is bigint and unique.
The paritions will be daily.

What is the optimal means of definiing the table and indices in this regard?

CREATE TABLE [dbo].[TableName](
	[PartitionDate] AS CONVERT(DATE,Eventtime,112) PERSISTED,
	[InsertTime] [datetime2](7) NOT NULL,
	[TimeField] [datetime2](7) NOT NULL,
	[OrderID] [bigint] NOT NULL,
	[GID] [int] NOT NULL,
	[EndPoint] [varchar](8) NOT NULL,
	[UserID] [varchar](16) NOT NULL,
	[AcctID] [varchar](16) NOT NULL,
	[ClientID] [varchar](40) NOT NULL,
	[Symbol] [varchar](16) NOT NULL,
	[Side] [char](1) NOT NULL,
	[TimeInForce] [varchar](5) NOT NULL,
	[ExecInstruction] [varchar](10) NULL,
 CONSTRAINT [PK_TableName_PartitionDate_OrderID] PRIMARY KEY CLUSTERED 
(
	[PartitionDate] ASC,
	[OrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [schemename] ([PartitionDate])
) ON [schemename] ([PartitionDate])
GO

Open in new window

Avatar of Mark Wills
Mark Wills
Flag of Australia image

Good question...

Yes, I believe the indexes MUST reference the partionkey...

Let me check a couple of things and I will get back soon...
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbaSQL

ASKER

>>But, in reality, if your want any flexibility or speed gains, then your indexes really should be aligned to the partitions...

I think this is where I need to focus.
Thank you, Mr. Wills.

Correct me if I'm wrong -- forget about the computed column (PartitionDate).  Going back to my original design, my partition key is TimeField DATETIME2.  My PK is TimeField_OrderID.
That in itself makes the PK aligned.

>>>>>>>>>
Partitioning NonClustered Indexes
When partitioning a unique nonclustered index, the index key must contain the partitioning column. When partitioning a nonunique, nonclustered index, SQL Server adds the partitioning column by default as a nonkey (included) column of the index to make sure the index is aligned with the base table. SQL Server does not add the partitioning column to the index if it is already present in the index.
>>>>>>>>

Any additional indexes will be nonclustered, of course, and non-unique.  Because SQL adds the partitioning column by default --- I'm pretty much aligned, yes?

CREATE TABLE [dbo].[TableName](
	[InsertTime] [datetime2](7) NOT NULL,
	[TimeField] [datetime2](7) NOT NULL,
	[OrderID] [bigint] NOT NULL,
	[GID] [int] NOT NULL,
	[EndPoint] [varchar](8) NOT NULL,
	[UserID] [varchar](16) NOT NULL,
	[AcctID] [varchar](16) NOT NULL,
	[ClientID] [varchar](40) NOT NULL,
	[Symbol] [varchar](16) NOT NULL,
	[Side] [char](1) NOT NULL,
	[TimeInForce] [varchar](5) NOT NULL,
	[ExecInstruction] [varchar](10) NULL,
 CONSTRAINT [PK_TableName_TimeField_OrderID] PRIMARY KEY CLUSTERED 
(
	[TimeField] ASC,
	[OrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [schemename] ([TimeField])
) ON [schemename] ([TimeField])
GO

Open in new window

Yes it does, but stating the obvious, it also means the partition must be defined first.

In books on line in sql 2008 there is a pretty good example using adventureworks on a not too dissimilar model as you are trying to do. Have you seen it ?

If you want I can try to track it down...
Avatar of dbaSQL

ASKER

nope, i got it, mr. wills.  thank you much