pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Simonetti <marks@opalsoftware.co.uk>
To: pgsql-bugs@postgresql.org
Subject: PQLIB: Prepared statement speed oddity
Date: Mon, 1 Feb 2016 13:00:40 +0000
Message-ID: <56AF56F8.2010303@opalsoftware.co.uk> (raw)
In-Reply-To: <20150108212429.11502.18220@wrigleys.postgresql.org>
References: <20150108212429.11502.18220@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hi,
I'm having a strange problem using prepared statements.  I am using them 
in an effort to eliminate large planning times.

I will try and break the problem down : -

1) A query in my database takes over half a second to plan, but often 
only 5ms to 80ms to run depending on the parameter: -

test=# explain analyze select * from myview where site_code = 'M3374';
..
  Planning time: 606.590 ms
  Execution time: 83.735 ms
(203 rows)

test=# explain analyze select * from myview where site_code = 'M3373';  
<=== NOTE different parameter
..
   Planning time: 624.158 ms
  Execution time: 5.275 ms
(213 rows)

2) To stop my application feeling sluggish, I decided to try using a 
prepared statement.

test=# prepare myprep as select * from myview where site_code = $1;
PREPARE

3) Dissapointingly the first few runs still have the planning pause, but 
gives the same execution result as expected:

test=# explain analyze execute myprep('M3374');
..
  Execution time: 83.596 ms
(202 rows)

4) Probably the first 5 or 6 runs are like this; they have the same 
planning pause of over half a second, even though I've "prepared" it...

5) Then after that I suddenly get amazing results, and even the 
execution speed drops substantially for some parameters: -

test=# explain analyze execute myprep('M3374');
..
  Execution time: 40.273 ms
(208 rows)

Half the execution speed!

So far so good...

6) However, if I start again but instead of executing myprep('M3374') 
first, I instead execute myprep('M3373') first, the plan seems to never 
get cached.

DEALLOCATE myprep;
test=# prepare myprep as select * from myview where site_code = $1;
PREPARE

test=# explain analyze select * from myview where site_code = 'M3373';
Half second pause for planning, 5ms exec time.

Repeat the query 5 times.. 10 times.. 20 times.. never changes, never 
improves.  Sometimes on the 5th or 6th the planning actually seems to 
take over 1 second, then go back to normal.

Again, it is STILL only 5ms execution time (only returns 1 row), but 
there is that annoying planning pause even though it is prepared, and 
ran many times.. it is not caching the plan.

7) To re-iterate, once it is cached (using the 'M3374' parameter) it is 
*very* fast with all parameters, but obviously I cannot rely on this in 
a user environment.

I hope the problem makes sense.

Thanks in advance,

Mark.
--



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)  latest in thread

Message-ID: <56AF56F8.2010303@opalsoftware.co.uk>
Permalink:  ../56AF56F8.2010303@opalsoftware.co.uk/
Also on:    postgresql.org/message-id/56AF56F8.2010303@opalsoftware.co.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: marks@opalsoftware.co.uk
  Subject: Re: PQLIB: Prepared statement speed oddity
  In-Reply-To: <56AF56F8.2010303@opalsoftware.co.uk>
  References: <20150108212429.11502.18220@wrigleys.postgresql.org> <56AF56F8.2010303@opalsoftware.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox