Postgresql 与语句相比,使用preparedStatement进行查询要慢得多

Postgresql 与语句相比,使用preparedStatement进行查询要慢得多,postgresql,jdbc,Postgresql,Jdbc,我有两个不同的方法,它们包含相同的SQL查询。第一种使用preparedStatement,速度非常慢 public String getPropertyPreparedStatement(String address) throws Exception { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; String content = null;

我有两个不同的方法,它们包含相同的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
当我直接在数据库上运行查询时,它也非常快,很像使用语句而不是preparedStatement的方法


为什么准备好的报表要慢得多?在仍然能够在查询中使用占位符的情况下,我必须使用哪些选项来保持使用语句的性能?

您准备的语句将
完整地址
转换为
文本
(Postgres的内置文本类型),而您的表似乎是使用
citext
(不区分大小写)文本类型创建的(或者,您在
full\u address::text
)上缺少索引。也许可以尝试在
full\u address::text
上创建索引,看看您准备的语句是否能找到它

另一个选项是为
完整地址
列使用
文本
类型,然后在
较低(完整地址)
上创建功能索引——该选项的适口性取决于您的要求

我认为部分问题在于JDBC不知道
citext
类型,因此除非您能让JDBC将您的地址作为
citext
类型发送到数据库,否则它将被查询计划器解释为
text
,就像您的
setString()
方法一样

有趣的是,我


披露:我为

工作,你是对的,这就是问题所在,我通过将其转换为
citext
示例:
WHERE full\u address=?::citext