FetchMode Join vs SubSelect

后端 未结 4 1601
闹比i
闹比i 2020-12-05 05:28

I have two tables Employee and Department following are the entity classes for both of them

Department.java
@Entity
@Table(name = \"DEPARTMENT\")
public clas         


        
4条回答
  •  旧巷少年郎
    2020-12-05 05:50

    A customer (financial services) of mine had a similar problem, and he wanted to "acquire the data in a single query". Well, I explained that it is better to have more than one query, because of the following:

    For FetchMode.JOIN the department would be transferred from the database to the application once per employee, because the join operation results in multiplying the department per employee. If you have 10 departments with 100 employees each, every of these 10 departments would be transferred 100 times within one query, simple SQL. So each department, in this case, is transferred 99 times more often than necessary, causing a data-transfer-overhead for the department.

    For Fetchmode SUBSELECT two queries are fired to the database. One would be used to get the data of the 1000 employes, one to get the 10 departments. This, for me, sounds much more efficient. For sure you would make sure that indices are in place so that data can be retrieved immediately.

    I would prefer FetchMode.SUBSELECT.

    It would be another case if each department has only one employee, but, as the name "department" suggests, this would very unlikely be the case.

    I suggest measuring of the access times to support this theory. For my customer I did measurements for different types of accesses, and the "department" table for my customer had many more fields (I did not design it, though). So it was soon clearly evident that the FetchMode.SUBSELECT was much faster.

提交回复
热议问题