From: Alex <cdalxndr@yahoo.com>
To: Justin Pryzby <pryzby@telsasoft.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: 15x slower PreparedStatement vs raw query
Date: Tue, 04 May 2021 09:21:32 +0000
Message-ID: <49daf090ac5b1.96e1ada6021ee@yahoo.com> (raw)
References: <3f8c88d8a6ef9.d82fa04cf610d.ref@yahoo.com>
<3f8c88d8a6ef9.d82fa04cf610d@yahoo.com>
<20210503201811.GA27406@telsasoft.com>
Shouldn't this process be automatic based on some heuristics?
Saving 10ms planning but costing 14s execution is catastrophic.
For example, using some statistics to limit planner time to some percent of
of previous executions.
This way, if query is fast, planning is fast, but if query is slow, more
planning can save huge execution time.
This is a better general usage option and should be enabled by default, and
users who want fast planning should set the variable to use the generic
plan.
Justin Pryzby wrote:
On Sun, May 02, 2021 at 07:45:26PM +0000, Alex wrote:
PreparedStatement: 15s
Raw query with embedded params: 1s
See issue on github with query and explain analyze:
https://github.com/pgjdbc/pgjdbc/issues/2145
<https://github.com/pgjdbc/pgjdbc/issues/2145;
| ..PostgreSQL Version? 12
|Prepared statement
|...
|Planning Time: 11.596 ms
|Execution Time: 14799.266 ms
|
|Raw statement
|Planning Time: 22.685 ms
|Execution Time: 1012.992 ms
The prepared statemnt has 2x faster planning time, which is what it's meant
to
improve.
The execution time is slower, and I think you can improve it with this.
https://www.postgresql.org/docs/12/runtime-config-query.html#GUC-PLAN-CACHE_MODE
<https://www.postgresql.org/docs/12/runtime-config-query.html#GUC-PLAN-CACHE_MODE;
|plan_cache_mode (enum)
| Prepared statements (either explicitly prepared or implicitly generated,
for example by PL/pgSQL) can be executed using custom or generic plans.
Custom plans are made afresh for each execution using its specific set of
parameter values, while generic plans do not rely on the parameter values
and can be re-used across executions. Thus, use of a generic plan saves
planning time, but if the ideal plan depends strongly on the parameter
values then a generic plan may be inefficient. The choice between these
options is normally made automatically, but it can be overridden with
plan_cache_mode. The allowed values are auto (the default),
force_custom_plan and force_generic_plan. This setting is considered when a
cached plan is to be executed, not when it is prepared. For more
information see PREPARE.
--
Justin
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: cdalxndr@yahoo.com, pryzby@telsasoft.com
Subject: Re: 15x slower PreparedStatement vs raw query
In-Reply-To: <49daf090ac5b1.96e1ada6021ee@yahoo.com>
* 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