Using conditional WHERE
is expensive. Firebird doesn't short circuit these kinds of evaluations and the optimizer is unable to select an optimal plan this way. It is probably better to generate the WHERE
condition dynamically.
Other solutions include using a UNION
, for example when you replace your first query with:
SELECT *
FROM MYBOOKS
WHERE BOOKID = :BOOKID
UNION ALL
SELECT *
FROM MYBOOKS
WHERE :BOOKID = 0
The optimizer usually chooses a better plan for this query, but given the multiple conditions you want to apply, this probably is not feasible.