这里和大家分享一下SqlServer 分区遇到的问题 How to Remove (Undo) Table Partitioning

这里和大家分享一下SqlServer 分区遇到的问题 How to Remove (Undo) Table Partitioning,第1张

概述The Problem - We have two partitioned tables (PartitionTable1 & PartitionTable2) split across four filegroups.  We need to remove partitioning from the tables, remove the four files and filegroups, an The Problem - We have two partitioned tables (Partitiontable1 & Partitiontable2) split across four filegroups.  We need to remove partitioning from the tables,remove the four files and filegroups,and then move all data to the PRIMARY filegroup without losing any data.
Sample Database - Start by creating a test database with a few filegroups and add some data files to those filegroups.
USE master ; GO -- Create a test database. CREATE DATABASE PartitionTest     ON PRIMARY (          name = N'PartitionTest'         , filename = N'D:\MSsql11.TEST1\MSsql\DATA\PartitionTest.mdf'         , SIZE = 25 MB , fileGROWTH = 25 MB )     LOG ON (          name = N'PartitionTest_log'         , filename = N'D:\MSsql11.TEST1\MSsql\DATA\PartitionTest_log.ldf'         , fileGROWTH = 25 MB ); GO
USE PartitionTest ; GO -- Add four new filegroups to the PartitionTest database. ALTER DATABASE PartitionTest ADD fileGROUP PartitionFG1 ; GO ALTER DATABASE PartitionTest ADD fileGROUP PartitionFG2 ; GO ALTER DATABASE PartitionTest ADD fileGROUP PartitionFG3 ; GO ALTER DATABASE PartitionTest ADD fileGROUP PartitionFG4 ; GO
-- Adds one file for each filegroup. ALTER DATABASE PartitionTest     ADD file     (         name = Partitionfile1 ,         filename = 'D:\MSsql11.TEST1\MSsql\DATA\Partitionfile1.ndf' ,         SIZE = 25 MB , MAXSIZE = 100 MB , fileGROWTH = 5 MB     )     TO fileGROUP PartitionFG1 ; GO ALTER DATABASE PartitionTest     ADD file     (         name = Partitionfile2 ,         filename = 'D:\MSsql11.TEST1\MSsql\DATA\Partitionfile2.ndf' , fileGROWTH = 5 MB     )     TO fileGROUP PartitionFG2 ; GO ALTER DATABASE PartitionTest     ADD file     (         name = Partitionfile3 ,         filename = 'D:\MSsql11.TEST1\MSsql\DATA\Partitionfile3.ndf' , fileGROWTH = 5 MB     )     TO fileGROUP PartitionFG3 ; GO ALTER DATABASE PartitionTest     ADD file     (         name = Partitionfile4 ,         filename = 'D:\MSsql11.TEST1\MSsql\DATA\Partitionfile4.ndf' , fileGROWTH = 5 MB     )     TO fileGROUP PartitionFG4 ; GO
Create our partition function and then our partition scheme.
-- Creates a partition function called myRangePF1 that will partition a table into four partitions CREATE PARTITION FUNCTION myRangePF1 ( int )     AS RANGE left FOR VALUES (500 , 1000 , 1500 ); GO


-- Creates a partition scheme called myRangePS1 that applIEs myRangePF1 to the four filegroups created above CREATE PARTITION SCHEME myRangePS1     AS PARTITION myRangePF1     TO ( PartitionFG1 , PartitionFG2 , PartitionFG3 , PartitionFG4 ); GO
Create the partitioned tables on the partition scheme; one (Partitiontable1) with a clustered index and one (Partitiontable2) with a non-clustered index.

-- Creates a partitioned table called Partitiontable1 with a clustered index CREATE table Partitiontable1 ( col1 int IDENTITY (1 ,1 ), col2 datetime , col3 char (8000 ))     ON myRangePS1 ( col1 ); GO
CREATE CLUSTERED INDEX [PK_col1] ON [dbo] . [Partitiontable1]     ( [col1] ASC ) ON [myRangePS1] ( [col1] ); GO
-- Creates a partitioned table called Partitiontable2 with a nonclustered index CREATE table Partitiontable2 ( col1 int IDENTITY (1 , col3 char (8000 ))     ON myRangePS1 ( col1 ); GO
CREATE NONCLUSTERED INDEX [IX_col2] ON [dbo] . [Partitiontable2]     ( [col1], [col2] ASC ) ON [myRangePS1] ( [col1] ); GO
Now add 2000 rows of dummy data to each table.  The random date generator code is courtesy of Latif Khan.
-- Insert dummy data. INSERT Partitiontable1 ( col2 , col3 ) SELECT  CAST ( CAST ( GETDATE () AS INT ) -2000 * RAND ( CAST ( CAST ( NEWID () AS BINARY (8 )) AS INT )) AS DATETIME ), REPliCATE ( '1' ,8000 ); GO 2000

INSERT Partitiontable2 ( col2 , REPliCATE ( '2' ,8000 ); GO 2000
Let's query the sys.partitions table and see what we have created.
-- Get partition information. SELECT      SCHEMA_name ( t . schema_ID ) AS Schemaname     , OBJECT_name ( i . object_ID ) AS Objectname     , p . partition_number AS PartitionNumber     , fg . name AS filegroup_name     , rows AS 'Rows'     , au . total_pages AS 'TotalDataPages'     , CASE boundary_value_on_right         WHEN 1 THEN 'less than'         ELSE 'less than or equal to'      END AS 'Comparison'     , value AS 'ComparisonValue'     , p . data_compression_desc AS 'DataCompression'     , p . partition_ID FROM sys . partitions p     JOIN sys . indexes i ON p . object_ID = i . object_ID AND  p . index_ID = i . index_ID     JOIN sys . partition_schemes ps ON ps . data_space_ID = i . data_space_ID     JOIN sys . partition_functions f ON f . function_ID = ps . function_ID     left JOIN sys . partition_range_values rv ON f . function_ID = rv . function_ID AND p . partition_number = rv . boundary_ID     JOIN sys . destination_data_spaces dds ON dds . partition_scheme_ID = ps . data_space_ID AND dds . destination_ID = p . 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 = p . partition_ID      JOIN sys . tables t ON p . object_ID = t . object_ID WHERE i . index_ID < 2 ORDER BY Objectname , p . partition_number ; GO
Here we can see both Partitiontable1 and Partitiontable2 are evenly split with 500 rows in each of the four partitions and each in a separate filegroup. 


Within SSMS,you can also see each table is showing the partition scheme and the four partitions.




Solution for Partitiontable1 - This table has a clustered index which makes our solution pretty easy.
Since we have a partitioned clustered index,we can remove partitioning from this table by simply executing a single statement; CREATE INDEX using the DROP_EXISTING option and specifying a different filegroup.  This will drop the current partitioned index (which includes the data) and recreate it on the PRIMARY filegroup all within a single command.

-- Quick and easy way to unpartition and move it. CREATE CLUSTERED INDEX [PK_col1]     ON [dbo] . [Partitiontable1] ( [col1] )     WITH ( DROP_EXISTING = ON )     ON [PRIMARY] ; GO
Now query the sys.partitions DMV again and you will see Partitiontable1 no longer shows up and only Partitiontable2 is remaining.

Once again in SSMS,you can will see Partitiontable1 Now resIDes on the PRIMARY filegroup and its data still remains intact.

Solution for Partitiontable2 - We can't use the prevIoUs index trick on the this table because it doesn't have a clustered index.  For this solution,we'll need to use a few ALTER commands such as MERGE RANGE,NEXT USED,SPliT RANGE,and SWITCH.

First we need to use the ALTER PARTITION FUNCTION MERGE command to combine all of the four partitions into a single partition.  The MERGE RANGE command removes the boundary point between the specifIEd partitions.

-- Merge all partitions into a single partition. ALTER PARTITION FUNCTION myRangePF1 () MERGE RANGE (500 ); GO ALTER PARTITION FUNCTION myRangePF1 () MERGE RANGE (1000 ); GO ALTER PARTITION FUNCTION myRangePF1 () MERGE RANGE (1500 ); GO
query the sys.partitions DMV again,and you will see that all 2000 rows have been combined,or merged,into a single partition and Now resIDe on the PartitionFG4 filegroup.

Next,we need to use ALTER PARTITION SCHEME NEXT USED to specify the PRIMARY filegroup as the next partition.

-- Create next partition as PRIMARY. ALTER PARTITION SCHEME myRangePS1 NEXT USED [PRIMARY] ; GO
Then we need to use ALTER PARTITION FUNCTION SPliT RANGE using a partition value that is larger than the maximum value of your partition column.  In our example,since we're doing a RANGE left partition then specifying any value greater than or equal to 2000 will do the trick.  The SPliT RANGE command will create a new boundary in the partitioned table.

-- Split the single partition into 2 separates ones to push all data to the PRIMARY FG. ALTER PARTITION FUNCTION myRangePF1 () SPliT RANGE (2000 ); GO
query the sys.partitions DMV once again.  You can see that Partitiontable2 is still partitioned into two partitions,but all 2000 rows Now resIDe in the PRIMARY filegroup.

At this point we're only half way done.  Now we need to create a non-partitioned table in the PRIMARY filegroup that matches the Partitiontable2 in every way,including any data types,constraints,etc.  This new table will only be used as a temporary holding location for the data.

-- Create a new temporary non-partitioned table. CREATE table NonPartitiontable ( col1 int IDENTITY (1 , col3 char (8000 ))     ON [PRIMARY] ; GO
CREATE NONCLUSTERED INDEX [IX_col2] ON [dbo] . [NonPartitiontable]     ( [col1] , [col2] ASC ) ON [PRIMARY] ; GO
Next we'll use the ALTER table SWITCH command to move the 2000 rows of data into the NonPartitiontable.

-- Switch the partitioned data into the temporary table. ALTER table Partitiontable2 SWITCH PARTITION 1 TO NonPartitiontable ; GO
query the sys.partitions DMV again to see there are Now zero rows in the Partitiontable2.

The SWITCH command is very efficIEnt because it's just making a Metadata change.  Under the covers,no data is actually being moved; it's just reassigning the partition_ID of Partitiontable2 to the the NonPartitiontable object_ID.  If you want to really see the undercover action,then you can run this script before and after the SWITCH command to see the 2000 rows of data never leave the same partition_IDs. Our data has never left partition_ID 72057594040156160.

SELECT      o . name     , o . object_ID     , p . index_ID     , p . partition_ID     , p . partition_number     , p . rows FROM sys . objects o     JOIN sys . partitions p ON o . object_ID = p . object_ID WHERE o . name IN ( 'Partitiontable2' , 'NonPartitiontable' ) ORDER BY o . name , p . partition_number ; GO
Before:

After:


Now that all the data has been moved to the temporary table,we can drop Partitiontable2 and rename the temporary table back to the original name.

-- Drop the partitioned table. DROP table Partitiontable2 ; GO
-- Rename the temporary table to the original name. EXEC sp_rename 'dbo.NonPartitiontable' , 'Partitiontable2' , 'OBJECT' ; GO
At this point the Partitiontable2 is no longer partitioned.

Partitioning has Now been completely removed from both Partitiontable1 and Partitiontable2.  We can drop the remaining parts (partition schema,partition function,files,and filegroups) of partitioning to complete the clean up.

-- Remove the partition scheme,function,and filegroups. DROP PARTITION SCHEME myRangePS1 ; GO DROP PARTITION FUNCTION myRangePF1 ; GO ALTER DATABASE [PartitionTest] REMOVE file Partitionfile1 ; ALTER DATABASE [PartitionTest] REMOVE file Partitionfile2 ; ALTER DATABASE [PartitionTest] REMOVE file Partitionfile3 ; ALTER DATABASE [PartitionTest] REMOVE file Partitionfile4 ; GO ALTER DATABASE [PartitionTest] REMOVE fileGROUP PartitionFG1 ; ALTER DATABASE [PartitionTest] REMOVE fileGROUP PartitionFG2 ; ALTER DATABASE [PartitionTest] REMOVE fileGROUP PartitionFG3 ; ALTER DATABASE [PartitionTest] REMOVE fileGROUP PartitionFG4 ; GO 总结

以上是内存溢出为你收集整理的这里和大家分享一下SqlServer 分区遇到的问题 How to Remove (Undo) Table Partitioning全部内容,希望文章能够帮你解决这里和大家分享一下SqlServer 分区遇到的问题 How to Remove (Undo) Table Partitioning所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1159478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存