In my scenario, the following query runs fast (0.5 seconds on a table with 70 million rows):
select * from Purchases
where (purchase_id = 1700656396)
Taking a different approach to tbone
's answer, I realized that I can dynamically construct the query in code, and still use bind variables (and thus gain flexibility with indexes, and still be 100% protected from SQL injection).
In my code, I can do something like this:
string sql = "select * from Purchases where 1 = 1";
if(purchase_id != null) sql += " and (purchase_id = :purchase_id)";
if(purchase_name != null) sql += " and (purchase_name = :purchase_name)";
if(purchase_price != null) sql += " and (purchase_price = :purchase_price)";
I tested this and it solves my issue.