0

I am using MyBatis to do a large number of updates to a Postgresql database. Some of the objects I'm pushing to the database have combined key objects for their ids--a custom java object that represents two columns in the resulting table, the combination of which is guaranteed to be unique. I'm seeing poor performance for updating these objects (there are thousands of them) and have seen that having an id field marked in your resultMap can improve performance. However, I'm not sure what the right syntax is for marking an association as an id.

I currently have created the resultMap with all of the properties described EXCEPT for the id itself.

<resultMap id="result" type = "com.sms.MyClass">
    <result....>
    <association property="id" javaType="com.sms.CombinedKeyClass">
        <constructor>
            <arg column="idVal1" javaType="int"/>
            <arg column="idVal2" javaType="int"/>
        </constructor>
        <result property="idVal1" column="idVal1"/>
        <result property="idVal2" column="idVal2"/>
    </association>
</resultMap>

I'm trying to figure out how to mark this association as the id for the resultMap. I tried adding the following:

 <resultMap id="result" type = "com.sms.MyClass">
    <id property="id" javaType="com.sms.CombinedKeyClass"/>
    <result....>
    <association property="id" javaType="com.sms.CombinedKeyClass">
        <constructor>
            <arg column="idVal1" javaType="int"/>
            <arg column="idVal2" javaType="int"/>
        </constructor>
        <result property="idVal1" column="idVal1"/>
        <result property="idVal2" column="idVal2"/>
    </association>
</resultMap>

MyBatis yells at me when I try this configuration, saying that it doesn't have a typeHandler for "property id". Is there a way to refer to the association as the typeHandler for the id? Or is there a way to mark the association itself as the id for the resultMap?

Also, is any of this going to help my update performance in the first place? Right now I'm passing in a List of these objects and using a "foreach" for each item in the list to update the relevant fields. I'd assumed this would be faster than making a separate update call to MyBatis for each individual update, but so far it hasn't been.

Michael
  • 43
  • 5
  • `` is not used during update. Using `` to update many rows is bad for performance. You should do 'batch update'. Please see my answer [here](https://stackoverflow.com/a/56515063/1261766). – ave Jul 10 '19 at 18:39
  • Thanks! This helped me fix my problem, huge performance boost right off the bat. Now I just need to play around with batch sizes.... – Michael Jul 11 '19 at 13:11
  • Possible duplicate of [Fastest way to update huge number of rows with input param List in MyBatis to Oracle db](https://stackoverflow.com/questions/56513222/fastest-way-to-update-huge-number-of-rows-with-input-param-listt-in-mybatis-to) – ave Jul 12 '19 at 02:17
  • Glad to know it helped! – ave Jul 12 '19 at 02:21

1 Answers1

0

Ave answered the question in one of the comments--the update operation doesn't actually use the resultMap, so indexing wouldn't make a difference. The real solution was optimizing the operation itself by using batching, both with a BATCH executor and by implementing a batch size as in the linked example in their comment.

Michael
  • 43
  • 5