generated from mg0x7BE/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPartitions.sql
More file actions
executable file
·51 lines (40 loc) · 2.1 KB
/
Partitions.sql
File metadata and controls
executable file
·51 lines (40 loc) · 2.1 KB
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
/**********************************************************************************************/
-- Check data distribution in various partitions of the table & the indexed view.
SELECT OBJECT_NAME(p.object_id) as obj_name, p.index_id, p.partition_number, p.rows, a.type_desc, filegroup_name(filegroup_id) as filegroup_name
FROM sys.system_internals_allocation_units a
JOIN sys.partitions p
ON p.partition_id = a.container_id
WHERE p.object_id IN (OBJECT_ID(N'dbo.f_whatever'), OBJECT_ID(N'dbo.v_f_sales_whatever ')) -- table/view
ORDER BY obj_name, p.index_id, p.partition_number
/**********************************************************************************************/
-- Example: row counts in non-empty partitions:
SELECT OBJECT_NAME(p.object_id) as obj_name, p.partition_number, SUM(p.rows) as 'rows'
FROM sys.partitions p
WHERE p.object_id IN (OBJECT_ID(N'myschema.ErrorLog'), OBJECT_ID(N'myschema.ProcessLog ')) AND p.rows > 0
GROUP BY OBJECT_NAME(p.object_id), p.partition_number
ORDER BY 2 desc, 1 asc
/**********************************************************************************************/
-- Returns all rows from one partition of a partitioned table or index
SELECT * FROM Production.TransactionHistory
WHERE $PARTITION.TransactionRangePF1(TransactionDate) = 5 ;
/**********************************************************************************************/
-- Getting the partition number for a set of partitioning column values
/*
USE AdventureWorks2008R2 ;
GO
CREATE PARTITION FUNCTION RangePF1 ( int )
AS RANGE FOR VALUES (10, 100, 1000) ;
GO
*/
SELECT $PARTITION.RangePF1 (10) ;
GO
/**********************************************************************************************/
-- Gets the number of rows in each nonempty partition of a partitioned table or index
USE AdventureWorks2008R2 ;
GO
SELECT $PARTITION.TransactionRangePF1(TransactionDate) AS Partition,
COUNT(*) AS [COUNT] FROM Production.TransactionHistory
GROUP BY $PARTITION.TransactionRangePF1(TransactionDate)
ORDER BY Partition ;
GO
/**********************************************************************************************/