← Files ClickHouseARCHIVED FILE

skills/clickhouse-best-practices/rules/query-join-null-handling.md

1.04 KB · Oct 3, 2026 · 06:07 UTC

↓ Download file

---
title: Optimize NULL Handling in Outer JOINs
impact: MEDIUM
impactDescription: "Default values instead of NULL reduces memory overhead"
tags: [query, JOIN, NULL, memory]
---

## Optimize NULL Handling in Outer JOINs

**Impact: MEDIUM**

Set `join_use_nulls = 0` to use default column values instead of NULL markers, reducing memory overhead compared to Nullable wrappers.

**Example:**

```sql
-- Use default values instead of NULLs for non-matching rows
SET join_use_nulls = 0;

SELECT o.order_id, c.name
FROM orders o
LEFT JOIN customers c ON c.id = o.customer_id;
-- Non-matching rows get '' for name instead of NULL
```

**When to use:**

| Setting | Behavior | Use Case |
|---------|----------|----------|
| `join_use_nulls = 0` (default) | Default values (empty string, 0) for non-matches | When you can handle default values |
| `join_use_nulls = 1` | NULL for non-matches | When you need to distinguish "no match" from "matched with default" |

Reference: [Minimize and Optimize JOINs](https://clickhouse.com/docs/best-practices/minimize-optimize-joins)

SHA-256: a1bf38fff84a97d30a13fb507104892ee371fe22d143980800b1cf16f4790cd3