SQL Server 中 sys.sysrowsets 的用途是什么?

use*_*976 5 sql-server

我只是想知道 SQL Server 中 sys.sysrowsets 的用法是什么?微软说它“存在于每个数据库中。为索引或堆的每个分区行集包含一行。” 但什么是分区?

另外这个表的 idmajor 和 idminor 是什么意思?我尝试查找表(246486340)的对象 ID,然后执行查询:

Select * from sys.sysrowsets where idmajor = 246486340
Run Code Online (Sandbox Code Playgroud)

结果有两条记录:

rowsetid           ownertype       idmajor idminor  numpart status  fgidfs  rcrows

72057603504865280   1              246486340     1     1        6     0     2582    
72057603612213248   1              246486340     6     1        2     0     2582
Run Code Online (Sandbox Code Playgroud)

但我认为应该只有一个具有该主要 id 的记录。

sql*_*dle 6

它是 sys.partitions 的基础表,它返回与

SELECT * FROM sys.partitions

但什么是分区?

请参阅MSDN 上的分区表和索引

idmajor 是通常称为的列名 object_id

idminor 是 index_id。

现在让我们做一些测试:我使用了CodePlex 的AdventureWorks 2012 。

USE AdventureWorks2012_Data;
GO
DECLARE @MyID int;
SET @MyID = (SELECT OBJECT_ID('HumanResources.Employee',
'U'));
SELECT name, object_id, type_desc
FROM sys.objects
WHERE name = OBJECT_NAME(@MyID);

Output:

name        object_id   type_desc

Employee    1237579447  USER_TABLE
Run Code Online (Sandbox Code Playgroud)

sys.partitions现在让我们查询视图:

SELECT partion_id, object_id, index_id FROM sys.partitions 
WHERE object_id = '1237579447'

Output:

partition_id      Object_id      index_id

72057594045136896   1237579447  1

72057594050510848   1237579447  2

72057594050576384   1237579447  3

72057594050641920   1237579447  5

72057594050707456   1237579447  6

72057594050772992   1237579447  7
Run Code Online (Sandbox Code Playgroud)

如果我们看一下sys.indexes视图:

SELECT object_id, name, index_id from sys.indexes where object_id = '1237579447'

Output:

Object_id           name                                index_id

1237579447  PK_Employee_BusinessEntityID                    1

1237579447  IX_Employee_OrganizationNode                    2

1237579447  IX_Employee_OrganizationLevel_OrganizationNode  3

1237579447  AK_Employee_LoginID                             5

1237579447  AK_Employee_NationalIDNumber                    6

1237579447  AK_Employee_rowguid                             7
Run Code Online (Sandbox Code Playgroud)

在上面的输出中, index_id 只是索引的 id。1 用于聚集索引,其他 (2-7) 用于其他非聚集索引。

表下所有索引的 object_id 与该表的 object_id 相同。

我们来看看sys.sysrowsets表:

USE AdventureWorks2012_data
GO
SELECT rowsetid, idmajor, idminor from sys.sysrowsets 
WHERE idmajor = '1237579447'
Run Code Online (Sandbox Code Playgroud)

我使用HumanResources.Employee表的 object_id来过滤数据。

输出:

rowsetid               idmajor         idminor
-------------------- ----------- -----------
   72057594045136896  1237579447           1

   72057594050510848  1237579447           2

   72057594050576384  1237579447           3

   72057594050641920  1237579447           5

   72057594050707456  1237579447           6

   72057594050772992  1237579447           7
Run Code Online (Sandbox Code Playgroud)

由此可以清楚地看出,object_idoridmajor对表及其下的所有索引都是相同的,idminor只不过index_id是一个索引。