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

  • Home
  • SEARCH
  • 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 8733933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:44:52+00:00 2026-06-13T09:44:52+00:00

I am basically obtaining a decently sized result set (a few thousand) through datanucleus

  • 0

I am basically obtaining a decently sized result set (a few thousand) through datanucleus by running a JPQL query. On each of these, I also want to find the number of references from another table. The data is in a MySQL db.

For example:

List<Instrument> instruments = em().createQuery("SELECT i FROM Instrument AS i").getResultList();
for(Instrument i : instruments)
{
    Query q = em().createQuery("SELECT COUNT(c) FROM Component AS c WHERE c.instrument.id = :id")
    q.setParameter("id", i.getId());
    long count = (Long) q.getSingleResult();
}

So, basically I want the list of instruments and also the list of components attached to the instrument as per the above example.

I’ve used similar code at a bunch of places and it performs pretty poorly. I understand that for 2000 instruments, I’ll fire 2000 additional queries to count components and that will slow things down. I’m sure there’s a better pattern to obtain the same result that I want. How can I get things to speed up?

  • 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-06-13T09:44:53+00:00Added an answer on June 13, 2026 at 9:44 am

    That’s right, this is not an optimal solution. But the good news is that all of this can be done with one or at most two queries.
    For instance you don’t have to execute the counting query once for each instrument.
    You can use grouping and get all counts with one query:

    List<Instrument> instruments = em().createQuery("SELECT i FROM Instrument AS i").getResultList();
    
    Query q = em().createQuery("SELECT c.instrument.id, COUNT(c) FROM Component AS c GROUP BY c.instrument.id")
    List<Object[]> counts = q.getResultList();
    
    for (Object[] elem : counts) {
        // do something 
        // elem[0] is instrument ID
        // elem[1] is count
    }
    

    I haven’t check that but you can probably also do everything with one query by putting the second query as a subquery in the first one:

    SELECT i 
    (SELECT COUNT(c) FROM Component AS c WHERE c.instrument.id = i.id)
    FROM Instrument AS i
    

    Similar to the first example, result list elem[0] would be an Instrument and elem[1] the count. It can be less efficient because the DB will have to execute the subquery for each instrument anyway, but it will be still quicker than your code, because it happens fully on DB side (no round-trips to DB for each counting query).

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

Sidebar

Related Questions

Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
Basically I have a large set of data in excel, and I was wondering
Basically I have a list of records. Each record (the VO bean) will have
Basically, each time I click on a graphicsview, I'd like a new QGraphicsEllipseItem to
Basically, I want to be able to run multiple threads - these threads will
Basically I'm going to go a bit broad here and ask a few questions
Basically, when I type these commands in the terminal by hand, the sift program
Basically, I want to only show these fields if checkbox is selected, if it
basically my app has a loginScreen and once logged u can go through many
Basically, I calculated the mean of each row of a matrix > mat <-

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.