From: Julien Rouhaud <rjuju123@gmail.com>
To: Matthias Apitz <guru@unixarea.de>
Cc: Josef Šimánek <josef.simanek@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: SELECT with LIKE clause makes full table scan
Date: Wed, 26 Jan 2022 20:13:06 +0800
Message-ID: <20220126121306.k6ilsxl7vy2ppmjn@jrouhaud> (raw)
In-Reply-To: <YfEy7Y4gpoMbKlkB@pureos>
References: <YfEoqiHQikgT82PB@c720-r368166>
<CAFp7QwrVFvMr9VOEsEQRBLhAcg9vG1AQwrg8S+mTmJ_f4Dp3sQ@mail.gmail.com>
<YfEy7Y4gpoMbKlkB@pureos>
Hi,
On Wed, Jan 26, 2022 at 12:39:25PM +0100, Matthias Apitz wrote:
>
> > > sisis=# explain (analyze, buffers) select * from d01buch where d01ort like 'Z 9610%' ;
> > > QUERY PLAN
> > > -------------------------------------------------------------------------------------------------------------------------------
> > > Gather (cost=1000.00..680262.71 rows=510 width=952) (actual time=1324.096..1349.429 rows=1 loops=1)
> > > Workers Planned: 4
> > > Workers Launched: 4
> > > Buffers: shared hit=102040 read=560674
> > > -> Parallel Seq Scan on d01buch (cost=0.00..679211.71 rows=128 width=952) (actual time=1117.663..1315.062 rows=0 loops=5)
> > > Filter: (d01ort ~~ 'Z 9610%'::text)
> > > Rows Removed by Filter: 1055853
> > > Buffers: shared hit=102040 read=560674
> > > Planning Time: 2.028 ms
> > > Execution Time: 1349.593 ms
> > > (10 Zeilen)
> > >
> > > Why is this (ignoring the Index) and what could be done?
> [...]
> create INDEX d01ort on d01buch(d01ort) ;/* D01ORT */
That index can't be used with a LIKE that has a trailing wildcard. You need to
either create an index with text_pattern_ops operator class (see
https://www.postgresql.org/docs/current/indexes-opclass.html), or a GIN index
using pg_trgm (which will also work with non-trailing wildcards), see
https://www.postgresql.org/docs/current/pgtrgm.html.
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-general@postgresql.org
Cc: rjuju123@gmail.com, guru@unixarea.de, josef.simanek@gmail.com
Subject: Re: SELECT with LIKE clause makes full table scan
In-Reply-To: <20220126121306.k6ilsxl7vy2ppmjn@jrouhaud>
References: <YfEoqiHQikgT82PB@c720-r368166> <CAFp7QwrVFvMr9VOEsEQRBLhAcg9vG1AQwrg8S+mTmJ_f4Dp3sQ@mail.gmail.com> <YfEy7Y4gpoMbKlkB@pureos> <20220126121306.k6ilsxl7vy2ppmjn@jrouhaud>
* 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