From: | Erki Eessaar <erki(dot)eessaar(at)taltech(dot)ee> |
---|---|
To: | "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org> |
Subject: | Query optimization - table elimination in case of LEFT JOIN but not in case of INNER JOIN |
Date: | 2021-11-11 18:29:10 |
Message-ID: | AM9PR01MB82689D5368D76B63B9923B07FE949@AM9PR01MB8268.eurprd01.prod.exchangelabs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-bugs |
Hello
The following observation from PostgreSQL (14) was so unexpected that it seems a bug to me.
Here is a simple scenario.
I created two associated tables and an index on the foreign key column. I generated test data and refreshed the statistics.
The conceptual structure of the tables: [Parent]-1--------------0..*-[Child]
CREATE TABLE Parent(parent_id INTEGER,
extra INTEGER NOT NULL,
CONSTRAINT pk_parent PRIMARY KEY (parent_id));
CREATE TABLE Child (child_id SERIAL,
parent_id INTEGER NOT NULL,
CONSTRAINT pk_child PRIMARY KEY (child_id),
CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES Parent (parent_id));
CREATE INDEX idx_child_parent ON Child (parent_id);
INSERT INTO Parent (parent_id, extra)
SELECT v, (RANDOM()*(10-(-5)))::INT + (-5)
FROM generate_series(1, 1000) AS v;
INSERT INTO Child (parent_id)
SELECT (RANDOM()*(1000-(1)))::INT + (1)
FROM (SELECT generate_series( 1,100000)) AS foo;
ANALYZE;
The following two queries are in this case logically equvalent, i.e., produce always the same result.
However, in case of LEFT JOIN the DBMS applies table elimination technique (the query is executed based on only table Child).
In case of INNER JOIN the table elimination does not take place and the system reads both tables Parent and Child for the query execution.
EXPLAIN ANALYZE SELECT parent_id, child_id
FROM Child LEFT JOIN Parent USING (parent_id);
EXPLAIN ANALYZE SELECT parent_id, child_id
FROM Child INNER JOIN Parent USING (parent_id);
Best regards
Erki Eessaar
From | Date | Subject | |
---|---|---|---|
Next Message | Erki Eessaar | 2021-11-11 19:15:30 | References to parameters by name are lost in INSERT INTO ... SELECT <parameter value> .... statements in case of routines with the SQL-standard function body |
Previous Message | David G. Johnston | 2021-11-11 17:54:43 | Re: Tenable Report Issue even after upgrading to correct Postgres version |