Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table partitioning using 2 columns

Tags:

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
like image 640
Rafael Colucci Avatar asked Jan 12 '11 16:01

Rafael Colucci


People also ask

Can we do partition by two columns?

No. Partition by clause allows multiple columns. You might be doing something wrong. Your Query should look something like this.

How do I create a partition on multiple columns in SQL Server?

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.

Can a table have two partitions?

You can add multiple range partitions that are listed in ascending order of their upper bound values to the high end (after the last existing partition) of a range-partitioned or composite range-partitioned table, provided the MAXVALUE partition is not defined.


2 Answers

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.

like image 186
Joe Stefanelli Avatar answered Oct 11 '22 01:10

Joe Stefanelli


I found this was an easier solution

select ROW_NUMBER() over (partition by CHECKSUM(value,ID) order by SortOrder) as Row From your_table
like image 43
Jonathan Avatar answered Oct 11 '22 01:10

Jonathan