博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to Remove Table Partitioning in SQL Server
阅读量:5254 次
发布时间:2019-06-14

本文共 5191 字,大约阅读时间需要 17 分钟。

In this article we will see how we can remove partitions from a table in a database in SQL server. In my previous post i had demonstrated how we can partition a table via T-SQL. Lets now remove the partitions and merge the data in a single partition. I will start from where we left off in my previous post of partitioning a table.

1) Run the below code to create a database named PartitionDB that would include a table that has been partitioned.

USE masterGOCREATE DATABASE PartitionDBON PRIMARY (NAME = N'PartitionDB',FILENAME = N'D:\MSSQL\Data\PartitionDB.mdf',SIZE = 50MB, FILEGROWTH = 150MB)LOG ON (NAME = N'PartitionDB_log',FILENAME = N'D:\MSSQL\Logs\PartitionDB_log.ldf',SIZE = 10MB, FILEGROWTH = 100MB);GO ALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG1;GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG2;GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG3;GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG4;GO ALTER DATABASE PartitionDB    ADD FILE    (        NAME = PartitionFile1,        FILENAME = 'D:\MSSQL\Data\PartitionFile1.ndf',        SIZE = 20MB, MAXSIZE = 50MB, FILEGROWTH = 5MB    )    TO FILEGROUP PartitionFG1;GO ALTER DATABASE PartitionDB    ADD FILE    (        NAME = PartitionFile2,        FILENAME = 'D:\MSSQL\Data\PartitionFile2.ndf',        SIZE = 20MB, MAXSIZE = 50MB, FILEGROWTH = 5MB    )    TO FILEGROUP PartitionFG2;GO ALTER DATABASE PartitionDB    ADD FILE    (        NAME = PartitionFile3,        FILENAME = 'D:\MSSQL\Data\PartitionFile3.ndf',        SIZE = 20MB, MAXSIZE = 50MB, FILEGROWTH = 5MB    )    TO FILEGROUP PartitionFG3;GO ALTER DATABASE PartitionDB    ADD FILE    (        NAME = PartitionFile4,        FILENAME = 'D:\MSSQL\Data\PartitionFile4.ndf',        SIZE = 20MB, MAXSIZE = 50MB, FILEGROWTH = 5MB    )    TO FILEGROUP PartitionFG4;GO CREATE PARTITION FUNCTION PartFunc1 (int)    AS RANGE LEFT FOR VALUES (10, 20, 30);GO CREATE PARTITION SCHEME PartScheme1    AS PARTITION PartFunc1    TO (PartitionFG1, PartitionFG2,PartitionFG3,PartitionFG4);GO USE [PartitionDB]GOCREATE TABLE PartitionTable    (    MyID int NOT NULL,    MyDate datetime NULL,    Name varchar(50) NULL    )  ON PartScheme1(MyID)GOUSE PartitionDBgoCREATE UNIQUE CLUSTERED INDEX IX_PartitionTableON PartitionTable(MyID)ON PartScheme1 (MyID);GO USE PartitionDBgoINSERT INTO PartitionTable (MyID, MyDate,name)VALUES (1,GETDATE(),'Rooney');INSERT INTO PartitionTable (MyID, MyDate,name)VALUES (11,GETDATE(),'Van persie');INSERT INTO PartitionTable (MyID, MyDate,name)VALUES (22,GETDATE(),'De Gea');INSERT INTO PartitionTable (MyID, MyDate,name)VALUES (34,GETDATE(),'Moyes');GO

Run the below code to see the details of the partitioned table

USE PartitionDBGOSELECTOBJECT_NAME(idx.object_id) AS TableName ,psh.name AS PartitionSchemeName ,fnc.name AS PartitionFunctionName,part.partition_number AS PartitionNumber ,fg.name AS [Filegroup],rows AS 'No of Records' ,CASE boundary_value_on_right WHEN 1 THEN 'less than'ELSE 'less than or equal to' END AS 'Condition',value AS 'Range' ,part.partition_id AS [Partition Id] FROM sys.partitions partJOIN sys.indexes idxON part.object_id = idx.object_idAND part.index_id = idx.index_id JOIN sys.partition_schemes pshON psh.data_space_id = idx.data_space_idJOINsys.partition_functions fncON fnc.function_id = psh.function_id LEFTJOIN sys.partition_range_values prvON fnc.function_id = prv.function_idAND part.partition_number = prv.boundary_idJOIN sys.destination_data_spaces ddsON dds.partition_scheme_id = psh.data_space_idAND dds.destination_id = part.partition_numberJOIN sys.filegroups fgON dds.data_space_id = fg.data_space_idJOIN (SELECT container_id, sum(total_pages) as total_pagesFROMsys.allocation_units GROUP BY container_id) AS auON au.container_id = part.partition_id JOIN sys.tables t ONpart.object_id = t.object_id WHERE idx.index_id < 2ORDER BY TableName,part.partition_number;GO

Now let us understand how we can remove the partitions from this table. The easiest way to do this is to drop the Clustered index from this table and recreate it on another filegroup.

Step 1: Drop the clustered index from the table

USE [PartitionDB]GODROP INDEX [IX_PartitionTable] ON[dbo].[PartitionTable] WITH ( ONLINE = OFF )GO

Step 2: Re-create the clustered index on another Filegroup. We will use the primary FG as example

USE [PartitionDB]GOCREATE UNIQUE CLUSTERED INDEX [IX_PartitionTable] ON[dbo].[PartitionTable]( [MyID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]GO

Step 3: Verify the state of the partitions by running the below code. You will find that there is only one partition with all the 4 rows in it.

USE PartitionDB
go
SELECT
*
FROM
sys.partitions
WHERE
OBJECT_NAME(OBJECT_ID)=
'partitiontable'
;
GO
You can verify the same via SSMS by performing the following steps:
Step 1: Right click on the table
Step 2: Click on properties
Step 3: Click on Storage
Step 4 : Verify that “Table is partitioned” is false.

I hope this article was helpful in understanding how we can remove Partitioning from table.

转载于:https://www.cnblogs.com/nanfei/p/5537707.html

你可能感兴趣的文章
Vue 自定义指令
查看>>
帆软 控件内容 清除
查看>>
第一页 - 工具的使用(webstorm)
查看>>
The Number of set-hdu-3006
查看>>
ssh 免签登录 亲测可以
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
D3.js 之 d3-shap 简介(转)
查看>>
制作满天星空
查看>>
MyBatis日记(三):戏说MyBatis配置文件
查看>>
类和结构
查看>>
CSS3选择器(二)之属性选择器
查看>>
java面试题-网络架构
查看>>
Openvswitch原理与代码分析(4):网络包的处理过程
查看>>
Openvswitch原理与代码分析(2): ovs-vswitchd的启动
查看>>
Open vSwitch Datapath浅析
查看>>
Openvswitch原理与代码分析(3): openvswitch内核模块的加载
查看>>
OVS架构
查看>>
Linux内核源码目录结构分析
查看>>
RTNETLINK内核与用户空间网络子系统交互机制
查看>>