Does it make sense to allow selects that use <foreach> etc to be prepared?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Does it make sense to allow selects that use <foreach> etc to be prepared?

Nate Weiss
Hi--

Just curious if it would be good practice to use type Callable rather
than Prepared for statements that use <foreach> or similar constructs
that may result in many different "versions" (that is, with different
numbers or parameters) of a statement? My feeble understanding is that
there would only be a benefit to the statement having been prepared
when the data is such that the number of jdbc parameters (question
marks in the final sql) is the same for many executions. If the size
of the collections being iterated over with <foreach> varies quite
abit, would you end up with a lot of prepared statements that are
rarely reused? Just wondering if that is something to keep an eye on
in general.

Thanks again...
Nate

Reply | Threaded
Open this post in threaded view
|

Re: Does it make sense to allow selects that use <foreach> etc to be prepared?

Nate Weiss
Okay, I was thinking that prepared was subclass of callable, but
actually its the other way around. So I guess my question is really,
if a given query will have a variable number of parameters, and you
know that in practice the number will vary quite a bit at runtime,
should one make the query use plain STATEMENT and use $ rather than #
for the parameters... So a not to accumulate a ton of prepared
statement instances, or just not worry about it?
Reply | Threaded
Open this post in threaded view
|

Re: Does it make sense to allow selects that use <foreach> etc to be prepared?

Matt Passell
Hi Nate,

Looking over the user guide, it appears that you can set the statementType to STATEMENT (as opposed to the default of PREPARED) for any of the major query types (select, insert, update, and delete).  Assuming you do that, you should be to use #{} and MyBatis will just do token replacement instead of inserting ?s and building a prepared statement.  If I'm wrong about that, I guess someone else can correct me.

--Matt


On Wed, Sep 29, 2010 at 10:37 PM, Nate Weiss <[hidden email]> wrote:
Okay, I was thinking that prepared was subclass of callable, but
actually its the other way around. So I guess my question is really,
if a given query will have a variable number of parameters, and you
know that in practice the number will vary quite a bit at runtime,
should one make the query use plain STATEMENT and use $ rather than #
for the parameters... So a not to accumulate a ton of prepared
statement instances, or just not worry about it?