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 1ouaM9-0005cL-5k for pgsql-performance@arkaria.postgresql.org; Mon, 14 Nov 2022 14:22:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ouaM7-0002Hc-SQ for pgsql-performance@arkaria.postgresql.org; Mon, 14 Nov 2022 14:22:35 +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 1ouaM7-0002HD-J9 for pgsql-performance@lists.postgresql.org; Mon, 14 Nov 2022 14:22:35 +0000 Received: from mail.mnc.ch ([62.2.77.205]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ouaM4-00070i-Am for pgsql-performance@lists.postgresql.org; Mon, 14 Nov 2022 14:22:34 +0000 X-Virus-Scanned: amavisd-new at mnc.ch Sender: gc@mnc.ch Received: by localhost.localdomain (Postfix, from userid 1000) id EA7671C42DA; Mon, 14 Nov 2022 15:22:27 +0100 (CET) From: Guillaume Cottenceau To: Rick Otten Cc: Ramdip Gill , pgsql-performance@lists.postgresql.org Subject: Re: =ANY(ARRAY) vs =ANY(ARRAY(expr)) performance References: Date: Mon, 14 Nov 2022 15:22:27 +0100 In-Reply-To: (Rick Otten's message of "Mon, 14 Nov 2022 09:11:18 -0500") Message-ID: <87zgctzkd8.fsf@mnc.ch> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Rick Otten writes: > I was able to reproduce a similar issue with using `= ANY(VALUES)` instead of `= ANY(ARRAY)`: > > 1. fast query using =ANY(ARRAY): https://explain.depesz.com/s/dwP8 > 2. slow query using =ANY(ARRAY(expr)): https://explain.depesz.com/s/3hGb > 3. slow query using =ANY(VALUES): https://explain.depesz.com/s/cYrn > > I have found the "ANY" operator to be slow in general. It is almost always faster to use the "<@" operator: > ``` > -- more intuitive: > select > count(*) > from > testarray > where > 'test' = ANY (myarray) > ; > > -- faster: > select > count(*) > from > testarray > where > ARRAY['test'::varchar] <@ myarray > ; > ``` > It is just one of those things, like replacing "OR" with "UNION ALL" whenever possible too, that just make queries faster in PostgreSQL without a > ton of effort or fuss. depends^^ db=> select count(*) from table where uid = any( string_to_array('11290331,11290332,11290333,11290431',',')::int[]); count ------- 4 (1 row) Time: 0.837 ms db=> select count(*) from table where uid = any( string_to_array('11290331,11290332,11290333,11290431',',')::int[]); count ------- 4 (1 row) Time: 0.854 ms db=> select count(*) from table where array[uid] <@ string_to_array('11290331,11290332,11290333,11290431',',')::int[]; count ------- 4 (1 row) Time: 52.335 ms db=> select count(*) from table where array[uid] <@ string_to_array('11290331,11290332,11290333,11290431',',')::int[]; count ------- 4 (1 row) Time: 44.176 ms -- Guillaume Cottenceau