从带有聚集索引的 SQL Server 表中删除数据期间 B-Tree 是否重新平衡?

jay*_*esh 11 index sql-server optimization

我在 SQL Server 数据库中有一个表,主键上有一个聚集索引。该表有 100 万行。如果我从表中删除 10K 行,在执行删除操作期间索引会被重组吗?

删除操作是存储过程的一部分。一次,可以有多个客户端执行存储过程,但是每次运行都会删除它自己的一组行(由主键唯一标识)。当多个客户端执行该过程时,我会阻塞键锁(U 型)。阻塞锁属于同一个表中的一行,它不属于任何并发运行的事务。不应该有任何阻塞,因为每次运行都试图删除它自己的一组行。锁定升级不会发生,因为它已关闭。

我怀疑,删除操作一定会导致索引重新平衡,因此在重组过程中,它可以对表的任何行进行键锁定。

我真的很感激对此的任何意见。

For*_*est 5

要回答标题中的问题,即 B 树在删除期间是否重新平衡,答案似乎是否定的,至少在下面的最小测试用例中是这样。

以下演示运行最好留给测试环境的命令。

--create table and fill it
DROP TABLE IF EXISTS bunchesofints
CREATE TABLE bunchesofints (
thisisanint INT PRIMARY KEY CLUSTERED,
junkrow CHAR(1000) NOT NULL
)

INSERT dbo.bunchesofints
SELECT TOP 5000
ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) AS thisisanint,
REPLICATE('a',1000) AS junkrow
FROM sys.all_objects a1
CROSS JOIN sys.all_objects a2


--with this query we can see all the non-leaf pages of the b-tree, plus the IAM
SELECT allocated_page_page_id, page_type_desc, page_level, is_allocated, next_page_page_id, previous_page_page_id
FROM sys.dm_db_database_page_allocations(DB_ID(),OBJECT_ID('dbo.bunchesofints'),NULL,NULL,'DETAILED')
WHERE page_type != 1
GO

--Ok, let's delete most of the rows
;WITH CTE AS (
    SELECT TOP (4500) *
    FROM dbo.bunchesofints
    ORDER BY thisisanint DESC
)

DELETE 
FROM CTE
GO

--Hmm, still have 3 non-leaf index pages
SELECT allocated_page_page_id, page_type_desc, page_level, is_allocated, next_page_page_id, previous_page_page_id
FROM sys.dm_db_database_page_allocations(DB_ID(),OBJECT_ID('dbo.bunchesofints'),NULL,NULL,'DETAILED')
WHERE page_type != 1



--So, where are the rows?
--please note the assumption that your test database has a single file.
DECLARE @firstindexpage INT, @lastindexpage INT, @db INT = DB_ID()
SELECT @firstindexpage = MIN(previous_page_page_id), @lastindexpage = MAX(next_page_page_id)
FROM sys.dm_db_database_page_allocations(DB_ID(),OBJECT_ID('dbo.bunchesofints'),NULL,NULL,'DETAILED')
WHERE page_type = 2 AND page_level = 1

DBCC PAGE(@db,1,@firstindexpage,3) WITH TABLERESULTS
DBCC PAGE(@db,1,@lastindexpage,3) WITH TABLERESULTS
Run Code Online (Sandbox Code Playgroud)

该演示表明,删除可能会产生非常不平衡的 B 树,几乎所有数据都位于一侧。