JDBC Oracle查询。使用setInt()而不是setObject()执行时速度慢

时间:2012-02-01 09:58:58

标签: java sql performance oracle jdbc

我有以下代码片段:

    public static void main(String[] args) throws SQLException {
    Connection c = getConnection();
    long time = System.currentTimeMillis();
    PreparedStatement ps = c.prepareStatement(sqlQuery);
    int index = 1;
    for (String param: parameters) {
        if (isInt(param)) {
            //ps.setInt(index++, Integer.parseInt(param));
            ps.setObject(index++, Integer.parseInt(param), java.sql.Types.NUMERIC , 0);
        } else {
            ps.setString(index++, param);
        }
    }
    displayResult(ps.executeQuery());
    System.out.println("It took " + (System.currentTimeMillis()-time) + ".");

    time = System.currentTimeMillis();
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery(expandParametersInStatement(sqlQuery, parameters));
    displayResult(rs);
    System.out.println("It took " + (System.currentTimeMillis()-time) + ".");
}

使用PreparedStatement执行的查询慢了4000倍。与Statement方法相比。它们给出相同的结果,执行顺序没有太大的区别。

使用setObject()而不是setInt()使PreparedStatement与Statement一样快。

有什么区别?数据库中的演员阵容不是那么贵吗?数据库中的数据类型是NUMBER(10)。我想这是使用的indeces的问题。但是,我不能在带有CAST的SQL Developer(x AS INTEGER)中复制它吗?

感谢。

陈述是:

private static String sqlQuery = "SELECT sum(value) " +
"FROM a monat, " +
"     n jahr, " +
"     kunde kunde " +
"WHERE monat.kunde_nr IN " +
"    (SELECT DISTINCT kunde.kunde_nr " +
"     FROM MASKE_4_KUNDEN kunde " +
"     WHERE kunde.firma_nr = ? " +
"       AND kunde.verkaufsbereich_nr = ? " +
"       AND kunde.vertriebsbereich_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END) " +
"       AND kunde.vertreter_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END)" +
"       AND kunde.konzern_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END) " +
"       AND kunde.geschaeftsjahr = ? " +
"       AND kunde.kunde_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END))" +
"  AND monat.firma_nr = ? " +
"  AND monat.verkaufsbereich_nr = ? " +
"  AND monat.jahr_nr = ? " +
"  AND jahr.kunde_nr = monat.kunde_nr " +
"  AND jahr.firma_nr = monat.firma_nr " +
"  AND jahr.jahr_nr = monat.jahr_nr " +
"  AND jahr.verkaufsbereich_nr = monat.verkaufsbereich_nr " +
"  AND kunde.kunde_nr = monat.kunde_nr " +
"  AND kunde.firma_nr = monat.firma_nr";

2 个答案:

答案 0 :(得分:0)

setInt()方法接收int作为第二个参数(不是Integer)。现在,我知道自动拳击应该有效,但显然它并不总是有效:http://www.coderanch.com/t/550628/JDBC/java/setInt-String-int-Lost 我的猜测是它在旧版本的JDBC中不起作用,它可以在较新版本中运行但仍然有问题...... 我会尝试使用Integer.intValue(),看看它是否更好!

如果有人对此有更多的意见 - 我很乐意听到它!

答案 1 :(得分:0)

我们在Oracle数据库10.2.0.4.0上只有setString()语句的预处理语句中也观察到了相同的行为。

问题已完全解决将jdbc驱动程序从10.2.0.3.0更新为10.2.0.4.0