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 master
GO
CREATE DATABASE PartitionDB
ON 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;
GO
ALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG2;
GO
ALTER DATABASE PartitionDB ADD FILEGROUP PartitionFG3;
GO
ALTER 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]
GO
CREATE TABLE PartitionTable
(
MyID int NOT NULL,
MyDate datetime NULL,
Name varchar(50) NULL
) ON PartScheme1(MyID)
GO
USE PartitionDB
go
CREATE UNIQUE CLUSTERED INDEX IX_PartitionTable
ON PartitionTable(MyID)
ON PartScheme1 (MyID);
GO USE PartitionDB
go
INSERT 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 PartitionDB
GO
SELECT
OBJECT_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 part
JOIN sys.indexes idx
ON part.object_id = idx.object_id
AND part.index_id = idx.index_id JOIN sys.partition_schemes psh
ON psh.data_space_id = idx.data_space_id
JOIN
sys.partition_functions fnc
ON fnc.function_id = psh.function_id LEFT
JOIN sys.partition_range_values prv
ON fnc.function_id = prv.function_id
AND part.partition_number = prv.boundary_id
JOIN sys.destination_data_spaces dds
ON dds.partition_scheme_id = psh.data_space_id
AND dds.destination_id = part.partition_number
JOIN sys.filegroups fg
ON dds.data_space_id = fg.data_space_id
JOIN (SELECT container_id, sum(total_pages) as total_pages
FROM
sys.allocation_units GROUP BY container_id) AS au
ON au.container_id = part.partition_id JOIN sys.tables t ON
part.object_id = t.object_id WHERE idx.index_id < 2
ORDER 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]
GO
DROP 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]
GO
CREATE 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.

How to Remove Table Partitioning in SQL Server的更多相关文章

  1. Generate class from database table How can i generate a class from a table at a SQL Server?

    Set @TableName to the name of your table. declare @TableName sysname = 'TableName' declare @Result v ...

  2. Migrating Oracle on UNIX to SQL Server on Windows

    Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...

  3. SQL Server 2008性能故障排查(二)——CPU

    原文:SQL Server 2008性能故障排查(二)--CPU 承接上一篇:SQL Server 2008性能故障排查(一)--概论 说明一下,CSDN的博客编辑非常不人性化,我在word里面都排好 ...

  4. [SQL in Azure] Getting Started with SQL Server in Azure Virtual Machines

    This topic provides guidelines on how to sign up for SQL Server on a Azure virtual machine and how t ...

  5. Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]

    http://sqlserverbuilds.blogspot.jp/   What version of SQL Server do I have? This unofficial build ch ...

  6. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...

  7. Sql Server之使用T_SQL创建,修改,查看数据库信息

    一.使用Transact_SQL创建数据库 Transact_SQL语法如下:  create database database_name   [ on     [primary]  [<fi ...

  8. SQL Server索引进阶:第十二级,创建,修改,删除

    在第十级中我们看到了索引的内部结构,在第十一级中我们看到了平衡树结构潜在的负面影响:索引碎片.有了索引内部结构的知识,我们可以检查在执行数据定义语句和数据操作语句的时候,都发生了什么.在本级中我们介绍 ...

  9. SQL Server Join方式

    原文:SQL Server Join方式 0.参考文献 Microsoft SQL Server企业级平台管理实践 看懂SqlServer查询计划 1.测试数据准备 参考:Sql Server中的表访 ...

随机推荐

  1. android densityDpi 的由来

    ---恢复内容开始--- 今天做屏幕适配的时候,发现一个奇怪的现象: HTC D820u/ 红米Note/HONOR H30-L02 /Coolpad 8297-T01 4款手机的分辨率均为 1280 ...

  2. php实现函数重载

    java..net等强类型预言中都有方法重载,但是PHP是弱类型语言,不能确定参数的类型, 而且如果php定义的方法接收一个参数,调用的时候传入多个也不会有问题,所以不能进行重载. 但是我们可以通过p ...

  3. Objective-C如何对内存管理的?

    Objective-C的内存管理主要有三种方式ARC(自动内存计数).手动内存计数.内存池. 1. (Garbage Collection)自动内存计数:这种方式和java类似,在你的程序的执行过程中 ...

  4. Java网络编程及安全

    一.实验内容: 1.运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成代码,一人加密后通过TCP发送: 注:加密使用AES或者 ...

  5. HP T505恢复出厂系统

    1.制作usb启动U盘. ------ 从HP网站上下载,或者找供应商提供 2.按F11,从U盘启动进去,会自动执行安装,等待完成即可以.

  6. Web Deploy安装时显示Web Management Service无法启动

      在安装显示如题错误,看了日志: IISWMSVC_STARTUP_UNABLE_TO_READ_CERTIFICATE 无法读取带有指纹"3f60e39108a7e4c54f671b75 ...

  7. C文件的格式化工具(astyle)

    工具简介 astyle用于格式化 C,C++,C#, Java等文件,方便将代码风格转变成预期的效果.如改变缩进方式.括号风格等等. 工具的安装 环境 ubuntu 14.04 $apt-get in ...

  8. android修改debug keystore文件使之和正式keystore sha1一致

    转自:http://blog.k-res.net/archives/1671.html Android应用开发接入各种SDK时会发现,有很多SDK是需要靠package name和keystore的指 ...

  9. HDU 1285 确定比赛排名 (数组实现 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    ...

  10. Rxjava的基本使用

    gradle配置: compile 'io.reactivex:rxjava:1.0.9' 观察者: Observer<String> observer = new Observer< ...