From: Shanti-Dominique <shanti@mildred.fr>
To: pgsql-general@lists.postgresql.org
Subject: Re: Efficient rows filter for array inclusion with gin index
Date: Wed, 28 Feb 2024 16:49:40 +0100
Message-ID: <f2dd527d-89d0-4bc0-a0a5-52b41f109c4f@mildred.fr> (raw)
In-Reply-To: <7dec2a11-fc1f-47d1-8354-7bee04f0a835@mildred.fr>
References: <7dec2a11-fc1f-47d1-8354-7bee04f0a835@mildred.fr>
Replying to myself after more investigation.
On 28/02/2024 12:05, Shanti-Dominique wrote:
>
>
> 2)
> SELECT *
> FROM items i1
> JOIN item_paths p1 ON i1.ref_id = p1.ref_id
> JOIN items i2 ON ARRAY[i2.ref_id] <@ p1.item_path
> WHERE ...
>
> 2) uses the operator <@ which is supported by the gin index, the test
> for inclusion is fast and the query does not run a sequential scan
> over the whole "item_paths" table. However, because of the
> ARRAY[i2.ref_id] construct, it performs a sequential scan on i2.
I was under the assumption that the ARRAY[] construct prevented
postgresql from efficiently using the index on the other side of the
operator, but I think I was mistaken. On a database full of data, I
tried getting around this but did not see any improvement of performance.
First I tried to add an index on the single element array:
CREATE FUNCTION uuidarr(ref_id uuid) RETURNS uuid[]
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT
RETURN ARRAY[ref_id];
CREATE INDEX items_ref_id_arr2_idx ON items USING gin (uuidarr(ref_id));
EXPLAIN
SELECT i2.*
FROM items i1
JOIN item_paths p1 ON i1.ref_id = p1.ref_id
JOIN items i2 ON uuidarr(i2.ref_id) <@ p1.item_path
WHERE i1.name = 'a';
The performance was even worse. Then I tried with a generated column:
CREATE TABLE items (
ref_id uuid DEFAULT public.gen_random_uuid() NOT NULL,
ref_id_array uuid[] GENERATED ALWAYS AS (uuidarr(ref_id)) STORED,
name character varying,
parent_ref_id uuid
);
CREATE INDEX items_ref_id_array_idx ON items USING gin (ref_id_array);
EXPLAIN
SELECT i2.*
FROM items i1
JOIN item_paths p1 ON i1.ref_id = p1.ref_id
JOIN items i2 ON i2.ref_id_array <@ p1.item_path
WHERE i1.name = 'a';
The performance was very similar to the query with ARRAY[...]
It seems there is no good solution for the general case, apart from
changing the structure of my dataset and removing the use of arrays
entirely.
I think I'll update my codebase and use <@ where it makes sense and =
ANY in other places, but it'll be difficult to know for sure without
running the query which one will be better.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: shanti@mildred.fr
Subject: Re: Efficient rows filter for array inclusion with gin index
In-Reply-To: <f2dd527d-89d0-4bc0-a0a5-52b41f109c4f@mildred.fr>
References: <7dec2a11-fc1f-47d1-8354-7bee04f0a835@mildred.fr> <f2dd527d-89d0-4bc0-a0a5-52b41f109c4f@mildred.fr>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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