Home  >  Q&A  >  body text

java - Mybatis related query

In actual development, Mybatis one-to-many and many-to-many related queries use resultMap reference, javaType reference or select reference. Which one has better performance?
Will lazy loading with select improve query speed and performance?

ringa_leeringa_lee2702 days ago654

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-27 17:43:50

    mybatis has three solutions for handling one-to-many situations:

    1. Join the sub-table when querying, and then let mybatis assemble it

    2. Do not join the sub-table when querying, and initiate a select to capture the sub-table data

    3. Similar to the second one, except using fetchType=lazy to delay the timing of crawling

    These three options each have their own problems:

    1. The first solution has two flaws: 1) It is inaccurate when doing paging queries, 2) If there are many related sub-tables, the Cartesian product will be very large

    2. The second option will have 1+N queries, and the number of SQLs initiated will be very scary

    3. The third solution seems to improve the efficiency of the first query, but if you get the lazy property in the loop, there is no difference from the second solution

    So if there are performance requirements, we need to assemble the one-to-many collection ourselves. The method is: collect the IDs of the main table, launch a one-time query to capture the data of all sub-tables, and then Manual assembly. The number of queries initiated in this way is 1+1.

    reply
    0
  • Cancelreply