3 ms·
You are absolutely right. That's because when a parametric query arrives, the parameters are unbound and the planner cannot take advantage of fit-to-purpose sel
by fdr 12y ago
You are absolutely right. That's because when a parametric query
arrives, the parameters are unbound and the planner cannot take
advantage of fit-to-purpose selectivity estimation. It must instead estimate generically.
Newish versions of Postgres (9.2+ I believe) try to paper over this
surprising effect by re-planning queries a few times to check for cost
stability before really saving the plan. It has proved very
practical.
See http://www.postgresql.org/docs/9.2/static/sql-prepare.html's http://www.postgresql.org/docs/9.2/static/sql-prepare.html's notes
section, reproduced here:
Notes
If a prepared statement is executed enough times, the server may
eventually decide to save and re-use a generic plan rather than
re-planning each time. This will occur immediately if the prepared
statement has no parameters; otherwise it occurs only if the generic
plan appears to be not much more expensive than a plan that depends
on specific parameter values. Typically, a generic plan will be
selected only if the query's performance is estimated to be fairly
insensitive to the specific parameter values supplied.