optimization - Struggling to optimize N+1 query in Hibernate -



optimization - Struggling to optimize N+1 query in Hibernate -

i’m struggling improve n+1 query on project i’m working on. utilize hibernate model shown below, , want express query retrieve items related portfolio, including lastly 2 prices on each item (price on given date , previous price).

example api:

list<items> items = finditemswithlatesttwoprices(portfolio, latestpricedate);

currently utilize 1 query extract items related portfolio, , iterate on items query 2 latest prices on given item (so n+1).

i tried expressing in native sql using correlated subquery , performance terrible. , fact there new prices every day (so query getting slower) has lead me think need different model, i’m struggling come model reasonably effective , constant on time number of prices increase.

i’ve been thinking different solutions including representing prices linked lists, or using sort of tree believe there improve alternatives. missing obvious? has working on similar problem come @ solution?

i don't care wether utilize hql or native sql long performance decent. i'm open create changes model.

thanks!

[edit]

since have on 2 years of cost data, , there can 1000+ items pr. portfolio, retrieving entire graph not idea. need random access date, storing 2 prices fields on item unfortunately not option.

not sure i´m catching concerns, you´ve figured, there´s no easy solution hibernate. come downwards modeling of domain. think you´re best separate normal case , special case. can model them in normal domain, or utilize special representations special cases.

for fetching n latest prizes have tried setting batch size on relation? create relation ordered (latest on top), , set batch-size 10. create hibernate query 10 , 10 rows, , indices on foreign key , order column should perform ok in cases.

it seems me maintain relations entire set. don´t afraid of explicitly model of import relationships "last months prices", though duplication of data. should possible avoid duplication in db in cases.

for random access based on dates sounds you´re best served custom query instead of access through domain model, if they´re slow consider using sec level caching, i´m guessing access pattern won´t benefit much this.

hibernate optimization orm data-structures query-optimization

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -