Chapter 30 Exercises: Partitioning Strategies
Exercise 30.1: Create a Range-Partitioned Table on z/OS
Objective: Implement range partitioning for a financial transaction table on DB2 for z/OS.
Scenario: Meridian National Bank's compliance team requires a new AUDIT_LOG table that stores audit events for seven years with monthly granularity.
Tasks:
-
Write the DDL to create a table space
AUDITTSin databaseMERIDIANDBwith the following specifications: - DSSIZE 8G - BUFFERPOOL BP32K - SEGSIZE 64 - MAXPARTITIONS 128 -
Create the
MERIDIAN.AUDIT_LOGtable with the following columns: -AUDIT_ID(BIGINT, identity, not null) -EVENT_TIMESTAMP(TIMESTAMP, not null, partition key) -USER_ID(VARCHAR(30), not null) -EVENT_TYPE(CHAR(10), not null) -OBJECT_SCHEMA(VARCHAR(128)) -OBJECT_NAME(VARCHAR(128)) -SQL_TEXT(VARCHAR(2000)) -RESULT_CODE(INTEGER) -
Define monthly partitions from January 2020 through December 2026 (84 partitions).
-
Create a DPSI on
(USER_ID, EVENT_TIMESTAMP)and another on(EVENT_TYPE, EVENT_TIMESTAMP).
Expected output: Complete DDL statements ready for execution on z/OS.
Exercise 30.2: Create a Range-Partitioned Table on LUW
Objective: Implement the same audit log table on DB2 LUW with ATTACH/DETACH capability.
Tasks:
- Create the
meridian.audit_logtable with equivalent columns using LUW syntax. - Define monthly partitions with explicit
STARTINGandENDINGclauses. -
Create both partitioned and non-partitioned indexes: - Partitioned index on
(event_timestamp, user_id)- Non-partitioned index on(user_id) -
Write a script to detach the January 2020 partition and attach a new January 2027 partition.
Expected output: DDL and DML scripts with proper SET INTEGRITY handling.
Exercise 30.3: Partition Elimination Verification
Objective: Verify that partition elimination is occurring for critical queries.
Tasks:
-
Write an EXPLAIN statement for the following query against the monthly-partitioned
TRANSACTION_HISTORY:sql SELECT ACCOUNT_ID, SUM(AMOUNT) AS TOTAL FROM MERIDIAN.TRANSACTION_HISTORY WHERE TRANS_DATE BETWEEN '2026-01-01' AND '2026-03-31' GROUP BY ACCOUNT_ID; -
Query the PLAN_TABLE (z/OS) or EXPLAIN tables (LUW) to determine: - Which partitions are accessed? - How many partitions are eliminated? - What access method is used within the qualifying partitions?
-
Rewrite the following query to enable partition elimination (it currently does NOT achieve elimination):
sql SELECT * FROM MERIDIAN.TRANSACTION_HISTORY WHERE YEAR(TRANS_DATE) = 2026 AND MONTH(TRANS_DATE) = 1; -
Explain why the original query defeats partition elimination and why your rewrite fixes it.
Exercise 30.4: Rolling Window Implementation
Objective: Design and implement a complete rolling window lifecycle for the TRANSACTION_HISTORY table.
Scenario: It is January 1, 2027. The table currently has 84 monthly partitions from January 2020 through December 2026. You need to: - Archive the January 2020 data. - Add a January 2027 partition.
Tasks (z/OS):
- Write the UNLOAD utility statement to extract January 2020 data before rotation.
- Write the
ALTER TABLE ... ROTATE PARTITIONstatement. - Write the RUNSTATS utility statement for the new (last) partition.
- Write the COPY utility statement for the new partition.
Tasks (LUW):
- Write the
ALTER TABLE ... DETACH PARTITIONstatement. - Write the EXPORT command to archive the detached table.
- Write the DDL to create a staging table for January 2027 data.
- Write the
ALTER TABLE ... ATTACH PARTITIONstatement. - Write the
SET INTEGRITYstatement. - Write the RUNSTATS command for the newly attached partition.
Exercise 30.5: Hash Partitioning and DPF Design
Objective: Design a DPF configuration for Meridian's digital banking analytics workload.
Scenario: The analytics team needs to distribute a CUSTOMER_360 table across four database partitions for parallel query processing.
Tasks:
- Create a database partition group with four partitions (0, 1, 2, 3).
- Create a table space in this partition group.
-
Create the
CUSTOMER_360table with the following columns: -CUSTOMER_ID(BIGINT, not null) -CUSTOMER_NAME(VARCHAR(100)) -TOTAL_DEPOSITS(DECIMAL(15,2)) -TOTAL_LOANS(DECIMAL(15,2)) -ACCOUNT_COUNT(INTEGER) -LAST_TRANSACTION_DATE(DATE) -RISK_SCORE(DECIMAL(5,2)) Distribution key:CUSTOMER_ID -
Create a
TRANSACTION_SUMMARYtable distributed byCUSTOMER_IDwith monthly range partitioning onSUMMARY_DATE. Explain why usingCUSTOMER_IDas the distribution key enables collocated joins withCUSTOMER_360. -
Write a query that joins these two tables and explain whether it is a collocated or non-collocated join.
Exercise 30.6: Partition Monitoring and Health Check
Objective: Build a monitoring script that identifies partition imbalances and maintenance needs.
Tasks:
-
Write a query for z/OS that reports the following for each partition of
TRANSACTION_HISTORY: - Partition number - Number of active pages - Allocated space in MB - Percentage of total table space used by this partition - Whether the partition is in REORG-pending status -
Write the equivalent query for LUW using
SYSCAT.DATAPARTITIONSandMON_GET_TABLE. -
Define thresholds for partition imbalance alerts: - Flag any partition that is more than 3x the average partition size. - Flag any partition with fewer than 100 rows (potentially empty or misconfigured).
-
Write a stored procedure (LUW) that generates an alert report showing imbalanced partitions.
Exercise 30.7: Partitioning Anti-Pattern Analysis
Objective: Identify and resolve partitioning design mistakes.
Scenario: A junior DBA created the following partitioned table:
CREATE TABLE meridian.daily_balances (
balance_date DATE NOT NULL,
account_id BIGINT NOT NULL,
closing_balance DECIMAL(15,2),
opening_balance DECIMAL(15,2)
)
PARTITION BY RANGE (balance_date)
(
-- Daily partitions for 10 years = 3,650 partitions
PARTITION d_20170101 STARTING ('2017-01-01') ENDING ('2017-01-01') INCLUSIVE,
PARTITION d_20170102 STARTING ('2017-01-02') ENDING ('2017-01-02') INCLUSIVE,
-- ... (3,648 more daily partitions)
PARTITION d_20261231 STARTING ('2026-12-31') ENDING ('2026-12-31') INCLUSIVE
);
Tasks:
- Identify at least three problems with this design.
- Propose an improved partitioning scheme with justification.
- Write the DDL for the improved design.
- Explain how the improved design handles a query like:
sql SELECT account_id, closing_balance FROM meridian.daily_balances WHERE balance_date = '2026-03-15'; - Calculate the partition elimination benefit for the original vs. improved design for the query:
sql SELECT account_id, AVG(closing_balance) FROM meridian.daily_balances WHERE balance_date BETWEEN '2026-01-01' AND '2026-03-31' GROUP BY account_id;