runprepQuery or runNamedQuery - Best Practice

Hello,

I would like to know what is a best practice / best performance using runprepQuery vs runNamedQuery.

I developed the same system and it will run in 4 different stations, with different parameters, but the insert/updates will use the same named query or script (runprepQuery).

I was wondering what would be the best way.

Thanks

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

The caching only looks at the resource path of the query, any query limit in place, and the value of the parameters. If those match an existing, unexpired entry, and the query is marked in the config as having caching permitted, you will get back the cached value, no matter where the query originated.

[edited to add -- this is only true for SELECT queries. Using a cache for UPDATE queries doesn't make a lot of sense]

5 Likes

It should be mentioned that for the cache to be checked, the query parameters must be an exact match. If you are feeding timestamps to named queries, you may want to normalize them to even seconds, or even multiples of five seconds, or even minutes, et cetera. To maximize the chance of hitting a cached result.

4 Likes

Awesome.
Thank you so much for the references.

Thank you to @KathyApplebaum and @pturmel too.

1 Like

What about Scripting (project Library), Custom Method or Property Change Script(valueChanged)?
I understand it is subjective and depends on the project, but what would be the best practice?

For most of my projects I use Script in the Library, and for example, call the script when the buttons are pressed.

This is the Right Answer™, in my not-so-humble opinion. Events and custom methods should always be a one-liner calling a project library script function, passing any necessary arguments.

This is especially important for gateway events, to avoid issues with those events' legacy scoping rules. (I sure hope that changes in v8.3.)

4 Likes