Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer is <strong>YES</strong>. Prepared statements cut both ways.</p> <p>On the one hand, the query does not have to be re-planned for every execution, saving some overhead. This can make a difference or be hardly noticeable, depending on the complexity of the query.</p> <p>On the other hand, with uneven data distribution, a one-size-fits-all query plan may be a bad choice. Called with particular values another query plan could be (much) better suited.</p> <p>Running the query with parameter values in place can lead to a different query plan. More planning overhead, possibly a (much) better query plan.</p> <p>Also consider <strong><em>unnamed prepared statements</em></strong> like <a href="https://stackoverflow.com/a/9120641/939860">@peufeu provided</a>. Those re-plan the query considering parameters every time - and you still have safe parameter handling.</p> <p>Similar considerations apply to queries inside PL/pgSQL functions, where queries can be treated as prepared statements internally - unless executed dynamically with <code>EXECUTE</code>. I quote the manual on <a href="http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN" rel="nofollow noreferrer">Executing Dynamic Commands</a>:</p> <blockquote> <p>The important difference is that <code>EXECUTE</code> will re-plan the command on each execution, generating a plan that is specific to the current parameter values; whereas PL/pgSQL may otherwise create a generic plan and cache it for re-use. In situations where the best plan depends strongly on the parameter values, it can be helpful to use <code>EXECUTE</code> to positively ensure that a generic plan is not selected.</p> </blockquote> <p>Apart from that, <a href="http://wiki.postgresql.org/wiki/Performance_Optimization" rel="nofollow noreferrer">general guidelines for performance optimization</a> apply.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload