简体   繁体   English

Jpa,Hibernate选择查询优化

[英]Jpa , Hibernate select query optimization

I am new to JPa / Hibernate. 我是JPa / Hibernate的新手。 We are using JPA with hibernate in our application. 我们在应用程序中将JPA与休眠一起使用。 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 ? 所以我的问题是,hibernate如何在表上执行搜索并检索记录?

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. 我有一个需要找到活动用户并在活动用户列表上执行文件管理器的场景,例如最近登录的用户,例如,最受欢迎的用户(基于某些条件),我有100个活动用户,其中最近有20个用户登录。 20位用户是最受欢迎的。

if i have to fetch records from database , when i query with active users , hibernate perform 100 select operations.(with table scan) 如果我必须从数据库中获取记录,则当我向活动用户查询时,hibernate会执行100次select操作。(使用表扫描)

and if i perform 2 seperate queries for recent and most popular users hibernate will perform 20 + 20 = 40 select operations. 如果我为最近和最受欢迎的用户执行2个单独的查询,休眠将执行20 + 20 = 40个选择操作。 (but with 2 times table scan) (但使用2次表扫描)

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. 如果我与jdbc进行比较,我会说通过获得有效的音调并对其执行过滤器,我将进行1次时间表扫描。

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 ! 但是使用休眠模式时,即使我进行2次时间表扫描,当我对最近和最受欢迎的用户进行单独选择时,它也会执行更多的选择操作,并且查询的选择更少。

@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. FetchType.EAGER导致休眠模式在用户加载后直接休眠。 This triggers a select for every User. 这会触发每个用户的选择。

You can change this to LAZY. 您可以将其更改为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 有关此的详细信息,请参见以下链接: http : //docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-dynamicfetchinghttp://docs.jboss.org/休眠/ ORM / 3.3 /参考/ EN / HTML / performance.html#性能取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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