pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Simple IN vs IN values performace
9+ messages / 7 participants
[nested] [flat]

* Simple IN vs IN values performace
@ 2021-02-22 15:00  Oleksandr Voytsekhovskyy <av@uniweb.ua>
  0 siblings, 2 replies; 9+ messages in thread

From: Oleksandr Voytsekhovskyy @ 2021-02-22 15:00 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Greetings,

We have queries with IN filters with long list of INT values

Sometimes, they running extremely slow, and I have found suggestion to use syntax
Field IN (VALUES(1465), (1478), ...
Instead of 
Field IN (1465, 1478, ...
On some cases it helps, but on other it makes query running 1000+ times slower

So the issue is:
- some queries much faster with simple IN
- some queries much faster with IN + VALUES

Here is explain on case when it’s extremely slow:

->  HashAggregate  (cost=5.78..9.62 rows=385 width=4)
       Group Key: ""*VALUES*"".column1" 
                   ->  Values Scan on ""*VALUES*""  (cost=0.00..4.81 rows=385 width=4)" 
What is the right way to pass long INT values list to IN filter?

I am using PostgreSQL 13.1 on Ubuntu=

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-22 22:23  David G. Johnston <david.g.johnston@gmail.com>
  parent: Oleksandr Voytsekhovskyy <av@uniweb.ua>
  1 sibling, 2 replies; 9+ messages in thread

From: David G. Johnston @ 2021-02-22 22:23 UTC (permalink / raw)
  To: Oleksandr Voytsekhovskyy <av@uniweb.ua>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Monday, February 22, 2021, Oleksandr Voytsekhovskyy <av@uniweb.ua> wrote:

> What is the right way to pass long INT values list to IN filter
>

Don’t.

Pass in a delimited string, then parse that string into an array and use “=
any(array)”.

This has the primary benefit of making the input a single parameter.

David J.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-22 22:36  Michael Lewis <mlewis@entrata.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Michael Lewis @ 2021-02-22 22:36 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Oleksandr Voytsekhovskyy <av@uniweb.ua>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Wouldn't using “= any(array)” change how the query is planned? Or is the
concern just parsing the values?

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-22 23:45  Ron <ronljohnsonjr@gmail.com>
  parent: Oleksandr Voytsekhovskyy <av@uniweb.ua>
  1 sibling, 2 replies; 9+ messages in thread

From: Ron @ 2021-02-22 23:45 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2/22/21 9:00 AM, Oleksandr Voytsekhovskyy wrote:
> Greetings,
>
> We have queries with IN filters with long list of INT values
>
> Sometimes, they running extremely slow, and I have found suggestion to use 
> syntax
> Field IN (VALUES(1465), (1478), ...
> Instead of
> Field IN (1465, 1478, ...
> On some cases it helps, but on other it makes query running 1000+ times slower

Insert the values into a TEMPORARY TABLE, then join that to your main table?

>
> So the issue is:
> - some queries much faster with simple IN
> - some queries much faster with IN + VALUES
>
> Here is explain on case when it’s extremely slow:
>
> ->  HashAggregate  (cost=5.78..9.62 rows=385 width=4)
>         Group Key: ""*VALUES*"".column1"
>                     ->  Values Scan on ""*VALUES*""  (cost=0.00..4.81 rows=385 width=4)"
> What is the right way to pass long INT values list to IN filter?
>
> I am using PostgreSQL 13.1 on Ubuntu

-- 
Angular momentum makes the world go 'round.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-23 02:33  Michael Lewis <mlewis@entrata.com>
  parent: Ron <ronljohnsonjr@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Michael Lewis @ 2021-02-23 02:33 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

>
> Insert the values into a TEMPORARY TABLE, then join that to your main
> table?
>

In my experience, this is very performant but needs an analyze command
after populating the temp table to ensure there are statistics so the plan
doesn't go awry. Otherwise, I'm not sure it is different from a
materialized CTE or a subquery with OFFSET 0 at the end.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-23 09:30  Oleksandr Voytsekhovskyy <young.inbox@gmail.com>
  parent: Michael Lewis <mlewis@entrata.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Oleksandr Voytsekhovskyy @ 2021-02-23 09:30 UTC (permalink / raw)
  To: Michael Lewis <mlewis@entrata.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>

Greetings,

Didn’t get your ideas with  materialized CTE or a subquery with OFFSET 0

Could you please show simple example?

> 23 февр. 2021 г., в 04:33, Michael Lewis <mlewis@entrata.com> написал(а):
> 
> Insert the values into a TEMPORARY TABLE, then join that to your main table?
> 
> In my experience, this is very performant but needs an analyze command after populating the temp table to ensure there are statistics so the plan doesn't go awry. Otherwise, I'm not sure it is different from a materialized CTE or a subquery with OFFSET 0 at the end.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-23 09:35  Alexander Voytsekhovskyy <young.inbox@gmail.com>
  parent: Ron <ronljohnsonjr@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Alexander Voytsekhovskyy @ 2021-02-23 09:35 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

Thanks for suggestion

with tmp tables there are another issue - there are already 5-6 tables and
2-3 IN filters. If i will replace them with tmp tables it may hit query
planner limits and it will become to produce terrible query plans, for
example when genetic query optimizer starts

On Tue, Feb 23, 2021 at 1:45 AM Ron <ronljohnsonjr@gmail.com> wrote:

> On 2/22/21 9:00 AM, Oleksandr Voytsekhovskyy wrote:
>
> Greetings,
>
> We have queries with IN filters with long list of INT values
>
> Sometimes, they running extremely slow, and I have found suggestion to use
> syntax
>
> Field IN (VALUES(1465), (1478), ...
>
> Instead of
>
> Field IN (1465, 1478, ...
>
> On some cases it helps, but on other it makes query running 1000+ times
> slower
>
>
> Insert the values into a TEMPORARY TABLE, then join that to your main
> table?
>
>
> So the issue is:
> - some queries much faster with simple IN
> - some queries much faster with IN + VALUES
>
> Here is explain on case when it’s extremely slow:
>
> ->  HashAggregate  (cost=5.78..9.62 rows=385 width=4)
>        Group Key: ""*VALUES*"".column1"
>                    ->  Values Scan on ""*VALUES*""  (cost=0.00..4.81 rows=385 width=4)"
>
> What is the right way to pass long INT values list to IN filter?
>
> I am using PostgreSQL 13.1 on Ubuntu
>
>
> --
> Angular momentum makes the world go 'round.
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-23 11:39  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Alexander Voytsekhovskyy <young.inbox@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Pavel Stehule @ 2021-02-23 11:39 UTC (permalink / raw)
  To: Alexander Voytsekhovskyy <young.inbox@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>

Hi

út 23. 2. 2021 v 10:36 odesílatel Alexander Voytsekhovskyy <
young.inbox@gmail.com> napsal:

> Thanks for suggestion
>
> with tmp tables there are another issue - there are already 5-6 tables and
> 2-3 IN filters. If i will replace them with tmp tables it may hit query
> planner limits and it will become to produce terrible query plans, for
> example when genetic query optimizer starts
>

you can increase these limits - they are relatively low, and can be
increased on modern CPU.

https://www.postgresql.org/docs/current/runtime-config-query.html

Regards

Pavel


> On Tue, Feb 23, 2021 at 1:45 AM Ron <ronljohnsonjr@gmail.com> wrote:
>
>> On 2/22/21 9:00 AM, Oleksandr Voytsekhovskyy wrote:
>>
>> Greetings,
>>
>> We have queries with IN filters with long list of INT values
>>
>> Sometimes, they running extremely slow, and I have found suggestion to
>> use syntax
>>
>> Field IN (VALUES(1465), (1478), ...
>>
>> Instead of
>>
>> Field IN (1465, 1478, ...
>>
>> On some cases it helps, but on other it makes query running 1000+ times
>> slower
>>
>>
>> Insert the values into a TEMPORARY TABLE, then join that to your main
>> table?
>>
>>
>> So the issue is:
>> - some queries much faster with simple IN
>> - some queries much faster with IN + VALUES
>>
>> Here is explain on case when it’s extremely slow:
>>
>> ->  HashAggregate  (cost=5.78..9.62 rows=385 width=4)
>>        Group Key: ""*VALUES*"".column1"
>>                    ->  Values Scan on ""*VALUES*""  (cost=0.00..4.81 rows=385 width=4)"
>>
>> What is the right way to pass long INT values list to IN filter?
>>
>> I am using PostgreSQL 13.1 on Ubuntu
>>
>>
>> --
>> Angular momentum makes the world go 'round.
>>
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Simple IN vs IN values performace
@ 2021-02-23 11:54  Alexander Voytsekhovskyy <young.inbox@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Alexander Voytsekhovskyy @ 2021-02-23 11:54 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Oleksandr Voytsekhovskyy <av@uniweb.ua>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Greetings,

i have tested both options ANY with string + parsing and simple array - and
there are cases when execution time 100+ times worse than IN

On Tue, Feb 23, 2021 at 12:23 AM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Monday, February 22, 2021, Oleksandr Voytsekhovskyy <av@uniweb.ua>
> wrote:
>
>> What is the right way to pass long INT values list to IN filter
>>
>
> Don’t.
>
> Pass in a delimited string, then parse that string into an array and use
> “= any(array)”.
>
> This has the primary benefit of making the input a single parameter.
>
> David J.
>
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2021-02-23 11:54 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22 15:00 Simple IN vs IN values performace Oleksandr Voytsekhovskyy <av@uniweb.ua>
2021-02-22 22:23 ` David G. Johnston <david.g.johnston@gmail.com>
2021-02-22 22:36   ` Michael Lewis <mlewis@entrata.com>
2021-02-23 11:54   ` Alexander Voytsekhovskyy <young.inbox@gmail.com>
2021-02-22 23:45 ` Ron <ronljohnsonjr@gmail.com>
2021-02-23 02:33   ` Michael Lewis <mlewis@entrata.com>
2021-02-23 09:30     ` Oleksandr Voytsekhovskyy <young.inbox@gmail.com>
2021-02-23 09:35   ` Alexander Voytsekhovskyy <young.inbox@gmail.com>
2021-02-23 11:39     ` Pavel Stehule <pavel.stehule@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