Table partitioning using 2 columns

2019-02-04 11:58发布

问题:

Is it possible to partition a table using 2 columns instead of only 1 for the partition function?

Consider a table with 3 columns

    ID (int, primary key, 
    Date (datetime), 
    Num (int)

I want to partition this table by 2 columns: Date and Num.

This is what I do to partition a table using 1 column (date):

create PARTITION FUNCTION PFN_MonthRange (datetime)
AS
RANGE left FOR VALUES ('2009-11-30 23:59:59:997',
                       '2009-12-31 23:59:59:997',
                       '2010-01-31 23:59:59:997',
                       '2010-28-02 23:59:59:997',
                       '2010-03-31 23:59:59:997')
go

回答1:

Bad News: The partition function has to be defined on a single column.

Good News: That single column could be a persisted computed column that is a combination of the two columns you're trying to partition by.



回答2:

I found this was an easier solution

select ROW_NUMBER() over (partition by CHECKSUM(value,ID) order by SortOrder) as Row From your_table


回答3:

Natively, no you can not partition by two columns in SQL Server.

There are a few things you could do, have a lookup table that you use to extract which arbitary integer (partition) each value is within, but you only have 1000 partitions maximum, so they are going to start occupying the same space. The computed column approach suffers this same problem, you have a 1k partition limit, chances are you will blow it.

I would probably just stick to a date partition, and range right on the 1st of the month, instead of ranging left on the last part of the month.

What do you intend to gain from the second partition value?