Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Configure table partitioning

A worked example

Single answer

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.OrderLines so that the query optimizer can process this join from pairs of collocated partitions.
  • dbo.OrderLines data 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?

Choose one answer
Why

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.OrderLines on the existing scheme psOrders with OrderDate as 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 psLines on pfOrderDate mapped to FG_Line1–FG_Line5, and use OrderLineID as 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 on OrderDate. 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 psLines on pfOrderDate mapped to FG_Line1–FG_Line5, and use OrderDate as 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.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free