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?
漂亮男人2017-05-27 17:43:50
mybatis has three solutions for handling one-to-many situations:
Join the sub-table when querying, and then let mybatis assemble it
Do not join the sub-table when querying, and initiate a select to capture the sub-table data
Similar to the second one, except using fetchType=lazy
to delay the timing of crawling
These three options each have their own problems:
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
The second option will have 1+N queries, and the number of SQLs initiated will be very scary
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.