Performance of generated queries based on example classes.

62 views
Skip to first unread message

jdevelop

unread,
Nov 14, 2012, 8:45:07 AM11/14/12
to mybatis-user
Hi all!

I looked through the mailing list and didn't find any information of
what overhead does imply.

For example:

<where >
<foreach collection="oredCriteria" item="criteria"
separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value"
item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>

Is this XML re-evaluated at run time for each query? Or some code
generation happens and produced bytecode is fast and efficient?

Thanks in advance!

Jeff Butler

unread,
Nov 14, 2012, 8:52:23 AM11/14/12
to mybati...@googlegroups.com
This is MyBatis dynamic SQL.  The XML is only parsed one time, but the SQL is re-generated every time the query is run.

The *ByExample queries generated by MyBatis generator are very flexible, but flexibility always comes with a cost.

If you are concerned about performance, you should write some tests to see what the impact will be in your environment.  If your queries are always the same, it would likely be better to write custom SQL.

Jeff Butler

Eugene Dzhurinsky

unread,
Nov 14, 2012, 9:00:25 AM11/14/12
to mybati...@googlegroups.com
On Wed, Nov 14, 2012 at 08:52:23AM -0500, Jeff Butler wrote:
> This is MyBatis dynamic SQL.  The XML is only parsed one time, but the SQL
> is re-generated every time the query is run.
> The *ByExample queries generated by MyBatis generator are very flexible,
> but flexibility always comes with a cost.
> If you are concerned about performance, you should write some tests to see
> what the impact will be in your environment.  If your queries are always
> the same, it would likely be better to write custom SQL.

But custom SQL will be built with same expressions. What I want to know - is
generation based on some sort of finite-state machine, generated from XML into
bytecode? Or it is evaluated at runtime?

--
Eugene N Dzhurinsky

Jeff Butler

unread,
Nov 14, 2012, 9:06:21 AM11/14/12
to mybati...@googlegroups.com
There's no magic bytecode manipulation.  The XML is parsed into a tree of Java objects and that tree is evaluated at runtime to generate SQL based on the values of the parameter object.  That's how all MyBatis dynamic SQL works.

Custom SQL might be faster is you don't need all the flexibility of the *ByExample methods - there would likely be a smaller tree to evaluate.  But again, unit tests are your friend here.

Jeff Butler
Reply all
Reply to author
Forward
0 new messages