Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEKtB-0004Um-24 for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 23:45:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lEKt7-00057D-7U for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 23:45:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEKt6-000573-S1 for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 23:45:12 +0000 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lEKt4-00074t-MQ for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 23:45:12 +0000 Received: by mail-oi1-x22e.google.com with SMTP id j1so3937608oiw.3 for ; Mon, 22 Feb 2021 15:45:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=sR9jhmf5y1nebxLnbGb+Dfd5Ib6VSdrin5eLOA9jr6Y=; b=bpYD1qSSE/UW3NF1nItD+lHG6XgbMxAt9u1++G87G4ZQQQLYr6MYzKG4U5VnoRPlXe A1h7E7EWIPdSLrHFlpTXLUm2HImJhurREyg2YXyxiqIY2l9djXmoxpCHYick/Z5PQ+Qw FvNs12WavTDbW81k2hso1087A2z7qessHu5HDiuy5P/nIB8KkYmBoTAN8D8m8qi2AWio yDq1hT8kI2oueYw8biNQAM9K3bY5PNOERyY3AqTLrFU/7yj/b9SastZ2Jt2dRzDSI9En KMA60aeX32W0Ltwhn5hR+YzN27xK93pTciUVBPj0Yt38vNTaAU/8n3OWAwUZYFV5ABUJ QEVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=sR9jhmf5y1nebxLnbGb+Dfd5Ib6VSdrin5eLOA9jr6Y=; b=I7DezijWBJPfDlgAGJU/JyHNCQ3d4R5L44boOazsqTGhUoR3KTV8eqBlLyH7xUtk0R v9ibNKDWRO6fci1xaThBhAhOBjNw2+VAUj8kGUAeSkBxT3aFyC6a/sDikBnYw5AP44RH 0/iGTw2er+NN6McJrTG3qpTBoG8x8adiD9+rIVojA3aXstMJqEfVOPleLQZ0aOchDMr5 rz4HCO7iQFioEvQ/0pS1KJCUXrkmywWSwCp1qyCthmhXLJbw9NV/cW93typtJkPMyRsO OwAhiZHVHpW824DdISH5Niyb3BioVG9I7xk8fXxIT3pkwh/wUFR0Gs63YcxKiBGAt5iq SS9Q== X-Gm-Message-State: AOAM531VbV1c7agxLxkVSi0ELR/u8zsbBNGCSStC+VmMK6KsRtpLWyiS M6MgeioBD7ArJSQw64HFI07YEQyASKPXbw== X-Google-Smtp-Source: ABdhPJxONN4V/6T8Smz+99BHlVwxknCvBD6DWIEfWUwS+9Kp+jJnIGhxXFo2S2EC+VPdvNZP6wcV4A== X-Received: by 2002:a54:468f:: with SMTP id k15mr17205915oic.58.1614037508485; Mon, 22 Feb 2021 15:45:08 -0800 (PST) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id j6sm227478ooa.40.2021.02.22.15.45.07 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 22 Feb 2021 15:45:07 -0800 (PST) Subject: Re: Simple IN vs IN values performace To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <7334ed05-d880-d6c5-46ed-24f6b6491a2a@gmail.com> Date: Mon, 22 Feb 2021 17:45:06 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------BF0C5CE75ED898567A61008D" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------BF0C5CE75ED898567A61008D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------BF0C5CE75ED898567A61008D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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.
--------------BF0C5CE75ED898567A61008D--