1
0
mirror of https://github.com/alex-bochkov/ssms-addin.git synced 2025-11-23 22:04:53 +02:00
Files
ssms-addin/QueryTemplates/Common Scripts/partitions-stats.sql

66 lines
2.9 KiB
MySQL
Raw Permalink Normal View History

2017-09-11 07:52:50 -07:00
--paritioned table and index details
SELECT
2018-07-17 11:40:08 -07:00
SCHEMA_NAME(o.schema_id) AS SchemaName,
2017-09-11 07:52:50 -07:00
OBJECT_NAME(p.object_id) AS ObjectName,
2020-04-29 14:06:52 -07:00
i.name AS IndexName,
2017-09-11 07:52:50 -07:00
p.index_id AS IndexID,
2020-04-29 14:06:52 -07:00
--ds.name AS PartitionScheme,
--pf.name AS PartitionFunction,
2022-02-23 10:29:01 -07:00
--CONCAT('ALTER PARTITION FUNCTION ', pf.name, '() MERGE RANGE (''', cast(prv_left.value as datetime), ''')') AS PartitionFunctionMerge
2017-09-11 07:52:50 -07:00
p.partition_number AS PartitionNumber,
2020-04-29 14:06:52 -07:00
--fg.name AS FileGroupName,
2017-09-11 07:52:50 -07:00
prv_left.value AS LowerBoundaryValue,
prv_right.value AS UpperBoundaryValue,
2020-04-29 14:06:52 -07:00
--CASE pf.boundary_value_on_right WHEN 1 THEN 'RIGHT' ELSE 'LEFT' END AS [Range],
FORMAT(p.rows, 'N0') AS [Rows],
2019-03-19 11:29:57 -07:00
p.[data_compression_desc] AS [Compression]
2017-09-11 07:52:50 -07:00
FROM sys.partitions AS p
2020-04-29 11:58:09 -07:00
JOIN sys.indexes AS i ON i.object_id = p.object_id AND i.index_id = p.index_id
JOIN sys.tables AS o ON i.object_id = o.object_id
JOIN sys.data_spaces AS ds ON ds.data_space_id = i.data_space_id
JOIN sys.partition_schemes AS ps ON ps.data_space_id = ds.data_space_id
JOIN sys.partition_functions AS pf ON pf.function_id = ps.function_id
JOIN sys.destination_data_spaces AS dds2 ON dds2.partition_scheme_id = ps.data_space_id AND dds2.destination_id = p.partition_number
JOIN sys.filegroups AS fg ON fg.data_space_id = dds2.data_space_id
LEFT JOIN sys.partition_range_values AS prv_left ON ps.function_id = prv_left.function_id AND prv_left.boundary_id = p.partition_number - 1
LEFT JOIN sys.partition_range_values AS prv_right ON ps.function_id = prv_right.function_id AND prv_right.boundary_id = p.partition_number
2017-09-11 07:52:50 -07:00
WHERE
2020-04-29 14:06:52 -07:00
p.index_id = 1 /* only show clustered index */
2019-03-19 11:29:57 -07:00
--AND OBJECT_NAME(p.object_id) = ''
2021-05-31 11:51:51 -06:00
--AND SCHEMA_NAME(o.schema_id) = ''
2020-04-29 11:58:09 -07:00
--AND i.[name] = ''
2019-03-19 11:29:57 -07:00
--AND p.partition_number = 0
2021-05-31 11:51:51 -06:00
--AND p.[rows] > 0
2017-09-11 07:52:50 -07:00
ORDER BY
2020-04-29 11:58:09 -07:00
ObjectName, IndexID, PartitionNumber
2020-03-14 14:20:04 -07:00
--non-partitioned tables/indexes
SELECT
SCHEMA_NAME(o.schema_id) AS SchemaName,
OBJECT_NAME(p.object_id) AS ObjectName,
i.name AS IndexName,
p.index_id AS IndexID,
p.partition_number AS PartitionNumber,
fg.name AS FileGroupName,
p.[rows] AS Rows,
p.[data_compression_desc] AS [Compression]
FROM sys.partitions AS p
JOIN sys.indexes AS i
ON i.object_id = p.object_id
AND i.index_id = p.index_id
2020-03-14 14:20:53 -07:00
JOIN sys.tables AS o
ON i.object_id = o.object_id
2020-03-14 14:20:04 -07:00
JOIN sys.data_spaces AS ds
ON ds.data_space_id = i.data_space_id
JOIN sys.filegroups AS fg
ON fg.data_space_id = i.data_space_id
WHERE
OBJECTPROPERTY(p.object_id, 'ISMSShipped') = 0
ORDER BY
ObjectName,
IndexID,
PartitionNumber;