Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:31:30+00:00 2026-05-23T17:31:30+00:00

I’m struggling to improve a n+1 query on a project I’m working on. I

  • 0

I’m struggling to improve a n+1 query on a project I’m working on. I use Hibernate with the model shown below, and I want to express a query to retrieve all items related to a portfolio, including the last two prices on each item (price on given date and previous price).

enter image description here

Example API:

List<Items> items = findItemsWithLatestTwoPrices(portfolio, latestPriceDate);

Currently I use one query to extract all items related to the portfolio, and then I iterate over those items to query the two latest prices on a given item (so n+1).

I tried expressing this in native sql using a correlated subquery , but the performance was terrible. This and the fact that there are new prices every day (so the query is getting slower) has lead me to think I need a different model, but I’m struggling to come up with a model that is reasonably effective and constant over time as number of prices increase.

I’ve been thinking about different solutions including representing prices as linked lists, or using some sort of tree but I believe there are better alternatives. Am I missing something obvious? Has anyone working on a similar problem come up with at good solution?

I don’t really care wether I use HQL or native SQL as long as the performance is decent. I’m also open to make changes to the model.

Thanks!

[Edit]

Since I have over two years of price data, and there can be 1000+ items pr. portfolio, retrieving the entire graph is probably not a good idea. Also I need random access by date, so storing the two prices as fields on the item is unfortunately not an option.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:31:30+00:00Added an answer on May 23, 2026 at 5:31 pm

    Not sure I´m catching all your concerns, but like you´ve probably figured, there´s no easy solution to this with Hibernate. It will come down to your modeling of the domain. I think you´re best to separate the normal case and the special case. You can model them in your normal domain, or use special representations for the special cases.

    For fetching the n latest prizes have you tried setting the batch size on the relation? Make the relation ordered (latest on top), and then set the batch-size to something like 10. That would make Hibernate query for 10 and 10 rows, and with indices on the foreign key and the order column it should perform ok in most cases.

    It also seems to me you could keep extra relations as well as the entire set. Don´t be afraid of explicitly model important relationships like “last months prices”, even though it would be duplication of data. It should be possible to avoid duplication in the DB in most cases.

    For your random access based on dates it sounds like you´re best served with a custom query instead of access through the domain model, if they´re too slow consider using second level caching, but I´m guessing that your access pattern won´t benefit much from this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im struggling with this one. I have a list of items and on a
Struggling with this tournament fixtures algorithm. The code is working perfectly but I need
Struggling to get nested repeaters working although I feel I am close! I am
Struggling with onchange for JQuery Datetime plugin: http://plugins.jquery.com/project/datetime I am using the following code.
struggling to get my head round this. i want to convert the following sql
I've been struggling with this one SQL query requirement today that I was wondering
Struggling with what should be a simple layout. I want a logo, with a
Struggling with the basics here. I want to setup an array, and fill it
Struggling getting a query to work…….. I have two tables:- tbl.candidates: candidate_id agency_business_unit_id tbl.candidate_employment_tracker
I am struggling to model a scenario and came across a question that, while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.