← Back to SQL, page 15

sql-0290 · Medium mid

A join uses ON o.customer_id = c.id OR o.email = c.email. Why can an OR in a join condition be slow, and how does SQLite plan it?

An OR across different columns rules out a hash or merge join on one key. SQLite can still probe both indexes per row with a MULTI-INDEX OR plan, but only if every branch has an index; otherwise it compares rows pair by pair. Splitting the join into two joins combined with UNION gives each branch its own plan.

Full answer
sql
CREATE TABLE c (id INTEGER PRIMARY KEY, email TEXT);
CREATE TABLE o (id INTEGER PRIMARY KEY, customer_id INTEGER, email TEXT);
CREATE INDEX c_email ON c(email);
EXPLAIN QUERY PLAN SELECT o.id, c.id FROM o JOIN c ON o.customer_id = c.id OR o.email = c.email;
text
QUERY PLAN
SCAN o
MULTI-INDEX OR
  INDEX 3
    SEARCH c USING COVERING INDEX c_email (email=?)
  INDEX 4
    SEARCH c USING INTEGER PRIMARY KEY (rowid=?)

SQLite scans o and, for each order, probes the email index and the primary key separately, then merges the matches. That works because both branches are indexed. Without the email index, the email branch has nothing to probe:

sql
DROP INDEX c_email;
EXPLAIN QUERY PLAN SELECT o.id AS oid, c.id AS cid FROM o JOIN c ON o.customer_id = c.id OR o.email = c.email;
text
QUERY PLAN
SCAN o
SCAN c

The UNION rewrite gives each branch its own join plan and removes pairs that matched both ways:

sql
EXPLAIN QUERY PLAN
SELECT o.id, c.id FROM o JOIN c ON o.customer_id = c.id
UNION
SELECT o.id, c.id FROM o JOIN c ON o.email = c.email;
text
QUERY PLAN
COMPOUND QUERY
  LEFT-MOST SUBQUERY
    SCAN o
    SEARCH c USING INTEGER PRIMARY KEY (rowid=?)
  UNION USING TEMP B-TREE
    SCAN o
    SEARCH c USING COVERING INDEX c_email (email=?)

Engines that cannot combine indexes for a join condition fall back to a nested loop that tests the OR for every pair of rows, which grows with the product of the table sizes.

The deeper question is the data model: if orders sometimes carry an id and sometimes only an email, a cleanup step that resolves emails to customer ids once makes every later query simple.

sql
CREATE TABLE c (id INTEGER PRIMARY KEY, email TEXT);
CREATE TABLE o (id INTEGER PRIMARY KEY, customer_id INTEGER, email TEXT);
CREATE INDEX c_email ON c(email);
EXPLAIN QUERY PLAN SELECT o.id, c.id FROM o JOIN c ON o.customer_id = c.id OR o.email = c.email;

EXPLAIN QUERY PLAN
SELECT o.id, c.id FROM o JOIN c ON o.customer_id = c.id
UNION
SELECT o.id, c.id FROM o JOIN c ON o.email = c.email;

DROP INDEX c_email;
EXPLAIN QUERY PLAN SELECT o.id AS oid, c.id AS cid FROM o JOIN c ON o.customer_id = c.id OR o.email = c.email;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • OR across different columns rules out hash joins on one key
  • Split into one join per key and UNION them
  • SQLite MULTI-INDEX OR needs an index on every branch
  • Fix inconsistent keys at the source when possible

Common mistakes

  • Accepting nested-loop scans for OR joins
  • Using UNION ALL and double-counting pairs that match both ways

Follow-ups

  • How would you resolve emails to ids permanently?
  • When can an engine use a multi-index OR here?

Tips

  • Compare the two plans