Back to Langfuse

Use ANY JOIN When Only One Match Needed

.agents/skills/clickhouse-best-practices/rules/query-join-use-any.md

3.172.1916 B
Original Source

Use ANY JOIN When Only One Match Needed

Impact: HIGH

Use ANY JOINs when you only need a single match rather than all matches. They consume less memory and execute faster.

Incorrect (returns all matches):

sql
-- Returns all matching rows, uses more memory
SELECT o.order_id, c.name
FROM orders o
LEFT JOIN customers c ON c.id = o.customer_id;

Correct (returns first match only):

sql
-- Returns only first match per row, faster and less memory
SELECT o.order_id, c.name
FROM orders o
LEFT ANY JOIN customers c ON c.id = o.customer_id;

ANY JOIN types:

TypeBehavior
LEFT ANY JOINAt most one match from right table
INNER ANY JOINAt most one match, only matching rows
RIGHT ANY JOINAt most one match from left table

Reference: Minimize and Optimize JOINs