java - 从结果集中获取数据太慢

标签 java postgresql jdbc resultset

使用结果集从 PostgreSQL 数据库中获取数据太慢。

这是我的代码。

for (int i = 0; i < qry_list.size(); i++) {
    try {
        resultSet = statement.executeQuery(qry_list.get(i));
        resultSet.setFetchSize(0);
        while (resultSet.next()) {
            totalfilecrated = totalfilecrated
                    + resultSet.getInt(columnname);
        }
    } catch (SQLException e) {

        e.printStackTrace();
    }
}

这里我尝试在 for 循环中获取数据。这样好吗?

这是我的查询。

用于获取各个组织的 ID(org_unit_id)。

"select org_unit_id from emd.emd_org_unit where org_unit_id 

in(select org_unit_id from emd.emd_org_unit_detail where entity_type_id=1 and is_active=true) and 

is_active=true order by org_unit_name_en";

然后我想获取每个 org_unit_id

的文件数
select count(*) as totalfilecreatedelectronic from fl_file ff

left join vw_employee_details_with_department epd on epd.post_detail_id=ff.file_opened_by_post_fk

where ff.file_nature = 'E' and ((ff.migration_date>='2011-01-01' and ff.migration_date<='2015-01-01') or 

(ff.opening_date >='2011-01-01' and ff.opening_date <='2015-01-01')) and 

epd.departmentid=org_unit_id";

最佳答案

看到您的第二个查询如何已经包含对 org_unit_id 列的引用,您可能会考虑直接加入 emd_org_unit 表:

select org.org_unit_id, count(*) as totalfilecreatedelectronic 
  from fl_file ff
  left join vw_employee_details_with_department epd on epd.post_detail_id=ff.file_opened_by_post_fk
  -- join to active entries in emd_org_unit
  inner join from emd.emd_org_unit org ON epd.departmentid=org.org_unit_id  
         AND org.is_active=true
   where ff.file_nature = 'E' 
        and (
  (ff.migration_date>='2011-01-01' and ff.migration_date<='2015-01-01') or 
  (ff.opening_date >='2011-01-01' and ff.opening_date <='2015-01-01')) 
 -- and now group by org_unit_id to get the counts
 group by org_unit_id

如果您为此创建一个 SQLFiddle,我想事情会变得更加清晰。

关于java - 从结果集中获取数据太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217944/

相关文章:

java - 无法通过 InputStream 访问文件夹

java - 为什么 JasperViewer 只能在本地主机上工作?

java - 使用 Spring Web 服务时的 Enum 类型转换

java - 如何在ebean中使用多个数据库

java - Tomcat、Java 和 Oracle 9. org.apache.naming.NamingContext 查找

java - 终止 : libs to export not found on above classpath:/sikulixlibs/windows/libs64

sql - 使用单列表

postgresql - postgres 从 regexp_split_to_table 生成的行中选择并抛出错误的位置

java - 提交或 conn.setAutoCommit(true)

java - 执行匿名 pl/sql block 并在 java 中获取结果集