简体   繁体   中英

Jpa , Hibernate select query optimization

I am new to JPa / Hibernate. We are using JPA with hibernate in our application. Currently i observed that performing select query on table with criteria, hibernate perform select query for each row.

So my question is , how hibernate perform search on table and retrieves records ?

I have a scenario where i need to find active users and perform filers on active user list , like recently logged in users, most popular user(based on some criteria) for example i have 100 active users of which 20 users are recently logged in. 20 users are most popular.

if i have to fetch records from database , when i query with active users , hibernate perform 100 select operations.(with table scan)

and if i perform 2 seperate queries for recent and most popular users hibernate will perform 20 + 20 = 40 select operations. (but with 2 times table scan)

so how hibernate fetches records from database ? if i compare with jdbc , i would say by getting active pitches and performing filters on it i will be doing 1 time table scan.

but with hibernate it performs more select operations and it queries less select when i do individual selects for recent and most popular users ,even when i do 2 time table scan !

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "fk_profile_id", referencedColumnName = "pk_id")
private Profile Profile;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "investmentPitch", targetEntity = InvestmentType.class, orphanRemoval = true)
private List<InvestmentType> investmentType;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "investmentPitch", targetEntity = TabDetail.class, orphanRemoval = true)
private List<TabDetail> TabDetail;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "investmentPitch", targetEntity = Address.class, orphanRemoval = true)
private List<Address> Address;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
@JoinTable(name = "pitch_sector", joinColumns = { @JoinColumn(name = "fk_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "fk_sector_id", nullable = false, updatable = false) })
private List<SectorMaster> sectors;

The FetchType.EAGER causes hibernate to load the profile directly after the User is loaded. This triggers a select for every User.

You can change this to LAZY. Then the select is only triggered if you first access the profile object. If you have to access the profile of every selected user this would also cause a select for every user. To avoid this you can preload the profile together with the user in one single select. For detailed information about this see the following links: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-dynamicfetching and http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM