How to Remove Table Partitioning in SQL Server

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.

时间: 2024-11-03 05:41:39

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

在JDBC中传递table参数给SQL server stored procedure

SQL Server JDBC驱动不支持直接传递Table参数给stored procedure 我的做法是先创建一个临时表,将需要插入的数据先插入到临时表里面,然后把这个临时表作为参数,传送给stored procedure.使用了Preparestatement来避免SQL注入问题. 先创建User Defined Table CREATE TYPE UserIdList AS TABLE( userId uniqueidentifier NOT NULL ); 再创建存储过程: CREAT

Migrating Oracle on UNIX to SQL Server on Windows

Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Appendix B: Getting the Best Out of SQL Server 2000 and Windows Appendix C: Baselining Appendix D: Installing Common Drivers and Applications Installing

Microsoft SQL Server Version List(SQL Server 版本)

原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Service Packs (SP), Cumulative Updates (CU), patches, hotfixes and other builds of MS SQL Server 2014, 2012, 2008 R2, 2008, 2005, 2000, 7.0, 6.5 and 6.0 tha

Implementing multi-level trees in MS SQL Server

Introduction Many projects now are in need of management of multi-tree level data. There are some ways to do this, but in this article I will introduce a good way to setup and install multi-tree level structures by using an MS SQL Server database. Ou

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

原文:SQL Server 2008性能故障排查(二)--CPU 承接上一篇:SQL Server 2008性能故障排查(一)--概论 说明一下,CSDN的博客编辑非常不人性化,我在word里面都排好了版,贴上来就乱得不成样了.建议CSDN改进这部分.也请大家关注内容不要关注排版.同时在翻译的过程中本人也整理了一次思路,所以还似乎非常愿意翻译,虽然有点自娱自乐,但是分享给大家也是件好事 CPU 瓶颈: CPU瓶颈可能因为某个负载所需的硬件资源不足而引起.但是过多的CPU使用通常可以通过查询优化(

[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 to get started creating SQL Server databases in Microsoft public cloud environment. With SQL Server in Azure Virtual Machines, you get the full benefit

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

一.使用Transact_SQL创建数据库 Transact_SQL语法如下:  create database database_name   [ on     [primary]  [<filespec> [,...n] ]   ]   [ log on    [<filespec>[,...n]]   ];   <filespec>::=    (      name=logical_file_name      [  ,  newname = new_login

Cacti 监控 SQL Server 数据库图文详解

模板下载: (1)可以直接下载本站发布的 Cnyunwei-Cacti+Nagios 集成全自动安装ISO进行安装,已集成此模板及更多的使用插件模板(2)可以直接去官方论坛下载最新的模板 http://forums.cacti.net/viewtopic.php?f=12&t=38135 php-mssql配置: cacti的机器上必须有php-mssql驱动php -m | grep mssql 查看是否有mssql,如没有安装的话,直接yum安装即可 yum install php-mssq

SQL server中使用临时表存储数据

将查询出来的数据直接用“INTO #临时表名称”的方式完成临时表的创建及数据的插入 SELECT * INTO #temp_NowStatusFROM Test SELECT * FROM #temp_NowStatus --查询临时表中的数据truncate table #temp_NowStatus --清除临时表中的数据--删除临时表if object_id('tempdb..#temp_NowStatus') is not null BEGIN drop table #temp_NowS