缓慢执行OpenJPA查询

时间:2011-06-07 19:35:52

标签: java openjpa

我有以下查询方法,其性能较慢:

@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;
}

该表有大约130万条记录,但是featureClass上有一个索引,而F.id是主键。我正在等待以下格式的结果:

机场 - &GT; 20316 大桥&GT; 6509 等

LinkedHashMap是一项要求,所以我可以维护密钥的顺序(或者是那个或稍后调用Collections.sort()...现在付费或者以后付款)。

表定义如下:

创建表功能(     FEATUREID INTEGER,     FEATURENAME VARCHAR(100),     FEATURECLASS VARCHAR(100),     LAT NUMERIC(14,5),     LNG NUMERIC(14,5),     FEATURESOURCE VARCHAR(10),     主键(FEATUREID) )ENGINE = InnoDB;

在FEATURE(FEATURECLASS)上创建索引idx_featureclass;

感谢任何帮助。

杰森

1 个答案:

答案 0 :(得分:2)

COUNT(DISTINCT F.id)似乎没必要。 COUNT(1)会给你相同的结果。并且可能性能更好; - )。