-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPartitionedTableExample.sql
More file actions
63 lines (53 loc) · 1.76 KB
/
Copy pathPartitionedTableExample.sql
File metadata and controls
63 lines (53 loc) · 1.76 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
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Purpose:
- Demonstrate a small, self-contained partitioned table keyed by an integer identifier.
Safety:
- Changes state by creating a partition function, scheme, table, and indexes.
- Run only in a disposable database or after renaming every sample object.
Requirements:
- SQL Server 2012 or later.
Customization:
- Review boundary values, filegroup placement, key type, and index design.
*/
CREATE PARTITION FUNCTION SampleEventIdPartitionFunction (INT)
AS RANGE RIGHT FOR VALUES (10000, 20000, 30000, 40000);
GO
CREATE PARTITION SCHEME SampleEventIdPartitionScheme
AS PARTITION SampleEventIdPartitionFunction
ALL TO ([PRIMARY]);
GO
CREATE TABLE dbo.SampleEvents
(
event_id INT NOT NULL,
occurred_at DATETIME2(0) NOT NULL,
payload NVARCHAR(4000) NULL,
CONSTRAINT PK_SampleEvents
PRIMARY KEY NONCLUSTERED (event_id)
ON [PRIMARY]
)
ON SampleEventIdPartitionScheme(event_id);
GO
CREATE CLUSTERED INDEX CX_SampleEvents_EventId
ON dbo.SampleEvents(event_id)
ON SampleEventIdPartitionScheme(event_id);
GO
-- Inspect boundary values and row distribution.
SELECT
pf.name AS partition_function,
prv.boundary_id,
prv.value AS boundary_value
FROM sys.partition_functions AS pf
LEFT JOIN sys.partition_range_values AS prv
ON prv.function_id = pf.function_id
WHERE pf.name = N'SampleEventIdPartitionFunction'
ORDER BY prv.boundary_id;
SELECT
$PARTITION.SampleEventIdPartitionFunction(event_id) AS partition_number,
COUNT_BIG(*) AS row_count
FROM dbo.SampleEvents
GROUP BY $PARTITION.SampleEventIdPartitionFunction(event_id)
ORDER BY partition_number;
-- Cleanup for a disposable test database:
-- DROP TABLE dbo.SampleEvents;
-- DROP PARTITION SCHEME SampleEventIdPartitionScheme;
-- DROP PARTITION FUNCTION SampleEventIdPartitionFunction;