Re: How bad is using queries with thousands of values for operators IN or ANY?

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема Re: How bad is using queries with thousands of values for operators IN or ANY?
Дата
Msg-id 699b4b9a-e67f-4823-dcc6-1be2a61ef217@gmx.net
обсуждение исходный текст
Ответ на Re: How bad is using queries with thousands of values for operators IN or ANY?  (Thorsten Schöning <tschoening@am-soft.de>)
Ответы Re: How bad is using queries with thousands of values for operators IN or ANY?  (Pavel Stehule <pavel.stehule@gmail.com>)
Список pgsql-general
Thorsten Schöning schrieb am 31.08.2020 um 12:37:
> So for what query size or number of IDs to compare in IN would you
> consider a different approach at all?


In my experience "hundreds" of IDs tend to be quite slow if used with an IN clause.

Rewriting the IN to a JOIN against a VALUES clause is very often faster:

So instead of:

  select *
  from t
  where id in (1,2,3, .... ,500);

using this:

  select *
  from t
    join (
       values (1),(2),(3),...(500)
    ) as x(id) on x.id = t.id

produces more often than not a more efficient execution plan (assuming no values are duplicated in the IN list)

Obviously I don't know if such a re-write is even feasible though.

Thomas




В списке pgsql-general по дате отправления:

Предыдущее
От: Dirk Krautschick
Дата:
Сообщение: High Availability, guarantee to use sync nodes
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Query performance with min and filter