如何从表中删除分区?

时间:2019-04-18 14:34:00

标签: sql-server tsql partitioning

我设法将分区添加到表(日志)中,但是需要创建回滚脚本以防万一需要将其删除。不幸的是,这现在失败了,并且由于在回滚脚本途中失败而导致Logs现在没有主键,并且当我收到错误消息时,我无法将其添加回去...

  

列'SuperLogId'是索引'PK__Logs__0E6B88F2'的分区列。唯一索引的分区列必须是索引键的子集。

尝试运行此代码时

ALTER TABLE dbo.Logs
ADD PRIMARY KEY CLUSTERED (Id ASC)

因此,我尝试按照本指南(https://www.patrickkeisler.com/2013/01/how-to-remove-undo-table-partitioning.html)进行操作,最终不得不编写此代码来生成脚本以合并所有动态创建的分区。

DECLARE @partitionsTable dbo.NVarCharCollectionTableType --User-defined table type to hold a collection of NVarChars.

INSERT INTO @partitionsTable
SELECT CONCAT('ALTER PARTITION FUNCTION Logs_SuperLogId_PartitionFunction() MERGE RANGE (', CONVERT(NVARCHAR, [Value]), ')')
FROM SYS.PARTITION_SCHEMES
INNER JOIN SYS.PARTITION_FUNCTIONS ON PARTITION_FUNCTIONS.FUNCTION_ID = PARTITION_SCHEMES.FUNCTION_ID
INNER JOIN SYS.PARTITION_RANGE_VALUES ON PARTITION_RANGE_VALUES.FUNCTION_ID = PARTITION_FUNCTIONS.FUNCTION_ID
WHERE PARTITION_SCHEMES.Name = 'Logs_SuperLogId_PartitionScheme'
AND PARTITION_FUNCTIONS.Name = 'Logs_SuperLogId_PartitionFunction'
ORDER BY [Value] ASC

DECLARE @statement NVARCHAR(MAX)

SELECT @statement =
    CASE
        WHEN @statement IS NULL
        THEN CAST([Text] AS NVARCHAR(MAX))
        ELSE CONCAT(@statement, '; ', [Text])
    END
    FROM @partitionsTable
    ORDER BY [Text] ASC

SELECT @statement

EXECUTE SP_EXECUTESQL @statement

ALTER PARTITION SCHEME Logs_SuperLogId_PartitionScheme NEXT USED [PRIMARY]

指南建议这样做会有所帮助,但没有帮助!尝试重新添加主键时仍然出现相同的错误,而尝试删除分区功能和分区方案时仍然出现这些错误!

DROP PARTITION SCHEME Logs_SuperLogId_PartitionScheme
  

当前使用分区方案“ Logs_SuperLogId_PartitionScheme”对一个或多个表进行分区。

DROP PARTITION FUNCTION CatLogs_CatSessionLogId_PartitionFunction
  

一个或多个分区方案正在使用分区函数'Logs_SuperLogId_PartitionFunction'。

如何继续使用我的分区方案?为什么我不能摆脱它并且不再使用它?我只想对Logs表进行分区,然后重新添加其原始的集群主键(我以前必须删除该集群主键,并用非集群主键替换,以使SuperLogId上具有集群索引,因此可以对其进行分区)。

更新

我能够使用以下技巧从表中删除分区,但仍然无法删除分区方案或功能。

--HACK: Dummy Index to disassociate the table from the partitioning scheme.
CREATE CLUSTERED INDEX IX_Logs_Id ON dbo.Logs(Id) ON [Primary]

--Now that the table has been disassociated with the partition, this dummy index can be dropped.
DROP INDEX IX_Logs_Id ON dbo.Logs

此后,我运行了此脚本来找出哪些表正在使用数据库中的任何分区,并且按预期不返回任何内容。

SELECT DISTINCT TABLES.NAME
FROM SYS.PARTITIONS
INNER JOIN SYS.TABLES ON PARTITIONS.OBJECT_ID = TABLES.OBJECT_ID
WHERE PARTITIONS.PARTITION_NUMBER <> 1

这使我可以重新添加主键,但是在尝试删除分区方案时仍然出现The partition scheme "Logs_SuperLogId_PartitionScheme" is currently being used...错误。

基于Microsoft文档(https://docs.microsoft.com/en-us/sql/t-sql/statements/drop-partition-scheme-transact-sql?view=sql-server-2017),如果没有表或分区索引引用该分区方案,则该分区方案应为可删除的。因此,我随后还运行了该脚本以使用它来检查索引...

SELECT DISTINCT indexes.NAME
FROM SYS.PARTITIONS
INNER JOIN SYS.indexes ON indexes.index_id = partitions.index_id
WHERE PARTITIONS.PARTITION_NUMBER <> 1

...它什么也没返回!那么到底是什么在使用我的分区方案?!

1 个答案:

答案 0 :(得分:1)

我能够使用以下代码从其表中删除该分区。

--HACK: Dummy Index to disassociate the table from the partitioning scheme.
CREATE CLUSTERED INDEX IX_Logs_Id ON dbo.Logs(Id) ON [Primary]

--Now that the table has been disassociated with the partition, this dummy index can be dropped.
DROP INDEX IX_Logs_Id ON dbo.Logs

然后,使用以下脚本,发现两个索引仍保留在分区方案中。

SELECT SCHEMA_NAME(B.SCHEMA_ID) SCHEMANAME, B.NAME TABLENAME, C.INDEX_ID, C.NAME INDEXNAME, C.TYPE_DESC,
A.PARTITION_NUMBER, D.NAME DATASPACENAME, F.NAME SCHEMADATASPACENAME,
H.VALUE DATARANGEVALUE, A.ROWS,
J.IN_ROW_RESERVED_PAGE_COUNT, J.LOB_RESERVED_PAGE_COUNT,
J.IN_ROW_RESERVED_PAGE_COUNT+J.LOB_RESERVED_PAGE_COUNT TOTALPAGECOUNT,
I.LOCATION
FROM SYS.PARTITIONS A
JOIN SYS.TABLES B ON A.OBJECT_ID = B.OBJECT_ID
JOIN SYS.INDEXES C ON A.OBJECT_ID = C.OBJECT_ID AND A.INDEX_ID = C.INDEX_ID
JOIN SYS.DATA_SPACES D ON C.DATA_SPACE_ID = D.DATA_SPACE_ID
LEFT JOIN SYS.DESTINATION_DATA_SPACES E ON E.PARTITION_SCHEME_ID = D.DATA_SPACE_ID AND A.PARTITION_NUMBER = E.DESTINATION_ID
LEFT JOIN SYS.DATA_SPACES F ON E.DATA_SPACE_ID = F.DATA_SPACE_ID 
LEFT JOIN SYS.PARTITION_SCHEMES G ON D.NAME = G.NAME
LEFT JOIN SYS.PARTITION_RANGE_VALUES H ON G.FUNCTION_ID = H.FUNCTION_ID AND H.BOUNDARY_ID = A.PARTITION_NUMBER
LEFT JOIN (SELECT DISTINCT DATA_SPACE_ID, LEFT(PHYSICAL_NAME, 1) LOCATION FROM SYS.DATABASE_FILES) I ON I.DATA_SPACE_ID = ISNULL(F.DATA_SPACE_ID, D.DATA_SPACE_ID)
LEFT JOIN SYS.DM_DB_PARTITION_STATS J ON J.OBJECT_ID = A.OBJECT_ID AND J.INDEX_ID = A.INDEX_ID AND J.PARTITION_NUMBER = A.PARTITION_NUMBER
ORDER BY 1, 2, 3, A.PARTITION_NUMBER

我要做的就是删除引用分区方案的两个索引,然后允许我删除分区方案,然后删除分区功能。