Her er et par måder at returnere partitionsoplysninger for en tabel i SQL Server.
- Du kan bruge
sys.partitions
systemkatalogvisning for at returnere partitionsoplysninger for en tabel og de fleste slags visninger. - Du kan bruge
sys.dm_db_partition_stats
system dynamisk administrationsvisning for at returnere side- og rækkeoplysninger for hver partition i den aktuelle database.
Hvis en tabel eller et indeks ikke er blevet partitioneret, vil disse visninger stadig returnere partitionsoplysninger (med et partitionsnummer af 1
). Dette skyldes, at alle tabeller og indekser i SQL Server indeholder mindst én partition, uanset om de er eksplicit partitioneret eller ej.
sys.partitions
sys.partitions
view indeholder en række for hver partition af alle tabellerne og de fleste typer indekser i databasen. Særlige indekstyper såsom fuldtekst, rumlig og XML er ikke inkluderet.
Her er et eksempel til at demonstrere.
SELECT *
FROM sys.partitions
WHERE object_id = OBJECT_ID('Movies');
Resultat:
+-------------------+-------------+------------+--------------------+-------------------+--------+---------------------------+--------------------+-------------------------+ | partition_id | object_id | index_id | partition_number | hobt_id | rows | filestream_filegroup_id | data_compression | data_compression_desc | |-------------------+-------------+------------+--------------------+-------------------+--------+---------------------------+--------------------+-------------------------| | 72057594049986560 | 91147370 | 1 | 1 | 72057594049986560 | 0 | 0 | 0 | NONE | | 72057594050052096 | 91147370 | 1 | 2 | 72057594050052096 | 100 | 0 | 0 | NONE | | 72057594050117632 | 91147370 | 1 | 3 | 72057594050117632 | 3979 | 0 | 0 | NONE | | 72057594050183168 | 91147370 | 1 | 4 | 72057594050183168 | 0 | 0 | 0 | NONE | +-------------------+-------------+------------+--------------------+-------------------+--------+---------------------------+--------------------+-------------------------+
Vi kan se, at der er fire partitioner til denne tabel.
Her er den igen, denne gang ved at bruge lodret output (for at spare dig for at skulle rulle sidelæns):
-[ RECORD 1 ]------------------------- partition_id | 72057594049986560 object_id | 91147370 index_id | 1 partition_number | 1 hobt_id | 72057594049986560 rows | 0 filestream_filegroup_id | 0 data_compression | 0 data_compression_desc | NONE -[ RECORD 2 ]------------------------- partition_id | 72057594050052096 object_id | 91147370 index_id | 1 partition_number | 2 hobt_id | 72057594050052096 rows | 100 filestream_filegroup_id | 0 data_compression | 0 data_compression_desc | NONE -[ RECORD 3 ]------------------------- partition_id | 72057594050117632 object_id | 91147370 index_id | 1 partition_number | 3 hobt_id | 72057594050117632 rows | 3979 filestream_filegroup_id | 0 data_compression | 0 data_compression_desc | NONE -[ RECORD 4 ]------------------------- partition_id | 72057594050183168 object_id | 91147370 index_id | 1 partition_number | 4 hobt_id | 72057594050183168 rows | 0 filestream_filegroup_id | 0 data_compression | 0 data_compression_desc | NONE
sys.dm_db_partition_stats
sys.dm_db_partition_stats view
returnerer oplysninger om side- og rækkeantal for hver partition i den aktuelle database. Men hvis du kun er interesseret i ét bord, kan du også indsnævre det til netop det bord.
Her er et eksempel.
SELECT *
FROM sys.dm_db_partition_stats
WHERE object_id = OBJECT_ID('Movies');
Resultat:
+-------------------+-------------+------------+--------------------+--------------------------+--------------------------+------------------------------+-----------------------+---------------------------+--------------------------------+------------------------------------+-------------------+-----------------------+-------------+ | partition_id | object_id | index_id | partition_number | in_row_data_page_count | in_row_used_page_count | in_row_reserved_page_count | lob_used_page_count | lob_reserved_page_count | row_overflow_used_page_count | row_overflow_reserved_page_count | used_page_count | reserved_page_count | row_count | |-------------------+-------------+------------+--------------------+--------------------------+--------------------------+------------------------------+-----------------------+---------------------------+--------------------------------+------------------------------------+-------------------+-----------------------+-------------| | 72057594049986560 | 91147370 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 72057594050052096 | 91147370 | 1 | 2 | 1 | 2 | 9 | 0 | 0 | 0 | 0 | 2 | 9 | 100 | | 72057594050117632 | 91147370 | 1 | 3 | 13 | 15 | 33 | 0 | 0 | 0 | 0 | 15 | 33 | 3979 | | 72057594050183168 | 91147370 | 1 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +-------------------+-------------+------------+--------------------+--------------------------+--------------------------+------------------------------+-----------------------+---------------------------+--------------------------------+------------------------------------+-------------------+-----------------------+-------------+
Her bruger den igen lodret output:
-[ RECORD 1 ]------------------------- partition_id | 72057594049986560 object_id | 91147370 index_id | 1 partition_number | 1 in_row_data_page_count | 0 in_row_used_page_count | 0 in_row_reserved_page_count | 0 lob_used_page_count | 0 lob_reserved_page_count | 0 row_overflow_used_page_count | 0 row_overflow_reserved_page_count | 0 used_page_count | 0 reserved_page_count | 0 row_count | 0 -[ RECORD 2 ]------------------------- partition_id | 72057594050052096 object_id | 91147370 index_id | 1 partition_number | 2 in_row_data_page_count | 1 in_row_used_page_count | 2 in_row_reserved_page_count | 9 lob_used_page_count | 0 lob_reserved_page_count | 0 row_overflow_used_page_count | 0 row_overflow_reserved_page_count | 0 used_page_count | 2 reserved_page_count | 9 row_count | 100 -[ RECORD 3 ]------------------------- partition_id | 72057594050117632 object_id | 91147370 index_id | 1 partition_number | 3 in_row_data_page_count | 13 in_row_used_page_count | 15 in_row_reserved_page_count | 33 lob_used_page_count | 0 lob_reserved_page_count | 0 row_overflow_used_page_count | 0 row_overflow_reserved_page_count | 0 used_page_count | 15 reserved_page_count | 33 row_count | 3979 -[ RECORD 4 ]------------------------- partition_id | 72057594050183168 object_id | 91147370 index_id | 1 partition_number | 4 in_row_data_page_count | 0 in_row_used_page_count | 0 in_row_reserved_page_count | 0 lob_used_page_count | 0 lob_reserved_page_count | 0 row_overflow_used_page_count | 0 row_overflow_reserved_page_count | 0 used_page_count | 0 reserved_page_count | 0 row_count | 0
Denne eksempeltabel er blevet partitioneret ved hjælp af den Microsoft anbefalede metode til at holde tomme partitioner i begge ender af partitionsområdet. Dette sikrer, at eventuelle fremtidige partitionsopdelinger/sammenfletninger ikke forårsager nogen uventet databevægelse.