DatabricksYan Yan, Yuchuan Huang, Xinyi Yu, Gengliang WangTutorial
Declare and optimize with UNIQUE constraints on Databricks
Summary
Databricks now supports UNIQUE constraints in Public Preview for Unity Catalog Delta tables, allowing tables to declare multiple uniqueness guarantees beyond a single primary key. Unlike primary keys, UNIQUE constraints can be nullable and multiple can exist per table, enabling schemas to record business-level uniqueness such as username or composite (workspace_id, external_user_id). These constraints are informational by default and not enforced on writes, but after validation they can be marked with RELY to enable query optimizations. With RELY, the optimizer can eliminate unnecessary joins and DISTINCT aggregations, achieving up to 7.9x and 12.0x speedups respectively on TPC-DS SF10000 benchmarks by removing over 99.99% of shuffle. Foreign keys can also reference UNIQUE constraints, and the feature supports composite and nullable unique columns with specific nullability considerations for optimization, and the post provides validation queries to check data before enabling RELY.
Context
Databricks previously supported only PRIMARY KEY and FOREIGN KEY constraints, which limited the ability to record multiple uniqueness guarantees in table schemas.
Approach / What changed
Declare UNIQUE constraints on Unity Catalog Delta tables using SQL DDL, validate data with provided queries, mark constraints with RELY after validation, and rely on the optimizer to eliminate joins and DISTINCT aggregations.
Takeaways
UNIQUE constraints can be nullable and multiple per table, unlike PRIMARY KEY which requires non-nullable columns and allows only one per table.
With RELY, the optimizer can eliminate inner joins when a foreign key references a UNIQUE constraint and the child column is non-nullable, achieving up to 7.9x speedup.
DISTINCT can be removed when selecting from a non-nullable UNIQUE column, but not from a nullable UNIQUE column unless a filter removes nulls, achieving up to 12.0x speedup.
FAQ
Can UNIQUE constraints be enforced on writes?
No, UNIQUE constraints are informational by default and not enforced on writes. Enforcement must be handled by the ingestion pipeline or another data-quality process.
How do I enable query optimization with UNIQUE constraints?
Validate the data, then drop and recreate the constraint with RELY. Ensure foreign keys also have RELY. The optimizer can then use them for join elimination and DISTINCT removal.
What happens if data violates a RELY constraint?
The optimizer may produce incorrect results because it assumes the constraint holds. Only use RELY for guarantees that are validated and continuously maintained.
Source and freshness
- Source
- databricks.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.