Re: Index filter instead of index condition w/ IN / ANY queries above certain set size

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Danny Shemesh <dany74q(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Index filter instead of index condition w/ IN / ANY queries above certain set size
Date: 2022-11-23 14:24:40
Message-ID: 73d1582fec0ef31e1f2e072e979a875cc851eff6.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2022-11-23 at 15:38 +0200, Danny Shemesh wrote:
> ->  Limit  (cost=0.56..24.17 rows=1 width=16) (actual time=2657.167..2657.167 rows=1 loops=39)
>       Buffers: shared hit=323452 read=887661
>       I/O Timings: read=2369932.536
>       ->  Index Only Scan using idx_hashes on refs refs_1  (cost=0.56..265306.68 rows=11238 width=16) (actual time=2657.162..2657.162 rows=1 loops=39)
>             Index Cond: ((tid = '13371337-1337-1337-1337-133713371337'::uuid) AND (pidh > cte_1.pidh))
>             Filter: (tidh = ANY ('{13391339-1339-1339-1339-133913391339}'::uuid[]))    <<<<<<<<<<<<<<<- Note this line
>             Rows Removed by Filter: 346024
>             Heap Fetches: 1506359
>           Buffers: shared hit=323452 read=887661
>             I/O Timings: read=2369932.536

PostgreSQL thinks that there are enough such rows that it is cheaper to use the index
that supports the ORDER BY. I don't know why there is a difference between = ANY
and = here, but you can use an expression like "ORDER BY pidh + 0" to avoid that.

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2022-11-23 14:25:24 Re: Getting PSQL in Windows to support TAB/Autocomplete via modified readline...
Previous Message Danny Shemesh 2022-11-23 13:38:47 Re: Index filter instead of index condition w/ IN / ANY queries above certain set size