如何在大多数事实表使用datetime2数据类型时设置按年进行分区?

huangapple go评论54阅读模式
英文:

How do you set up partitioning by year when most fact tables have a datetime2 data type?

问题

我们正在使用SQL Server 2019。我们的事实表使用datetime2,但我想按年份进行分区。

我没有sysadmin权限,所以无法设置不同的文件组。我可以创建分区函数和分区方案,但我不清楚如何设置分区方案,以便当我将表分区到ActivityLog时,它会将条目存储在各自的年份分区中。

我已在网上搜索,但没有找到关于所有这些如何工作的答案。

英文:

We're using SQL Server 2019. Our fact tables utilize datetime2 but I want to partition on year.

I don't have sysadmin privs so I can't set up different filegroups. I can create partition functions and partition schemes, but it isn't clear to me how to set up the partition scheme so that when I partition the table on ActivityLog for example that it will store entries in their respective year partition.

I've searched the web and haven't found answers as to how it all works.

答案1

得分: 3

按年份在事实表中对datetime2列进行分区可以是管理大型数据集、提高查询性能和降低维护成本的有用技巧。以下是设置按年份进行分区的步骤:

  1. 定义分区函数:分区函数定义了分区数据的范围或边界。在这种情况下,您将定义一个按年份分区数据的分区函数。例如,以下代码创建了一个按年份分区数据的分区函数:
    CREATE PARTITION FUNCTION pfFactTableByYear (datetime2(0))
    AS RANGE RIGHT FOR VALUES
    ('2010-01-01T00:00:00', '2011-01-01T00:00:00', '2012-01-01T00:00:00', '2013-01-01T00:00:00', '2014-01-01T00:00:00', '2015-01-01T00:00:00', '2016-01-01T00:00:00', '2017-01-01T00:00:00', '2018-01-01T00:00:00', '2019-01-01T00:00:00', '2020-01-01T00:00:00')
  1. 定义分区方案:分区方案将分区函数映射到一组文件组。在这种情况下,您将定义一个分区方案,将分区函数映射到一组文件组。例如,以下代码创建了一个将分区函数映射到一组文件组的分区方案:
    CREATE PARTITION SCHEME psFactTableByYear
    AS PARTITION pfFactTableByYear
    TO (fg2010, fg2011, fg2012, fg2013, fg2014, fg2015, fg2016, fg2017, fg2018, fg2019, fg2020)
  1. 创建具有分区的事实表:您将创建具有第2步中定义的分区方案的事实表。例如,以下代码创建了一个按年份分区的事实表:
    CREATE TABLE FactTable
    (
        Id INT IDENTITY(1,1),
        DateColumn datetime2(0) NOT NULL,
        ValueColumn decimal(18,2) NOT NULL,
        CONSTRAINT PK_FactTable PRIMARY KEY (Id, DateColumn)
    )
    ON psFactTableByYear(DateColumn)

这将创建一个具有包括分区列(DateColumn)的主键的事实表,并将分区方案映射到事实表的数据文件组。

  1. 将数据加载到事实表中:一旦创建了事实表,您可以使用标准的INSERT语句将数据加载到其中。

  2. 执行维护任务:随着时间的推移,需要创建新分区来容纳新数据。您可以使用分区切换或运行定期创建新分区的维护脚本来自动化这个过程。您还可以定期归档或删除旧数据,以保持数据集的可管理性。

请注意,按年份进行分区只是分区事实表的一个选项,对于其他分区策略,例如按月、季度或其他时间段分区,分区函数和方案需要相应地进行调整。

英文:

Partitioning by year on a datetime2 column in a fact table can be a useful technique for managing large data sets, improving query performance, and reducing maintenance costs. Here are the steps to set up partitioning by year:

  1. Define a partition function: A partition function defines the ranges or
    boundaries for partitioning the data. In this case, you would define a
    partition function that partitions the data by year. For example, the
    following code creates a partition function that partitions the data by
    year:
    CREATE PARTITION FUNCTION pfFactTableByYear (datetime2(0))
    AS RANGE RIGHT FOR VALUES
    ('2010-01-01T00:00:00', '2011-01-01T00:00:00', '2012-01-01T00:00:00', '2013-01-01T00:00:00', '2014-01-01T00:00:00', '2015-01-01T00:00:00', '2016-01-01T00:00:00', '2017-01-01T00:00:00', '2018-01-01T00:00:00', '2019-01-01T00:00:00', '2020-01-01T00:00:00')
  1. Define a partition scheme: A partition scheme maps the partition function to
    a set of filegroups. In this case, you would define a partition scheme that
    maps the partition function to a set of filegroups. For example, the
    following code creates a partition scheme that maps the partition function
    to a set of filegroups:
    CREATE PARTITION SCHEME psFactTableByYear
    AS PARTITION pfFactTableByYear
    TO (fg2010, fg2011, fg2012, fg2013, fg2014, fg2015, fg2016, fg2017, fg2018, fg2019, fg2020)
  1. Create the fact table with partitioning: You would create the fact table
    with the partition scheme defined in step 2. For example, the following code
    creates a fact table with partitioning by year:
    CREATE TABLE FactTable
    (
        Id INT IDENTITY(1,1),
        DateColumn datetime2(0) NOT NULL,
        ValueColumn decimal(18,2) NOT NULL,
        CONSTRAINT PK_FactTable PRIMARY KEY (Id, DateColumn)
    )
    ON psFactTableByYear(DateColumn)

This creates a fact table with a primary key that includes the partitioning column (DateColumn), and maps the partition scheme to the fact table's data filegroups.

  1. Load data into the fact table: Once the fact table is created, you can load
    data into it using standard INSERT statements.

  2. Perform maintenance tasks: As time goes on, new partitions will need to be
    created to accommodate new data. You can automate this process using
    partition switching or by running a maintenance script that creates new
    partitions on a regular basis. You may also want to periodically archive or
    remove old data to keep the data set manageable.

Note that partitioning by year is just one option for partitioning a fact table, and the partition function and scheme would need to be adjusted accordingly for other partitioning strategies, such as partitioning by month, quarter, or some other time period.

huangapple
  • 本文由 发表于 2023年2月16日 08:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定