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 5989739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:08:41+00:00 2026-05-22T23:08:41+00:00

I have the following query method that has slow performance: @Override public Map<String, Long>

  • 0

I have the following query method that has slow performance:

@Override
public Map<String, Long> getFeatureCounts() {

    StopWatch timer = new StopWatch();
    timer.start();
    Map<String, Long> resultMap = new LinkedHashMap<String, Long>();  // I want to ensure that the features are in alpha order

    EntityManager em = entityManagerFactory.createEntityManager();

    String jpaQuery = "SELECT F.featureClass, COUNT(DISTINCT F.id) FROM Feature F GROUP BY F.featureClass ORDER BY F.featureClass";
    Query query = em.createQuery(jpaQuery);

    List resultList = query.getResultList();

    for (Object o : resultList) {
        Object[] o2 = (Object[]) o;
        String key = (String) o2[0];
        Long value = (Long) o2[1];
        resultMap.put(key, value);
    }

    em.close();

    timer.stop();
    log.info("getFeatureCounts() executed in " + timer.getTime() + "ms.");

    return resultMap;
}

The table has about 1.3M records, but there is an index on featureClass, and F.id is the primary key. I’m waiting results back in something like the following format:

Airport->20316
Bridge->6509
etc.

LinkedHashMap is a requirement so I can maintain the order of the keys (it’s either that or call Collections.sort() later… kind of a pay now or pay later thing).

Table definition is below:

CREATE TABLE FEATURE(
FEATUREID INTEGER,
FEATURENAME VARCHAR(100),
FEATURECLASS VARCHAR(100),
LAT NUMERIC(14,5),
LNG NUMERIC(14,5),
FEATURESOURCE VARCHAR(10),
PRIMARY KEY(FEATUREID)
) ENGINE = InnoDB;

create index idx_featureclass on FEATURE(FEATURECLASS);

Any help is appreciated.

Jason

  • 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-22T23:08:42+00:00Added an answer on May 22, 2026 at 11:08 pm

    The COUNT(DISTINCT F.id) seems unnecessary. A COUNT(1) would give you the same result. And probably better performance ;-).

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

Sidebar

Related Questions

I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
I have the following query method in my ActiveRecord model: def self.tagged_with( string )
I have the following form that has 2 selects: <form id=form class=form_visitar method=post action=ajax/selects.php>
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
I have the following query of linq to entities. The problem is that it
I have the following calls in one of my classes @Override public Integer save(NewsItem
I've been playing around with Expression Trees. I have the following simple method that
I want to encapsulate the following query into a method that returns a query
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS

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.