Issue with Oracle bind variables not using index properly

后端 未结 4 1421
执念已碎
执念已碎 2020-12-06 14:16

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)
         


        
4条回答
  •  春和景丽
    2020-12-06 14:26

    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.

提交回复
热议问题