Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.1 KB
Raw Blame

title: Use ANY JOIN When Only One Match Needed impact: HIGH impactDescription: "Returns first match only; less memory and faster execution" tags: [query, JOIN, ANY, performance]

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):

-- 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):

-- 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:

Type Behavior
LEFT ANY JOIN At most one match from right table
INNER ANY JOIN At most one match, only matching rows
RIGHT ANY JOIN At most one match from left table

Reference: Minimize and Optimize JOINs