perf: Split ifnull into two conditions

This produces better query plan with index intersection using 2
conditions instead of fulltable scan on dumb condition

TODO: LOTS OF TESTS
This commit is contained in:
Ankush Menat 2025-05-02 11:17:53 +05:30
parent c317462379
commit 23ffdc87ae

View file

@ -912,7 +912,12 @@ from {tables}
f.operator = "ilike"
condition = f"{column_name} {f.operator} {value}"
else:
condition = f"ifnull({column_name}, {fallback}) {f.operator} {value}"
# PERF: try to transform ifnull into two conditions, this way query plan can use index
# intersection instead of full table scans.
if fallback == value and f.operator == "=":
condition = f"( {column_name} is NULL OR {column_name} {f.operator} {value} )"
else:
condition = f"ifnull({column_name}, {fallback}) {f.operator} {value}"
return condition