java - Mybatis related query
ringa_lee
ringa_lee 2017-05-27 17:41:50
0
1
648

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_lee
ringa_lee

ringa_lee

reply all(1)
漂亮男人

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!