Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aQFe9-0001K6-Uo for pgsql-bugs@arkaria.postgresql.org; Mon, 01 Feb 2016 14:40:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aQFe9-00044v-HO for pgsql-bugs@arkaria.postgresql.org; Mon, 01 Feb 2016 14:40:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aQE5z-0002Lc-Cw for pgsql-bugs@postgresql.org; Mon, 01 Feb 2016 13:00:43 +0000 Received: from smarthost03a.mail.zen.net.uk ([212.23.1.20]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1aQE5s-0001yr-KD for pgsql-bugs@postgresql.org; Mon, 01 Feb 2016 13:00:42 +0000 Received: from [88.96.211.225] (helo=ash.forest) by smarthost03a.mail.zen.net.uk with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1aQE5r-0003QF-4K for pgsql-bugs@postgresql.org; Mon, 01 Feb 2016 13:00:35 +0000 Received: from [10.0.0.12] by ash.forest with esmtpa (Exim 4.84) (envelope-from ) id 1aQE5o-0001cu-V6 for pgsql-bugs@postgresql.org; Mon, 01 Feb 2016 13:00:34 +0000 From: Mark Simonetti Subject: PQLIB: Prepared statement speed oddity To: pgsql-bugs@postgresql.org References: <20150108212429.11502.18220@wrigleys.postgresql.org> Message-ID: <56AF56F8.2010303@opalsoftware.co.uk> Date: Mon, 1 Feb 2016 13:00:40 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <20150108212429.11502.18220@wrigleys.postgresql.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam_score: -1.0 X-Spam_score_int: -9 X-Spam_bar: - X-Spam_report: Spam detection software, running on the system "ash.forest", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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: - [...] Content analysis details: (-1.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP X-Originating-smarthost03a-IP: [88.96.211.225] X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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