Должен работать через несколько секунд на приличной машине.Определение таблицы?Индексы?план запроса?Конфигурация?Память?
SELECT id_predios
FROM Tbl_Predio t1
WHERE NOT EXISTS (
SELECT *
FROM Tbl_Proprietarios t2
WHERE t2.id_predios = t1.id_predios
)
;
РЕДАКТИРОВАТЬ: план запроса с 2 * 999K записей:
Hash Anti Join (cost=29813.47..61463.98 rows=79 width=4) (actual time=3470.658..7142.042 rows=1045 loops=1)
Hash Cond: (t1.id_predios = t2.id_predios)
-> Seq Scan on tbl_predio t1 (cost=0.00..13912.33 rows=999033 width=4) (actual time=0.038..1458.946 rows=999033 loops=1)
-> Hash (cost=13911.54..13911.54 rows=998954 width=4) (actual time=3238.919..3238.919 rows=998954 loops=1)
-> Seq Scan on tbl_proprietarios t2 (cost=0.00..13911.54 rows=998954 width=4) (actual time=0.057..1479.807 rows=998954 loops=1)
Total runtime: 7143.919 ms
(6 rows)
РЕДАКТИРОВАТЬ 2: тестовый сценарий:
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp;
DROP TABLE tmp.Tbl_Predio CASCADE;
DROP TABLE tmp.Tbl_Proprietarios CASCADE;
CREATE TABLE tmp.Tbl_Predio ( id_predios INTEGER NOT NULL );
CREATE TABLE tmp.Tbl_Proprietarios ( id_predios INTEGER NOT NULL );
INSERT INTO tmp.Tbl_Predio ( id_predios) SELECT serie.val
FROM generate_series(1,1000000) AS serie(val)
;
INSERT INTO tmp.Tbl_Proprietarios ( id_predios) SELECT serie.val
FROM generate_series(1,1000000) AS serie(val)
;
DELETE FROM tmp.Tbl_Predio WHERE random() <= 0.001 ;
DELETE FROM tmp.Tbl_Proprietarios WHERE random() <= 0.001 ;
ALTER TABLE tmp.Tbl_Predio ADD PRIMARY KEY (id_predios) ;
ALTER TABLE tmp.Tbl_Proprietarios ADD PRIMARY KEY (id_predios) ;
EXPLAIN ANALYZE
SELECT id_predios
FROM tmp.Tbl_Predio t1
WHERE NOT EXISTS (
SELECT *
FROM tmp.Tbl_Proprietarios t2
WHERE t2.id_predios = t1.id_predios
)
;