How to get query plan information from Postgres into JDBC

后端 未结 3 1891
眼角桃花
眼角桃花 2020-12-31 23:37

I want to capture the cost numbers from the query plan you get when you \'Explain\' a query. Is there any way to get at this data inside of a Java ResultSet(or similar obje

3条回答
  •  隐瞒了意图╮
    2021-01-01 00:15

    An other example with PreparedStatement, this time.

    Like this:

    PreparedStatement preparedStatement = connection.prepareStatement("EXPLAIN (ANALYZE true , VERBOSE true , BUFFERS true)" +
                    "SELECT * FROM Table");
    
    ResultSet resultSet = preparedStatement.executeQuery();
    while (resultSet.next()) {
    System.out.println(resultSet.getString(1));
    }
    

    Or with a bind parameter:

    PreparedStatement preparedStatement = connection.prepareStatement("EXPLAIN (ANALYZE true , VERBOSE true , BUFFERS true)" +
                    "SELECT * FROM Player WHERE id = ?");
    
    preparedStatement.setLong(1, 1);
    
    ResultSet resultSet = preparedStatement.executeQuery();
    while (resultSet.next()) {
          System.out.println(resultSet.getString(1));
    }
    

提交回复
热议问题