Query optimization - table elimination in case of LEFT JOIN but not in case of INNER JOIN

Поиск
Список
Период
Сортировка
От Erki Eessaar
Тема Query optimization - table elimination in case of LEFT JOIN but not in case of INNER JOIN
Дата
Msg-id AM9PR01MB82689D5368D76B63B9923B07FE949@AM9PR01MB8268.eurprd01.prod.exchangelabs.com
обсуждение исходный текст
Список 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

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

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: Tenable Report Issue even after upgrading to correct Postgres version
Следующее
От: Erki Eessaar
Дата:
Сообщение: References to parameters by name are lost in INSERT INTO ... SELECT .... statements in case of routines with the SQL-standard function body