pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedWhere col like 'abc%' with PreparedStatement
3+ messages / 2 participants
[nested] [flat]
* Where col like 'abc%' with PreparedStatement
@ 2021-03-22 15:52 Andy Fan <zhihui.fan1213@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Andy Fan @ 2021-03-22 15:52 UTC (permalink / raw)
To: pgsql-jdbc@lists.postgresql.org
The real SQL I want to execute is SELECT * FROM t WHERE col like 'abc%';
This query
probably can go with IndexScan since it is prefixed with 'abc'. However I
am confused when I want
to use PreparedStatement.
For example:
List<Map<String, Object>> imap = jdbcTemplate.queryForList("select *
from tm where a like ?",
new Object[] {"a" + "%"}
);
The parse tree is sent to postgreSQL is "a like ?", server knows nothing
about if it is prefixed with
something or not, so probably Index Scan can't be selected. So what the
user can use the prepared
Statement and use the IndexScan for the above case?
Thanks
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Where col like 'abc%' with PreparedStatement
@ 2021-03-22 15:56 Tom Lane <tgl@sss.pgh.pa.us>
parent: Andy Fan <zhihui.fan1213@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2021-03-22 15:56 UTC (permalink / raw)
To: Andy Fan <zhihui.fan1213@gmail.com>; +Cc: pgsql-jdbc@lists.postgresql.org
Andy Fan <zhihui.fan1213@gmail.com> writes:
> The real SQL I want to execute is SELECT * FROM t WHERE col like 'abc%';
> This query
> probably can go with IndexScan since it is prefixed with 'abc'. However I
> am confused when I want
> to use PreparedStatement.
> For example:
> List<Map<String, Object>> imap = jdbcTemplate.queryForList("select *
> from tm where a like ?",
> new Object[] {"a" + "%"}
> );
> The parse tree is sent to postgreSQL is "a like ?", server knows nothing
> about if it is prefixed with
> something or not, so probably Index Scan can't be selected. So what the
> user can use the prepared
> Statement and use the IndexScan for the above case?
The plancache's "custom plan vs generic plan" mechanism is meant to
deal with cases like that. It doesn't always get it right, but if
you consistently use actual values like "abc%" then it will notice
that inserting the parameter value produces a significantly better
plan, and do things that way instead of making a generic plan that
doesn't depend on the parameter value.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Where col like 'abc%' with PreparedStatement
@ 2021-03-23 00:12 Andy Fan <zhihui.fan1213@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 3+ messages in thread
From: Andy Fan @ 2021-03-23 00:12 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-jdbc@lists.postgresql.org
On Mon, Mar 22, 2021 at 11:56 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Andy Fan <zhihui.fan1213@gmail.com> writes:
> > The real SQL I want to execute is SELECT * FROM t WHERE col like 'abc%';
> > This query
> > probably can go with IndexScan since it is prefixed with 'abc'. However
> I
> > am confused when I want
> > to use PreparedStatement.
>
> > For example:
> > List<Map<String, Object>> imap =
> jdbcTemplate.queryForList("select *
> > from tm where a like ?",
> > new Object[] {"a" + "%"}
> > );
>
> > The parse tree is sent to postgreSQL is "a like ?", server knows nothing
> > about if it is prefixed with
> > something or not, so probably Index Scan can't be selected. So what the
> > user can use the prepared
> > Statement and use the IndexScan for the above case?
>
> The plancache's "custom plan vs generic plan" mechanism is meant to
> deal with cases like that. It doesn't always get it right, but if
> you consistently use actual values like "abc%" then it will notice
> that inserting the parameter value produces a significantly better
> plan, and do things that way instead of making a generic plan that
> doesn't depend on the parameter value.
>
> regards, tom lane
>
Thank you Tom! Actually I have set plan_cache_mode to force_generic_plan
and I do want 1). Use a generic plan to save the planning cost. 2). still
use the
IndexScan. 3). we CAN assume the user ALWAYS input "abc%"-like value (not
%abc%') for my case. I am searching for a method to tell this fact to the
server. Is it something we can do?
--
Best Regards
Andy Fan (https://www.aliyun.com/)
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2021-03-23 00:12 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-22 15:52 Where col like 'abc%' with PreparedStatement Andy Fan <zhihui.fan1213@gmail.com>
2021-03-22 15:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-03-23 00:12 ` Andy Fan <zhihui.fan1213@gmail.com>
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