简体   繁体   English

Java / PostgreSQL-性能的数据优化

[英]Java / PostgreSQL - Data Optimization for Performance

it's been a while. 有一阵子了。 I'm currently struggling on optimizing the performance of my application. 我目前正在努力优化应用程序的性能。 Here's a snippet similar to what I currently have. 这是与我目前拥有的代码段相似的代码段。

A Stored procedure with this query: 此查询的存储过程:

SELECT field_a, field_b, field_c
FROM table_a
WHERE field_a = param_a
AND field_b = param_b;

In Java, I have this ArrayList of SampleParameterClass with attributes of paramA and paramB declared as String . 在Java中,我有这个SampleParameterClass ArrayList ,其paramAparamB属性声明为String

ArrayList<SampleParameterClass> sampleParams = new ArrayList<SampleParameterClass>();

sampleParams.add(new SampleParameterClass(stringValueForParamA, stringValueForParamB);

Let's assume that sampleParams is populated by hundreds of elements. 假设sampleParams由数百个元素组成。

try
    {
        conn = ConnectionPool.getConnection();

        con.setAutoCommit(false);

        CallableStatement myProcedure = connection.prepareCall("{ ? = call my_proc_name(?, ?) }");

        for(int paramCounter = 0; paramCounter < sampleParams.size(); paramCounter ++){

            myProcedure.registerOutParameter(1, Types.OTHER);
            myProcedure.setObject(2, sampleParams.get(paramCounter).getParamA);
            myProcedure.setObject(3, sampleParams.get(paramCounter).getParamB);
            myProcedure.execute();

            ResultSet rs = (ResultSet) myProcedure.getObject(1);

            while (rs.next()) {

                String resultA = rs.getString("param_a");
                String resultB = rs.getString("param_b");
                String resultC = rs.getString("param_c");

                myArrayListOfResult.add(new Result(resultA, resultB, resultC);

            }
        }

    }
    catch (SQLException e) {
        myProcedure.close();
        conn.close();
    }
}

Basically, what the code above does is to loop throughout all the parameters in the ArrayList and call the stored procedure for every parameter. 基本上,上面的代码要做的是循环遍历ArrayList中的所有参数,并为每个参数调用存储过程。 If I have a thousands of data to be processed, it would be slow as a snail isn't it? 如果我有成千上万的数据要处理,那会很慢,因为蜗牛不是吗?

My question would be: 我的问题是:
1) Is it possible to call the procedure just once even though I have thousands of parameters in queue? 1)即使我在队列中有成千上万个参数,也可以只调用一次该过程吗? Something similar to passing the whole ArrayList of parameters. 类似于传递整个ArrayList参数。
2) If above question cannot be answered, are there any method to minimize the process? 2)如果以上问题无法回答,是否有任何方法可以最小化该过程?

Note: 注意:
-Same question for a procedure with an INSERT statement. -对于带有INSERT语句的过程的相同问题。
-I haven't found a similar question to this, if I missed something. 如果我错过了什么,我还没有找到类似的问题。 Please do post a URL to that question and I'll mark it as an answer to this question. 请在该问题上张贴一个URL,我会将其标记为该问题的答案。
-I haven't found a reading material something like passing a list in a procedure. -我还没有找到阅读材料,例如在过程中传递列表。

Thanks, any answer is much appreciated. 谢谢,任何答案都非常感谢。

Why not pass the parameters as a delimtered string and then use string_to_array(text, text [, text]) on the postgresql side to convert the string into an array. 为什么不将参数作为扩展字符串传递,然后在postgresql端使用string_to_array(text,text [,text])将字符串转换为数组。

See http://www.postgresql.org/docs/9.1/static/functions-array.html 参见http://www.postgresql.org/docs/9.1/static/functions-array.html

and

Postgres integer arrays as parameters? Postgres整数数组作为参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM