Slow function and prepared statement execution
A prepared statement is a valuable technique that rejects SQL injection into your applications as well as allowing you to bypass regular query parsing in order to save overhead. You might use one like the following:
PREPARE getvals (int) AS SELECT * FROM t WHERE t.v=$1; EXECUTE getvals(5);
This returns everything in the rows with a matching v
value. The PREPARE
saves the output from the query parsing and planning stage. You might think that this will always be a win over directly executing the query if it's being executed more than once, because that overhead will then be amortized over more statements.
This isn't necessarily true. When a statement is prepared, the query optimizer can only produce a generic query plan for it, not knowing anything about the actual values that are going to be requested. When you execute a regular query, the statistics about each column available to the optimizer can sometimes do much better than...