Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elWww-0003TN-7m for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 09:32:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1elWwu-0002ss-T0 for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 09:32:28 +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_2) (envelope-from ) id 1elWwu-0002sh-LO for pgsql-general@lists.postgresql.org; Tue, 13 Feb 2018 09:32:28 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1elWws-0000RB-5i for pgsql-general@postgresql.org; Tue, 13 Feb 2018 09:32:28 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 7CABC41040; Tue, 13 Feb 2018 10:32:24 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id G3_8mth88UGj; Tue, 13 Feb 2018 10:32:22 +0100 (CET) Message-ID: <1518514340.2525.16.camel@cybertec.at> Subject: Re: Prepared statements (PREPARE and JDBC) are a lot slower than "normal" ones. From: Laurenz Albe To: Robert Zenz , Postgres General Date: Tue, 13 Feb 2018 10:32:20 +0100 In-Reply-To: <5A82AEFF.2020603@sibvisions.com> References: <5A82AEFF.2020603@sibvisions.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Robert Zenz wrote: > We are seeing a quite heavy slow down when using prepared statements in 10.1. > > I haven't done some thorough testing, to be honest, but what we are having is a > select from a view (complexity of it should not matter in my opinion), something > like this: > > prepare TEST (text, int) select * from OUR_VIEW where COLUMNA = $1 and > COLUMNB = $2; > > -- Actual test code follows. > > -- Takes ~2 seconds. > select * from OUR_VIEW where COLUMNA = 'N' and COLUMNB = 35; > > -- Takes ~10 seconds. > execute TEST ('N', 35); > > Both return the same amount of rows, order of execution does not matter, these > times are reproducible. If the same select statement is executed through JDBC it > takes roughly 6 seconds (execution time only, no data fetched at that point). > I'm a little bit at a loss here. Is such a slow down "expected", did we simply > miss that prepared statements are slower? Or is there something else going on > that we are simply not aware of? Most likely, you are seeing the effects of a generic plan being used. During the first five executions, the prepared statement will run a "custom plan" generated with the actual parameters. If the cost estimate of these plans is not cheaper than the cost estimate of the generic plan (without substituting the actual parameters), the generic plan will be used from the sixth execution on. You can compare the execution plans generated with EXPLAIN (ANALYZE, BUFFERS) EXECUTE test ('N', 35); and EXPLAIN (ANALYZE, BUFFERS) select * from OUR_VIEW where COLUMNA = 'N' and COLUMNB = 35; Yours, Laurenz Albe