-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPARTITIONS_TABLE.sql
58 lines (56 loc) · 2.12 KB
/
PARTITIONS_TABLE.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--MSSQL Server
--секции таблицы
declare @TableName nvarchar(200) = N'dbo.TABLE_NAME';
select p.partition_id
, p.partition_number
, schema_name(o.schema_id) + '.' + object_name(i.object_id) as table_name
, ps.name as partition_scheme_name
, fg.name as filegroup
, f.name as partition_function_name
, tp.name as partition_function_type
, c.name as partition_column_name
, case boundary_value_on_right
when 1
then 'less than'
else 'less than or equal to'
end as comparison
, rv.value
, p.rows
, au.total_pages as pages
, convert(varchar(6), convert(int, substring(au.first_page, 6, 1) + substring(au.first_page, 5, 1))) + ':' + convert(varchar(20), convert(int, substring(au.first_page, 4, 1) + substring(au.first_page, 3, 1) + substring(au.first_page, 2, 1) + substring(au.first_page, 1, 1))) as first_page
, p.data_compression_desc
from sys.partitions as p
inner join sys.objects as o
on p.object_id = o.object_id
inner join sys.indexes as i
on p.object_id = i.object_id
and p.index_id = i.index_id
inner join sys.index_columns as ic
on ic.object_id = i.object_id
and ic.index_id = i.index_id
and ic.partition_ordinal >= 1 -- because 0 = non-partitioning column
inner join sys.columns as c
on c.object_id = o.object_id
and c.column_id = ic.column_id
inner join sys.partition_schemes as ps
on ps.data_space_id = i.data_space_id
inner join sys.partition_functions as f
on f.function_id = ps.function_id
left join sys.partition_range_values as rv
on f.function_id = rv.function_id
and p.partition_number = rv.boundary_id
inner join sys.partition_parameters as pp
on pp.function_id = f.function_id
inner join sys.types as tp
on tp.user_type_id = pp.user_type_id
inner join sys.destination_data_spaces as dds
on dds.partition_scheme_id = ps.data_space_id
and dds.destination_id = p.partition_number
inner join sys.filegroups as fg
on dds.data_space_id = fg.data_space_id
inner join sys.system_internals_allocation_units as au
on p.partition_id = au.container_id
where i.index_id in (0, 1)
and o.object_id = object_id(@TableName)
order by rv.value desc
;