4

If we have a prepared statement like:

SELECT my_func($1::text, $2::int)

Is there is any gain in speed if I prepare a statement with this call and do the call via the prepared statement.

4

1 回答 1

3

Let me quote the docs here:

Prepared statements have the largest performance advantage when a single session is being used to execute a large number of similar statements. The performance difference will be particularly significant if the statements are complex to plan or rewrite, for example, if the query involves a join of many tables or requires the application of several rules. If the statement is relatively simple to plan and rewrite but relatively expensive to execute, the performance advantage of prepared statements will be less noticeable.

Emphasize is mine. I think it clearly states in which conditions PREPARE can have benefits.

Still, all languages currently provide a native way to prepare statements (like PHP), so the overall machinery is executed for you behind the scenes.

To make it short:

  • if it is a one-timer from the client, execute directly;
  • if it comes from the application and assumes user input, use your platform and it's functionality to prepare for security reasons;
  • if statement is executed many times within a session, use any means (either PREPARE or platform's functionality) to prepare for performance reasons.
于 2013-02-08T09:00:49.393 回答