Why is the PostgreSQL JDBC prepared statement threshold defaulted to 5?

后端 未结 1 1826
攒了一身酷
攒了一身酷 2021-01-06 11:23

By default, the parameter statement treshold is set to 5, instead of 1. That is,

((PGStatement) my_statement).getPrepareThreshold()

always

相关标签:
1条回答
  • 2021-01-06 11:31

    Server side prepared statements consume server side resources to store the execution plan for the statement. The threshold provides a heuristic that causes statements that are actually used "often" to be prepared. The definition of "often" defaults to 5.

    Note that server side prepared statements can cause poor execution plans because they are not based on the parameters passed during the prepare. If the parameters passed to a prepared statement have a different selectivity on a particular index (for example), then the general query plan of the prepared statement may be suboptimal. As another example, if you have a situation where the execution of the query is much greater than the cost to create an explain plan, and the explain plan isn't properly set due to lack of bind parameters, you may be better off not using server side prepared statements.

    When the driver reaches the threshold, it will prepare the statement as follows:

        if (!oneShot)
        {
            // Generate a statement name to use.
            statementName = "S_" + (nextUniqueID++);
    
            // And prepare the new statement.
            // NB: Must clone the OID array, as it's a direct reference to
            // the SimpleParameterList's internal array that might be modified
            // under us.
            query.setStatementName(statementName);
            query.setStatementTypes((int[])typeOIDs.clone());
        }
    

    The statement name is sent as part of the wire protocol, which tells Postgres to prepare it server side.

    0 讨论(0)
提交回复
热议问题