与语句相比,用preparedStatement查询要慢得多

大卫

我有两种包含相同SQL查询的不同方法。拳头用的是preparedStatement,这很慢

public String getPropertyPreparedStatement(String address) throws Exception {
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    String content = null;

    try {
        Class.forName("org.postgresql.Driver");
        conn = DataSourceUtils.getConnection(template.getDataSource());

        pst = conn.prepareStatement(
                "EXPLAIN ANALYZE SELECT property.id AS property_id , full_address, street_address, street.street, city.city as city, state.state_code as state_code, zipcode.zipcode as zipcode FROM property INNER JOIN street ON street.id = property.street_id INNER JOIN city ON city.id = property.city_id INNER JOIN state ON state.id = property.state_id INNER JOIN zipcode ON zipcode.id = property.zipcode_id WHERE full_address = ?");
        pst.setString(1, address);

        rs = pst.executeQuery();

        while (rs.next()) {
            // content = rs.getString("street_address");
            System.out.println(rs.getString(1));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pst != null) {
            pst.close();
        }
        if (rs != null) {
            rs.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

    return content;
}

上面方法的解释分析如下

Nested Loop  (cost=1.27..315241.91 rows=1 width=97) (actual time=0.091..688.583 rows=1 loops=1)
  ->  Nested Loop  (cost=0.98..315233.61 rows=1 width=107) (actual time=0.079..688.571 rows=1 loops=1)
        ->  Nested Loop  (cost=0.71..315225.26 rows=1 width=120) (actual time=0.069..688.561 rows=1 loops=1)
              ->  Nested Loop  (cost=0.42..315216.95 rows=1 width=127) (actual time=0.057..688.548 rows=1 loops=1)
                    ->  Seq Scan on property  (cost=0.00..315208.51 rows=1 width=131) (actual time=0.032..688.522 rows=1 loops=1)
                          Filter: ((full_address)::text = '139-Skillman-Ave-Apt-5C-Brooklyn-NY-11211'::text)
                          Rows Removed by Filter: 8790
                    ->  Index Scan using street_pkey on street  (cost=0.42..8.44 rows=1 width=28) (actual time=0.019..0.019 rows=1 loops=1)
                          Index Cond: (id = property.street_id)
              ->  Index Scan using city_id_pk on city  (cost=0.29..8.30 rows=1 width=25) (actual time=0.010..0.010 rows=1 loops=1)
                    Index Cond: (id = property.city_id)
        ->  Index Scan using state_id_pk on state  (cost=0.28..8.32 rows=1 width=19) (actual time=0.008..0.008 rows=1 loops=1)
              Index Cond: (id = property.state_id)
  ->  Index Scan using zipcode_id_pk on zipcode  (cost=0.29..8.30 rows=1 width=22) (actual time=0.010..0.010 rows=1 loops=1)
        Index Cond: (id = property.zipcode_id)
Planning Time: 2.400 ms
Execution Time: 688.674 ms

下面的方法使用语句,我直接在查询中拥有地址以测试性能

public String getPropertyStatement() throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    String content = null;

    try {
        Class.forName("org.postgresql.Driver");
        conn = DataSourceUtils.getConnection(template.getDataSource());
        stmt = conn.createStatement();

        rs = stmt.executeQuery(
                "EXPLAIN ANALYZE SELECT property.id AS property_id , full_address, street_address, street.street, city.city as city, state.state_code as state_code, zipcode.zipcode as zipcode FROM property INNER JOIN street ON street.id = property.street_id INNER JOIN city ON city.id = property.city_id INNER JOIN state ON state.id = property.state_id INNER JOIN zipcode ON zipcode.id = property.zipcode_id WHERE full_address = '139-Skillman-Ave-Apt-5C-Brooklyn-NY-11211'");

        while (rs.next()) {
            // content = rs.getString("street_address");
            System.out.println(rs.getString(1));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stmt != null) {
            stmt.close();
        }
        if (rs != null) {
            rs.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

    return content;
}

对上述方法进行解释分析

Nested Loop  (cost=29.82..65.96 rows=1 width=97) (actual time=0.232..0.235 rows=1 loops=1)
  ->  Nested Loop  (cost=29.53..57.65 rows=1 width=107) (actual time=0.220..0.223 rows=1 loops=1)
        ->  Nested Loop  (cost=29.25..49.30 rows=1 width=120) (actual time=0.211..0.213 rows=1 loops=1)
              ->  Nested Loop  (cost=28.97..41.00 rows=1 width=127) (actual time=0.198..0.200 rows=1 loops=1)
                    ->  Bitmap Heap Scan on property  (cost=28.54..32.56 rows=1 width=131) (actual time=0.175..0.177 rows=1 loops=1)
                          Recheck Cond: (full_address = '139-Skillman-Ave-Apt-5C-Brooklyn-NY-11211'::citext)
                          Heap Blocks: exact=1
                          ->  Bitmap Index Scan on property_full_address  (cost=0.00..28.54 rows=1 width=0) (actual time=0.162..0.162 rows=1 loops=1)
                                Index Cond: (full_address = '139-Skillman-Ave-Apt-5C-Brooklyn-NY-11211'::citext)
                    ->  Index Scan using street_pkey on street  (cost=0.42..8.44 rows=1 width=28) (actual time=0.017..0.017 rows=1 loops=1)
                          Index Cond: (id = property.street_id)
              ->  Index Scan using city_id_pk on city  (cost=0.29..8.30 rows=1 width=25) (actual time=0.010..0.010 rows=1 loops=1)
                    Index Cond: (id = property.city_id)
        ->  Index Scan using state_id_pk on state  (cost=0.28..8.32 rows=1 width=19) (actual time=0.007..0.007 rows=1 loops=1)
              Index Cond: (id = property.state_id)
  ->  Index Scan using zipcode_id_pk on zipcode  (cost=0.29..8.30 rows=1 width=22) (actual time=0.010..0.010 rows=1 loops=1)
        Index Cond: (id = property.zipcode_id)
Planning Time: 2.442 ms
Execution Time: 0.345 ms

当我直接在数据库上运行查询时,它也非常快,非常类似于使用语句而不是prepareStatement的方法。

为什么prepareStatement这么慢?在保持仍然可以在查询中使用占位符的同时,我必须具有哪些选择来保持using语句的性能?

Richyen

您准备好的语句将转换full_addresstext(Postgres的内置文本类型),而您的表似乎是用citext(不区分大小写的)文本类型创建的(或者,您缺少的索引full_address::text)。也许尝试在其上创建一个索引full_address::text,看看您准备好的语句是否可以使用它。

另一种选择是text为该full_address使用一种类型,然后在其上创建功能索引lower(full_address)-该选项的可适性取决于您的需求。

我认为问题的一部分在于JDBC不了解citext类型,因此除非您可以让JDBC将地址作为citext类型发送给数据库,否则查询计划程序会将其解释为text,就像您的setString()方法可能做。

有趣的是,我最近遇到了类似的问题

披露:我为EnterpriseDB(EDB)工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

与台式机相比,UWP上的FillGeometry要慢得多吗?

来自分类Dev

与仅计算答案相比,为什么打印答案要慢得多?

来自分类Dev

使用`database / sql`查询比直接查询数据库要慢得多

来自分类Dev

为什么在Node JS中执行Mysql查询比直接执行Mysql查询要慢得多?

来自分类Dev

为什么使用子查询进行删除比使用简单的ID列表要慢得多?

来自分类Dev

Promela中的Select语句比等效的if语句慢得多?

来自分类Dev

为什么javascript中的变量要慢得多?

来自分类常见问题

为什么复制经过改组的列表要慢得多?

来自分类Dev

为什么Object.create比构造函数要慢得多?

来自分类Dev

为什么在Python中嵌套“ if”比并行“ and”要慢得多?

来自分类Dev

为什么纹理查找比直接计算要慢得多?

来自分类Dev

为什么写入内存比读取内存要慢得多?

来自分类Dev

反转后,小数乘法的速度要慢得多

来自分类Dev

犰狳inplace_plus比“普通” plus操作要慢得多

来自分类Dev

fastLm()比lm()慢得多

来自分类Dev

Powershell-为什么使用Invoke-WebRequest比浏览器下载要慢得多?

来自分类Dev

对于非常接近零的值,双重计算的运行速度要慢得多

来自分类Dev

为什么对于不相同(但相等)的String对象,String.equals要慢得多?

来自分类Dev

为什么AngularJs在iPad(相对于台式机)上要慢得多?

来自分类Dev

为什么对Azure Document DB的第一个请求比随后的请求要慢得多?

来自分类Dev

为什么使用Python生成器遍历二叉树要慢得多?

来自分类Dev

为什么push方法比通过Javascript中的数组索引放置值要慢得多

来自分类Dev

为什么在线程中运行异步操作比纯任务或纯线程操作要慢得多

来自分类Dev

为什么大型邮箱上的PHP imap_headerinfo()函数要慢得多?

来自分类Dev

为什么遍历列表比遍历python中的迭代器要慢得多?

来自分类Dev

为什么取消分配堆内存比分配堆内存要慢得多?

来自分类Dev

为什么a.insert(0,0)比要慢得多[0:0] = [0]?

来自分类Dev

为什么带有参数的pandas.read_sql比内联参数要慢得多

来自分类Dev

使用TensorFlow进行梯度下降比基本的Python实现要慢得多,为什么呢?

Related 相关文章

  1. 1

    与台式机相比,UWP上的FillGeometry要慢得多吗?

  2. 2

    与仅计算答案相比,为什么打印答案要慢得多?

  3. 3

    使用`database / sql`查询比直接查询数据库要慢得多

  4. 4

    为什么在Node JS中执行Mysql查询比直接执行Mysql查询要慢得多?

  5. 5

    为什么使用子查询进行删除比使用简单的ID列表要慢得多?

  6. 6

    Promela中的Select语句比等效的if语句慢得多?

  7. 7

    为什么javascript中的变量要慢得多?

  8. 8

    为什么复制经过改组的列表要慢得多?

  9. 9

    为什么Object.create比构造函数要慢得多?

  10. 10

    为什么在Python中嵌套“ if”比并行“ and”要慢得多?

  11. 11

    为什么纹理查找比直接计算要慢得多?

  12. 12

    为什么写入内存比读取内存要慢得多?

  13. 13

    反转后,小数乘法的速度要慢得多

  14. 14

    犰狳inplace_plus比“普通” plus操作要慢得多

  15. 15

    fastLm()比lm()慢得多

  16. 16

    Powershell-为什么使用Invoke-WebRequest比浏览器下载要慢得多?

  17. 17

    对于非常接近零的值,双重计算的运行速度要慢得多

  18. 18

    为什么对于不相同(但相等)的String对象,String.equals要慢得多?

  19. 19

    为什么AngularJs在iPad(相对于台式机)上要慢得多?

  20. 20

    为什么对Azure Document DB的第一个请求比随后的请求要慢得多?

  21. 21

    为什么使用Python生成器遍历二叉树要慢得多?

  22. 22

    为什么push方法比通过Javascript中的数组索引放置值要慢得多

  23. 23

    为什么在线程中运行异步操作比纯任务或纯线程操作要慢得多

  24. 24

    为什么大型邮箱上的PHP imap_headerinfo()函数要慢得多?

  25. 25

    为什么遍历列表比遍历python中的迭代器要慢得多?

  26. 26

    为什么取消分配堆内存比分配堆内存要慢得多?

  27. 27

    为什么a.insert(0,0)比要慢得多[0:0] = [0]?

  28. 28

    为什么带有参数的pandas.read_sql比内联参数要慢得多

  29. 29

    使用TensorFlow进行梯度下降比基本的Python实现要慢得多,为什么呢?

热门标签

归档