使用 2 列进行表分区

发布于 2024-10-12 03:16:56 字数 562 浏览 5 评论 0原文

是否可以使用 2 列而不是仅 1 列的分区函数来对表进行分区?

考虑一个包含 3 列的表,

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

我想将该表按 2 列分区:DateNum

这就是我使用 1 列(日期)对表进行分区的方法:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-10-19 03:16:57

坏消息:分区函数必须在单个列上定义。

好消息:该单列可以是持久计算列 这是您尝试分区的两列的组合。

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.

一萌ing 2024-10-19 03:16:57

我发现这是一个更简单的解决方案

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

I found this was an easier solution

select ROW_NUMBER() over (partition by CHECKSUM(value,ID) order by SortOrder) as Row From your_table
热风软妹 2024-10-19 03:16:57

本质上,您不能在 SQL Server 中按两列进行分区。

您可以做一些事情,使用一个查找表来提取每个值所在的任意整数(分区),但最多只有 1000 个分区,因此它们将开始占用相同的空间。计算列方法也遇到同样的问题,您有 1k 分区限制,很可能您会超出它。

我可能会坚持使用日期分区,并在该月的第一天向右排列,而不是在该月的最后一部分向左排列。

您打算从第二个分区值中获得什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文