Optimize SQL query with JOIN for OR condition (MariaDB)

54 views Asked by At

After simplifying a huge SQL query I have an easy query like this:

SELECT billing.id, payments.id 
FROM billing
LEFT JOIN payments ON payments.billing_id = billing.id
WHERE billing.email = 'foo' OR payments.txn = 'bar';

It takes ~0.5 sec to execute. Of cause the relevant columns have BTREE indexes.

This is what EXPLAIN tells me:

1   SIMPLE  billing     index   email       email       1027    NULL          99464 Using index 
1   SIMPLE  payments    ref     billing_id  billing_id  5       db.billing.id   1   Using where 

If I remove any of the OR-conditions or if I change OR to AND the query executes within 0.001 sec.

Any ideas how fix the performance without rewriting the query by UNION statement.

SQL Server version is 10.5.22-MariaDB (can be changed if needed).

0

There are 0 answers