Re: Prepared Statements and Pooling

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Mister Junk <junkmail3568540(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Prepared Statements and Pooling
Date: 2015-08-11 08:05:06
Message-ID: CAF-3MvP3mWOxe2LbSJEw8j4cTtX8gVPkFkrAojyxndCKeXu4sw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11 August 2015 at 06:44, Mister Junk <junkmail3568540(at)gmail(dot)com> wrote:
> I'm using prepared statements to prevent SQL injection, but I have done some
> reading and it seems like using Prepared statements COULD improve
> performance. I understand the concept, but I do not know how to implement
> this.

They could, they could also reduce performance.

Prepared statements store and reuse an earlier generated query plan
for a query, so that's the time you safe.

However, because the query planner can't make use of the actual query
parameter values (they are yet unknown at that point), it can only
generate a general query plan.

If your data is equally distributed for your query parameters, that
won't matter much, but if some values are far more common than others,
that can actually hurt performance.

In case of a normal statement, the query planner has enough
information to decide which plan would be more efficient. With a
prepared statement it has not.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael H 2015-08-11 08:52:28 Postgresql-9.1 CentOS7 effective_cache_size issue
Previous Message Andomar 2015-08-11 07:32:20 Re: Prepared Statements and Pooling