runprepQuery or runNamedQuery - Best Practice

With the exception of some special cases (ahmmm QueryString parameters). You should almost always default to Named Queries.

Performance wise, I doubt there is anything more than negligible gains or losses between the two functions in a scripting environment. They both use the same JDBC driver, so the same queries will basically run exactly the same.

Although, I will admit that I don't know if Named Query caching applies to the scripting function or just bindings, so that could make a difference.

Best practice is more nuanced in this case. For performance when it comes to perspective see this topic:

For a reference of when you should use runPrepQuery over runNamedQuery see this topic

In short -

  • If you can use a named query, you should use a binding if at all possible.
  • If you don't need some type of dynamic SQL*, use a named query
  • If you do need some type of dynamic SQL, use a prep query.
  • If you are not using a competent DB engine (a.k.a MSSQL), and you need to provide an array of values (e.g. SELECT * FROM table WHERE column IN (value1,value2,value3)) use a prep query.
  • If you need to batch insert, use a prep query.

*Dynamic SQL as in a user is providing schema level objects such as column names.

There may be other edge cases where a prep query is the solution, but for most general cases, just use a Named Query

7 Likes