An Azure SQL Managed Instance database contains the table dbo.Orders, which is partitioned as shown in the exhibit. You are creating a new table named dbo.OrderLines that also has an OrderDate datetime2 column.
Queries frequently join dbo.Orders and dbo.OrderLines with an equijoin on the OrderDate column. You have the following requirements:
- Partition
dbo.OrderLinesso that the query optimizer can process this join from pairs of collocated partitions. dbo.OrderLinesdata must be stored only on filegroups FG_Line1 to FG_Line5, which already exist.
-- T-SQL fragment CREATE PARTITION FUNCTION pfOrderDate (datetime2) AS RANGE RIGHT FOR VALUES ('20250101', '20250401', '20250701', '20251001');
CREATE PARTITION SCHEME psOrders AS PARTITION pfOrderDate TO (FG_Ord1, FG_Ord2, FG_Ord3, FG_Ord4, FG_Ord5);
CREATE TABLE dbo.Orders ( OrderID int NOT NULL, OrderDate datetime2 NOT NULL, CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (OrderDate, OrderID) ) ON psOrders (OrderDate);
How should you partition dbo.OrderLines?
For an equijoin between partitioned tables to be processed from pairs of collocated partitions, the partitioning columns should match the join columns and the tables should use the same partition function or an equivalent one with identical boundary values. Collocation depends on the partition function, while the partition scheme decides which filegroup holds each partition. A new scheme built on pfOrderDate therefore keeps the tables collocated while placing OrderLines data on FG_Line1 to FG_Line5.
- Not correct: Create
dbo.OrderLineson the existing schemepsOrderswithOrderDateas the partitioning column. The partition scheme defines the filegroup of each partition; psOrders maps partitions to FG_Ord1 to FG_Ord5, so the data would not be stored on the FG_Line filegroups. - Not correct: Create a scheme
psLinesonpfOrderDatemapped to FG_Line1–FG_Line5, and useOrderLineIDas the partitioning column. For faster equijoins, the partitioning columns should be the same as the join columns; partitioning on OrderLineID does not match the OrderDate join. - Not correct: Create a function
pfLines (datetime2)RANGE RIGHT with boundaries '20250201', '20250501', '20250801', '20251101', a scheme on FG_Line1–FG_Line5, and partition onOrderDate. A different partition function is collocated only if it is essentially the same, including the same boundary values; these boundaries differ from pfOrderDate. - Correct: Create a scheme
psLinesonpfOrderDatemapped to FG_Line1–FG_Line5, and useOrderDateas the partitioning column. Using the same named partition function makes the tables collocated, OrderDate matches the join column, and the new scheme places each partition on the FG_Line filegroups.