简体   繁体   English

在SQL Server 2005中进行分区

[英]Partitioning in SQL Server 2005

I have some questions about partitioning and strategy, how and when to use it. 我对分区和策略,如何以及何时使用它有一些疑问。 As I understood from Partitioned Tables and Indexes in SQL Server 2005 , partitioning is not used only for manageability but also to improve performance on very large database tables (VLDB). SQL Server 2005中的分区表和索引可以理解, 分区不仅用于可管理性,而且还用于提高超大型数据库表(VLDB)的性能。 We have a table with millions of records. 我们有一个包含数百万条记录的表。 This table store performance data, for example how many times was specified item be clicked and so on. 该表存储性能数据,例如,单击指定的项目次数等等。 We need evaluate this data for current month in real application on daily basis. 我们需要每天在实际应用中评估当月的数据。 What I want to prevent is to move data from one table to another, only because of performance. 我要防止的是仅出于性能原因将数据从一个表移动到另一个表。 My idea was: I create partition on this VLDB by month for current year. 我的想法是:我根据当年的月份在此VLDB上创建分区。 Then I would create clustered index on date field of table and partition scheme. 然后,我将在表和分区方案的日期字段上创建聚簇索引。 I don't know if I understand it correctly, but what I except is, that index will be created per partition separately. 我不知道我是否正确理解它,但是我唯一的区别是,将为每个分区分别创建该索引。

Generally how my code looks like in development phase. 通常,我的代码在开发阶段的样子。

/*
Maybe it looks like complicated, but instead of static upper bound, 
I use function to determine end of the month for the current year.

So for example (executed in year 2009)
    SELECT DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1901)*12)+1-1,31-1)) 
returns 
    2008-01-30 23:59:59.997
*/
CREATE PARTITION FUNCTION PartitionMonthlyCurrentYear(DATETIME) AS
RANGE LEFT FOR VALUES
(
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1901)*12)+1-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+1-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+2-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+3-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+4-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+5-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+6-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+7-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+8-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+9-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+10-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+11-1,31-1)),
    DATEADD(ms, -2, DATEADD(month,((DATEPART(YEAR, GetDate())-1900)*12)+12-1,31-1))
);

/*
Create scheme on the primary file group. I'm aware about performance issues of this.
*/
CREATE PARTITION SCHEME SchemeMonthlyCurrentYear 
AS PARTITION PartitionMonthlyCurrentYear
ALL TO ([PRIMARY]);

/*
Create clustered index on table and scheme
*/
CREATE CLUSTERED INDEX [IX_Log_Seiten_archive_Datum] ON [dbo].[Log_Seiten_archiv] 
(
    [Datum] DESC
)ON SchemeMonthlyCurrentYear(Datum)
GO

My questions: 我的问题:

  1. What you think about this approach? 您如何看待这种方法?
  2. How can I remove table from specified partition scheme? 如何从指定的分区方案中删除表? If I drop index, still can't drop scheme and function because there is still dependency on table Log_Seiten_archiv 如果删除索引,仍然不能删除方案和功能,因为仍然依赖于表Log_Seiten_archiv
  3. How can I assign table to use different partition function? 如何分配表以使用不同的分区功能? Because of development I need often change how partitions function is defined, or create new one. 由于开发的原因,我经常需要更改定义分区功能的方式或创建新分区。 How can I do this for existing table? 如何为现有表格执行此操作? For example I want change year on function I showed before. 例如,我想更改以前显示的功能的年份。

Regards Anton Kalcik 问候安东·卡尔西克

  1. You can't create SQL Server partition functions with non-deterministic functions like getdate(). 您不能使用诸如getdate()之类的不确定函数来创建SQL Server分区函数。 If you populated your database with data spanning 10 years, and then simply stopped and watched the SQL Server run for a few years, you would see SQL Server moving that data from one partition to another as dates changed. 如果您在数据库中填充了10年的数据,然后只是停止并看着SQL Server运行了几年,则随着日期的更改,您会看到SQL Server将该数据从一个分区移动到另一个分区。 SQL Server partitioning doesn't work that way. SQL Server分区不能以这种方式工作。 When the partition function is called, the data is placed into a specific partition, and that's the end of it. 当分区函数被调用时,数据被放置在一个特定的分区中,到此为止。 It's not moved again unless you want to manually move it. 除非您要手动移动它,否则不会再次移动它。

  2. To remove partitioning on a table, apply a new clustered index that doesn't use a partition scheme. 要删除表上的分区,请应用不使用分区方案的新的聚集索引。 The data will be moved. 数据将被移动。 (Be aware that it'll take a long time depending on the size of your data and the speed of your disks - for a multi-terabyte data warehouse, we're probably talking hours if not days.) (请注意,这将需要很长时间,具体取决于数据大小和磁盘速度-对于一个数TB的数据仓库,如果不是几天,我们可能要聊几个小时。)

  3. To assign a different partition function, you apply a new clustered index using the new partition function. 要分配其他分区功能,请使用新分区功能应用新的聚簇索引。 Again, though, if you change your partition functions often, it's going to take a long time to rebuild, and partitioning probably isn't the answer you're looking for. 再一次,但是,如果您经常更改分区功能,则将需要很长时间来重建,而分区可能不是您要找的答案。

Partitioning is an awesome solution to >100 million row databases with separate sets of drive arrays, but it sounds like you're just putting everything on the same set of drives, same filegroup. 分区是对具有独立驱动器阵列集的超过1亿行数据库的绝佳解决方案,但听起来就像您将所有内容都放在同一组驱动器,同一文件组上一样。 You're not going to see a big performance increase there. 您不会在那里看到大幅的性能提升。

Also, be aware that partitioning is only available in SQL Server Enterprise Edition, not Standard. 另外,请注意,分区仅在SQL Server Enterprise Edition中可用,而在Standard中不可用。 It's available in Dev, but you can't use that in production due to licensing restrictions. 它在Dev中可用,但是由于许可限制,您不能在生产中使用它。

thank you for your quick answer. 谢谢你们的快速响应。

  1. You are true that developer is responsible how data will be transported into partitioned table. 您确实对开发人员负责如何将数据传输到分区表中负责。 There are many approaches how to this and every of them has advantages and disadvantages. 有许多方法可以做到这一点,每种方法都有其优点和缺点。 I found excellent article about this on Technet . 我在Technet上找到了关于此的出色文章。

I understood now, how can I remove or reassign partition function. 我现在了解了,如何删除或重新分配分区功能。

Here example: 这里的例子:

Create partition on existing table: 在现有表上创建分区:

CREATE CLUSTERED INDEX [IX_Log_Seiten_archive_Datum_Kanzleinr] ON [dbo].[Log_Seiten_archiv_Daily] 
(
    [Datum] DESC,
    [kanzleinr] ASC
)ON SchemeDailyCurrentYear(Datum) 
GO

Drop partition on existing table: 在现有表上删除分区:

DROP INDEX [IX_Log_Seiten_archive_Datum_Kanzleinr] ON [dbo].[Log_Seiten_archiv_Daily];

CREATE CLUSTERED INDEX [IX_Log_Seiten_archive_Datum_Kanzleinr] ON [dbo].[Log_Seiten_archiv_Daily] 
(
    [Datum] DESC,
    [kanzleinr] ASC
)ON [PRIMARY]
GO

Thanks for you help AKa 感谢您的帮助AKa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM