---
title: Optimize queries with RELY on primary key and unique constraints
description: Add the RELY option to primary key and unique constraints so Databricks can rewrite queries to skip unnecessary DISTINCT operations and joins.
canonical_url: https://docs.databricks.com/aws/en/sql/user/queries/query-optimization-constraints
---

# Optimize queries with `RELY` on primary key and unique constraints

Primary key and unique constraints specify which columns uniquely identify each row in a table. Along with foreign keys, they help users and tools understand relationships in your data. When you specify these constraints with the `RELY` option, the query optimizer can use them to rewrite queries, for example, by removing a `DISTINCT` operation on a key column or eliminating a join that doesn't change the result.

> [!NOTE]
>
> `RELY` query optimizations require pro or serverless SQL warehouses, or serverless compute, and aren't supported on classic SQL warehouses.

## Add primary key or unique constraints

You can add a primary key or unique constraint in your table creation statement, as in the following example, or add one to a table using the `ADD CONSTRAINT` clause.

```sql
CREATE TABLE customer (
  c_customer_sk int,
  PRIMARY KEY (c_customer_sk)
  )
```

In this example, `c_customer_sk` is the customer ID key. The primary key constraint specifies that each customer ID value should be unique in the table. Unique constraints follow the same pattern using `UNIQUE` instead of `PRIMARY KEY`.

Databricks does not enforce key constraints. They can be validated through your existing data pipeline or ETL. See [Manage data quality with pipeline expectations](https://docs.databricks.com/aws/en/ldp/expectations.md) to learn about working expectations on streaming tables and materialized views. See [Constraints on Databricks](https://docs.databricks.com/aws/en/tables/constraints.md) to learn about working with constraints on Delta tables.

> [!NOTE]
>
> It is the user's responsibility to check whether a constraint is satisfied. Relying on a constraint that is not satisfied can lead to incorrect query results.

## Use `RELY` to enable optimizations

When you know that a primary key or unique constraint is valid, you can enable optimizations based on the constraint by specifying it with the `RELY` option. See [ADD CONSTRAINT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-alter-table-add-constraint.md) for the complete syntax.

The `RELY` option allows Databricks to exploit the constraint to rewrite queries. The following optimizations can only be performed if the `RELY` option is specified in an `ADD CONSTRAINT` clause or `ALTER TABLE` statement.

Using `ALTER TABLE`, you can modify a table's primary key to include the `RELY` option, as shown in the following example.

```sql

ALTER TABLE
  customer DROP PRIMARY KEY;
ALTER TABLE
  customer
ADD
  PRIMARY KEY (c_customer_sk) RELY;
```

## Optimization examples

The following examples extend the previous example that creates a `customer` table where `c_customer_sk` is a verified unique identifier named as a `PRIMARY KEY` with the `RELY` option specified. The same optimizations can apply to a `UNIQUE` constraint with the `RELY` option.

### <a id="example-1" />Example 1: Eliminate unnecessary aggregations

The following shows a query that applies a `DISTINCT` operation to a primary key.

```sql
SELECT
  DISTINCT c_customer_sk
FROM
  customer;
```

Because the `c_customer_sk` column is a verified `PRIMARY KEY` constraint, all values in the column are unique. With the `RELY` option is specified, Databricks can optimize the query by not performing the `DISTINCT` operation.

The optimizer can also remove `DISTINCT` when the selected column is covered by a valid `UNIQUE` constraint specified with `RELY`.

### <a id="example-2" />Example 2: Eliminate unnecessary joins

The following example shows a query where Databricks can eliminate an unnecessary join.

The query joins a fact table, `store_sales` with a dimension table, `customer`. It performs a left outer join, so the query result includes all records from the `store_sales` table and matched records from the `customer` table. If there is no matching record in the `customer` table, the query result shows a `NULL` value for the `c_customer_sk` column.

```sql
SELECT
  SUM(ss_quantity)
FROM
  store_sales ss
  LEFT JOIN customer c ON ss.customer_sk = c.c_customer_sk;
```

To understand why this join is unnecessary, consider the query statement. It requires only the `ss_quantity` column from the `store_sales` table. The `customer` table is joined on its primary key or on a unique constraint, so each row of `store_sales` matches at most one row in `customer`. Because the operation is an outer join, all records from the `store_sales` table are preserved, so the join does not change any data from that table. The `SUM` aggregation is the same whether or not these tables are joined.

Using the primary key or unique constraint with `RELY` gives the query optimizer the information it needs to eliminate the join. The optimized query looks more like this:

```sql
SELECT
  SUM(ss_quantity)
FROM
  store_sales ss
```

## Next steps

See [View the Entity Relationship Diagram](https://docs.databricks.com/aws/en/catalog-explorer/entity-relationship-diagram.md) to learn how to explore primary key and foreign key relationships in the Catalog Explorer UI.
