sql server表分区

The idea behind this article is to discuss the importance and the implication of SQL Partition and understand the SQL truncate command partitioning enhancements in SQL 2016

本文背后的想法是讨论SQL分区的重要性和含义,并了解SQL 2016中SQL 截断命令分区增强功能

One of the biggest challenges for a DBA is to identify the right candidate for table partitioning, as it requires expertise in design and implementation.

对于DBA来说,最大的挑战之一是确定合适的表分区候选者,因为它需要设计和实现方面的专业知识。

The following are the various truncating options available in SQL 2016

以下是SQL 2016中可用的各种截断选项

  • Truncate individual partitions 截断单个分区
  • Truncate multiple individual partitions 截断多个单独的分区
  • Truncate a Range of partitions 截断分区范围
  • Truncate a Range with multiple individual partitions 截断具有多个单独分区的范围

文章重点 (Article Highlights)

  • Define the importance and the implication of SQL table partitioning 定义SQL表分区的重要性和含义
  • Identify the right candidate for table partitioning 确定合适的表分区候选者
  • Provide inline comparison of features available in the different editions of SQL 2016 提供SQL 2016不同版本中可用功能的内联比较
  • Discuss the SQL truncate partition enhancements in SQL 2016 讨论SQL 2016中SQL截断分区增强功能
  • Demonstrate the SQL truncate table partition use cases 演示SQL截断表分区用例

SQL分区 (SQL Partition)

Let’s understand the objective of SQL partitioning, why we need partitioning, and the factors that are vital to decide on a Table Partitioning Strategy.

让我们了解SQL分区的目标,为什么需要分区以及决定表分区策略至关重要的因素。

Partitions are logical mapping of the physical data. A well-designed partition gives us an option to scale out the data. It optimizes the performance and simplifies the management of data by partitioning each table into multiple separate partitions. Although, not all tables are good candidates for partitioning. If the answer is ‘yes’ to all or most of the following questions, table partitioning may be a viable database design strategy; if the answer is ‘no’ to most of the following questions, table partitioning may not be the right solution for that table.

分区是物理数据的逻辑映射。 精心设计的分区为我们提供了扩展数据的选项。 通过将每个表划分为多个单独的分区,它可以优化性能并简化数据管理。 虽然,并非所有表都适合分区。 如果对以下所有或大多数问题的回答为“是”,则表分区可能是可行的数据库设计策略; 如果对以下大多数问题的回答为“否”,则表分区可能不是该表的正确解决方案。

  • Is the table large enough?
    Large fact tables are good candidates for table partitioning. If we have millions or billions of records in a table, we may see performance benefits from breaking that data up into logically smaller chunks. Since smaller tables are less susceptible to performance problems, the administrative overhead of maintaining the partitions will outweigh any performance benefits we might see by partitioning.
  • 桌子够大吗?
    大事实表是表分区的理想选择。 如果我们在一个表中有数百万或数十亿条记录,我们可能会发现将数据分解为逻辑上较小的块可以提高性能。 由于较小的表不太容易受到性能问题的影响,因此维护分区的管理开销将超过我们通过分区可能看到的任何性能优势。
  • Does your application or system maintain a window of historical data? 您的应用程序或系统是否维护历史数据窗口?
    Another consideration for partition design is your organization’s data retention policy. For example, your data warehouse may require that you keep the data from the past twelve months. If the data is partitioned by month, you can easily drop the oldest monthly partition from the warehouse and load current data into the most recent monthly partition.
    分区设计的另一个考虑因素是组织的数据保留策略。 例如,您的数据仓库可能要求您保留过去十二个月的数据。 如果按月对数据进行分区,则可以轻松地从仓库中删除最早的每月分区,并将当前数据加载到最新的每月分区中。
  • Are you experiencing performance issues on database maintenance tasks? 您是否在数据库维护任务中遇到性能问题?
    As we deal with larger tables, it is more difficult to perform the rebuilding operations. In such scenarios, one can rely on table partitioning, so that maintenance operations can be seamlessly performed.
    当我们处理更大的表时,执行重建操作会更加困难。 在这种情况下,可以依靠表分区,以便可以无缝执行维护操作。
  • Can the data be divided into equal parts based on a certain criteria? 可以根据一定的标准将数据分为相等的部分吗?
    Choose the partition criteria that will divide the data as evenly as possible. This will let the SQL Query Optimizer to decide and select the best plan for query execution. The monthly partition as mentioned in the second point is a good example for this.
    选择将尽可能均匀地划分数据的分区标准。 这将使SQL查询优化器可以决定并选择最佳的查询执行计划。 第二点提到的每月分区就是一个很好的例子。
  • Is concurrency an issue?
    Does the system involve a huge volume of data loading and reporting? Are user queries frequently getting locked or blocked? If yes, partitioning could be an option to ease the situation, since the Query Optimizer would be able to handle the execution better.
  • 并发问题吗?
    系统是否涉及大量的数据加载和报告? 用户查询是否经常被锁定或阻止? 如果可以,可以使用分区来缓解这种情况,因为查询优化器将能够更好地处理执行情况。

If someone complains about slowness of a query, it isn’t necessary that the related tables need to be partitioned. In most cases, partitioning would not improve performance. A proper indexing strategy may suffice for some of the performance problems in many such use cases. For instance, if a query is written to use a partition key, then the query execution improves; deterioration in performance may be linked to not using the partition key in this case

如果有人抱怨查询速度慢,则不必对相关表进行分区。 在大多数情况下,分区不会提高性能。 在许多此类用例中,适当的索引策略可能足以解决某些性能问题。 例如,如果编写查询以使用分区键,则查询执行将得到改善; 在这种情况下,性能下降可能与不使用分区键有关。

The earlier versions of SQL Server required a lot of extra effort in restoring the database with partitioned tables to a non-Enterprise edition of SQL Server. Any attempt to restore resulted in dropping of the Partition Function and the Partition Scheme prior to the database backup and restore process; this is not the case with SQL 2016.

在将具有分区表的数据库还原到非企业版SQL Server之前,SQL Server的早期版本需要大量的额外工作。 任何还原尝试都会导致在数据库备份和还原过程之前删除分区功能和分区方案; SQL 2016并非如此。

The data truncation/deletion/reshuffling operations had to rely on partition switching and merge mechanism. However, with SQL Server 2016, some interesting features with the truncate command have been introduced, which serve the purpose of data removal with minimal logging.

数据截断/删除/重新组合操作必须依靠分区切换和合并机制。 但是,在SQL Server 2016中,引入了一些带有truncate命令的有趣功能,这些功能用于以最少的日志记录删除数据的目的。

功能支持 (Feature Support)

The Partitioning feature has been an Enterprise edition feature, starting from SQL 2005, but for the first time, it has been made available on all the editions of SQL 2016.

从SQL 2005开始,分区功能已成为企业版功能,但首次在所有SQL 2016版本中都可用。

Feature Enterprise Standard Web Express with Advanced Services Express
Table and
Index Partitioning
Yes Yes  Yes  Yes  Yes 
特征 企业 标准 网页 快捷 服务 表达
表和
索引分区

SQL截断表语法及其用法 ( SQL Truncate table syntax and its usage)

SQL TRUNCATE TABLE and using WITH PARTITIONS () option enables the mechanism to truncate the data within the defined partition number(s) or a range of partitions.

SQL TRUNCATE TABLE和使用WITH PARTITIONS()选项使该机制能够截断定义的分区号或分区范围内的数据。


TRUNCATE TABLE   [ { database_name .[ schema_name ] . | schema_name . } ]  table_name  [ WITH ( PARTITIONS ( { <partition_number_expression> | <range> }   [ , ...n ] ) ) ]
[ ; ]  <range> ::=
<partition_number_expression> TO <partition_number_expression>
  •  TRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (2));
  •  TRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (1,3,6))
  •  TRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (1,2, 4 TO 6));
  • TO, for example:  TO分隔的分区号,例如:
     TRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (4 TO 6));

如何以及何时将SQL截断表与Partition子句一起使用? (How and when to use the SQL truncate table with Partition clause?)

The following section discusses the scenario of how to use the SQL truncate Table option for data removal for partitioned table

下一节讨论如何使用SQL截断表选项删除分区表的数据的方案

The Quick Archival/Purging process involves switching out the partitions. Though Switch Partition is a metadata update operation and doesn’t involve movement of physical data between the data files, in some cases like high transactional databases, it requires exclusive locks, and this causes the table being blocked for loading and removing the data.

快速归档/清除过程涉及切换分区。 尽管“ 交换分区”是一种元数据更新操作,并且不涉及数据文件之间的物理数据移动,但在某些情况下(例如,高事务性数据库),它需要排他锁,这会导致表被阻塞,无法加载和删除数据。

Data purging (deletion) from specific partition or partitions was a tedious task in the previous versions of SQL. Data caused slowness and locked the table, which prevented the users from querying the table; it also ended up using a lot of transaction log space. Prior to SQL 2016, the following steps were performed for data deletion or to remove files from the partition

在早期版本SQL中,从一个或多个特定分区清除(删除)数据是一项繁琐的任务。 数据导致速度慢并锁定了表,从而阻止了用户查询表。 它最终还占用了大量事务日志空间。 在SQL 2016之前,执行以下步骤进行数据删除或从分区中删除文件

  • Create a new table for switching out with the same definition and clustered index as of the partitioned table 创建一个新表以与分区表具有相同的定义和聚集索引进行转出
  • Switch partition out to the other table  将分区切换到另一个表
  • Alter the Partition function and Partition Scheme to get rid of the file group 更改分区功能和分区方案以摆脱文件组
  • by merging the boundary 通过合并边界
  • Remove the file group 删除文件组

Note: This is challenging when we deal with a huge number of rows

注意:当我们处理大量行时,这具有挑战性

What about introducing the new SQL TRUNCATE TABLE command in this scenario?

在这种情况下,如何引入新SQL TRUNCATE TABLE命令呢?

This is a quite seamless and an efficient way to delete the rows from the partitioned table, as it works as a normal SQL Truncate Table operation.

这是从分区表中删除行的非常无缝且有效的方法,因为它可以像普通SQL 截断表操作一样工作。

  • Execute SQL TRUNCATE TABLE with partition clause 使用partition子句执行SQL TRUNCATE TABLE
  • Execute MERGE command 执行MERGE命令
  • Remove the empty filegroup 删除空文件组

示范 (Demonstration)

In SQL Server 2016 we have the feature to truncate data at the partition level. It’s pretty straight forward and simple. Let’s go through the simple steps to show how it works.

在SQL Server 2016中,我们具有在分区级别截断数据的功能。 这非常简单明了。 让我们通过简单的步骤来演示其工作原理。

Create a sample database powerSQLPartitionTest and add new filegroups. These files are physical representation of SQL data

创建一个示例数据库powerSQLPartitionTest并添加新的文件组。 这些文件是SQL数据的物理表示


DROP DATABASE IF EXISTS powerSQLPartitionTest;
GO
CREATE DATABASE powerSQLPartitionTest;USE powerSQLPartitionTest
GO
--The following statements create filegroups to a database powerSQLPartitionTestALTER DATABASE powerSQLPartitionTest ADD FILEGROUP [Filegroup_2017]
GO
ALTER DATABASE powerSQLPartitionTest ADD FILEGROUP [Filegroup_2018]
GO
ALTER DATABASE powerSQLPartitionTest ADD FILEGROUP [Filegroup_2019]
GO
ALTER DATABASE powerSQLPartitionTest ADD FILEGROUP [Filegroup_2020]
GO
ALTER DATABASE powerSQLPartitionTest ADD FILEGROUP [Filegroup_2021]  #Add one file to each filegroup so that you can store partition data in each filegroupALTER DATABASE powerSQLPartitionTestADD FILE(
NAME = N’data_2017’,
FILENAME = N’f:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_2017.ndf’,
SIZE = 5MB,
MAXSIZE = 100MB,
FILEGROWTH = 2MB)TO FILEGROUP [Filegroup_2017]
GO
ALTER DATABASE powerSQLPartitionTestADD FILE(
NAME = N’data_2018’,
FILENAME = N’f:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_2018.ndf’,
SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)TO FILEGROUP [Filegroup_2018]
GO
ALTER DATABASE powerSQLPartitionTestADD FILE(
NAME = N’data_2019’,FILENAME = N’f:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_2019.ndf’,SIZE = 5MB,MAXSIZE = 100MB,
FILEGROWTH = 2MB)TO FILEGROUP [Filegroup_2019]
GO
ALTER DATABASE powerSQLPartitionTestADD FILE(
NAME = N’data_2020’,FILENAME = N’f:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_2020.ndf’,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)TO FILEGROUP [Filegroup_2020]
GO
ALTER DATABASE powerSQLPartitionTestADD FILE(
NAME = N’data_2021’,FILENAME = N’f:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_2021.ndf’,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)TO FILEGROUP [Filegroup_2021]
GO  ---Create Partition Range Function as followsCREATE PARTITION FUNCTION powerSQLPartitionTest_PartitionRange (INT)
AS RANGE LEFT FOR VALUES (10,20,30,40,50);
GO----Mapping partition scheme filegroups to the partition range function CREATE PARTITION SCHEME powerSQLPartitionTest_PartitionScheme
AS PARTITION powerSQLPartitionTest_PartitionRange TO ([PRIMARY], [Filegroup_2017],Filegroup_2018,Filegroup_2019,Filegroup_2020,Filegroup_2021);
GO--Now that there is a partition function and scheme, you can create a partitioned table. The syntax is very similar to any other CREATE TABLE statement except it references the partition scheme instead of a referencing filegroupCREATE TABLE powerSQLPartitionTestTable
(ID INT NOT NULL,
Date DATETIME default getdate())
ON powerSQLPartitionTest_PartitionScheme (ID);
GO--Now that the table has been created on a partition scheme powerSQLPartitionTestTable , populate table using sample dataInsert into powerSQLPartitionTestTable (ID)
SELECT r_Number
FROM (
SELECT ABS(CAST(NEWID() AS binary(6)) %1000) + 1 r_Number
FROM master..spt_values) sample
GROUP BY r_Number
ORDER BY r_Number--- select Data from powerSQLPartitionTestTableSELECT *
FROM powerSQLPartitionTestTable;
GO--Next we can use $PARTITION function to retrieve row countsFor each partition:  SELECT $PARTITION.powerSQLPartitionTest_PartitionRange(ID)  AS PARTITIONID,COUNT(* ) AS ROW_COUNT
FROM     dbo.powerSQLPartitionTestTable
GROUP BY $PARTITION.powerSQLPartitionTest_PartitionRange(ID)
ORDER BY PARTITIONIDSELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)=’powerSQLPartitionTestTable’;
GO--SQL Truncate individual PartitionTRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (2));SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)=’powerSQLPartitionTestTable’;
GO

--SQL Truncate Multiple individual Partition
TRUNCATE TABLE powerSQLPartitionTestTable WITH (PARTITIONS (1,3))
GO
SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)=’powerSQLPartitionTestTable’;
GO

--Range of Parititions
TRUNCATE TABLE powerSQLPartitionTestTable WITH (PARTITIONS (4 TO 6));
GO
SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)=’powerSQLPartitionTestTable’;
GO

-- Both Range of Parititions and Individual Partitions
TRUNCATE TABLE powerSQLPartitionTestTable WITH (PARTITIONS (1,2, 4 TO 6));
GO
SELECT *
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)=’powerSQLPartitionTestTable’;
GO
 --Remove files from Partitioned table using truncate enhancement

The below SQL gives SQL partition internal details

下面SQL给出了SQL分区的内部细节


SELECT $PARTITION.powerSQLPartitionTest_PartitionRange(ID)  AS PARTITIONID,COUNT(* ) AS ROW_COUNT
FROM     dbo.powerSQLPartitionTestTable
GROUP BY $PARTITION.powerSQLPartitionTest_PartitionRange(ID)
ORDER BY PARTITIONID
 --The filegroup_2017 is ready for data deletion by using SQL truncate command

--SQL Truncate individual Partition
TRUNCATE TABLE dbo.powerSQLPartitionTestTable WITH (PARTITIONS (2));

Now, the filegroup_2017 is empty and ready for removal. Make sure the dependency is broken from every part of its usage. Once done, merge the boundary point this will remove the entry from Partition Function

现在,filegroup_2017为空,可以删除了。 确保从用法的每个部分都打破了依赖关系。 完成后,合并边界点,这将从分区功能中删除该条目


--Merge the range in order to get rid of the data filegroup_2017
ALTER PARTITION FUNCTION powerSQLPartitionTest_PartitionRange() MERGE RANGE (200);

ALTER DATABASE powerSQLPartitionTest REMOVE FILE data_2017

--Online Index Maintenantenance on ALL ParitionsALTER INDEX PK_powerSQLPartitionTestTable_IDX
ON [dbo].powerSQLPartitionTestTable
REBUILD PARTITION = ALL
WITH (ONLINE= ON);--Online Index Maintenantenance on Parition 3ALTER INDEX PK_powerSQLPartitionTestTable_IDX
ON [dbo].powerSQLPartitionTestTable
REBUILD PARTITION = 3
WITH (ONLINE= ON);

结论 (Conclusion)

This article details the use of Partitioning in SQL Server and the factors which are vital for considering before partitioning a table. It also outlines the use of the SQL truncate table partition enhancement. With SQL Server 2016, one can plan for a better index maintenance and data management strategies.

本文详细介绍了SQL Server中分区的用法以及在对表进行分区之前必须考虑的重要因素。 它还概述了SQL 截断表分区增强功能的使用。 使用SQL Server 2016,可以计划更好的索引维护和数据管理策略。

翻译自: https://www.sqlshack.com/sql-server-2016-enhancements-truncate-table-table-partitioning/

sql server表分区

sql server表分区_SQL Server 2016增强功能– SQL截断表和表分区相关推荐

  1. sql server表分区_SQL Server中的FORCESCAN和分区表

    sql server表分区 I would like to share one curios case that I recently came across. 我想分享一下我最近遇到的一个古玩案例. ...

  2. sql server新增列_SQL Server 2016安装中的新增功能?

    sql server新增列 SQL Server 2016 introduced officially on the 1st of June 2016. It comes with many new ...

  3. 哈希分区和顺序分区_SQL Server中的哈希分区

    哈希分区和顺序分区 In SQL Server, when talking about table partitions, basically, SQL Server doesn't directly ...

  4. sql server新增列_SQL Server 2017中的新增功能

    sql server新增列 SQL Server 2017 is considered a major release in the history of the SQL Server life cy ...

  5. sql 标量子查询_SQL Server 2017:标量子查询简化

    sql 标量子查询 Nowadays a lot of developers use Object-Relational Mapping (ORM) frameworks. ORM is a prog ...

  6. sql server 主键_SQL Server中人口过多的主键和CE模型的变化

    sql server 主键 In this blog post, we are going to talk about another cardinality estimation model enh ...

  7. sql server高可用_SQL Server 2019常规可用性和安装概述

    sql server高可用 介绍 (Introduction) On November 4th, 2019, during the Ignite conference at Orlando, Micr ...

  8. sql 自定义函数 示例_SQL Server Choose()函数介绍和示例

    sql 自定义函数 示例 In the article, a CASE statement in SQL, we explored one of the important logical expre ...

  9. cte公用表表达式_SQL Server中的CTE; 使用公用表表达式解决重新编制标识符列的问题

    cte公用表表达式 Since we know that the SQL CTE (common table expression) offers us a tool to group and ord ...

最新文章

  1. 互联网时代的云服务器四大功能
  2. mysql去除重复数据 重建表_删除掉mysql 的.ibd,.frm,ibdata1,ib_logfile0和ib_logfile1文件后再drop表。然后重建此表,有问题吗...
  3. 教育场景下的实时音频解决方案
  4. mui用ajax上拉加载更多,mui上拉加载更多的使用
  5. html4的语法,HTML——语法
  6. 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
  7. Opencv笔记(十三)——图像的梯度
  8. 【LeetCode】汉明距离(Hamming Distance)
  9. 【数据分享】工人收入工资及其社会经济影响因素数据
  10. Ubuntu安装Monaco字体
  11. 通过vb代码将多个excel合并成一个
  12. GitHub上的那些LGTM和WIP代表什么
  13. 【励志】知乎热门:我是怎么变自律的?
  14. ITOP4412----Uboot2020移植记录
  15. Android Mms专题之:联系人管理
  16. c语言如何生成csv文件格式,生成 csv 文件
  17. 数码管显示原理——MCU51
  18. 机器学习实战案例—验证码(CAPTCHA)识别基于Logistic
  19. java 将json写入txt_关于json:在java中将String写入文本文件
  20. 蜻蜓FM语音下载(qingtingdown)

热门文章

  1. list 分组_Todo List(源码已上传git): 待办事项自定义分组 - 第三章
  2. Python/Java程序员面试必备常用问题解析与答案
  3. python几个面试题整理
  4. POJ2987 Firing 最大权闭合图
  5. 好用的chrome插件总结
  6. 一文读懂babel编译流程,再也不怕面试官的刁难了
  7. LeetCode(709)——转换成小写字母(JavaScript)
  8. 【Vue2.0】—常用的内置指令(九)
  9. 【Vue2.0】—事件处理和事件修饰符(二)
  10. JavaScript学习(八十七)—流程控制语句的总结,超级详细!!!