I have two large tables and I need to fit them together. Matching should not be a clear comparison. I used trigrams, Levenshtein's formula, but I get very weak performance. Maybe someone can help improve performance. The size of table A is about 200 thousand rows, the size of table B is about 600 thousand rows.
CREATE TABLE TBL_A(NAME VARCHR,SURNAME VARCHAR, BIRTH_DATE DATE, TABLE_B_ID INT4);
CREATE TABLE TBL_B(ID INT4, NAME VARCHR, SURNAME VARCHAR, BIRTH_DATE DATE);
--variant 1
SET pg_trgm.similarity_threshold = 0.8;
UPDATE TBL_A A SET TABLE_B_ID = B.ID
FROM TBL_B B
WHERE A.NAME % B.NAME
AND A.SURNAME % B.SURNAME
AND ABS(A.BIRTH_DATE ::DATE - B.BIRTH_DATE ::DATE)<=1
--variant 2
UPDATE TBL_A A SET TABLE_B_ID = B.ID
FROM TBL_B B
WHERE A.NAME = B.NAME
AND A.SURNAME = B.SURNAME
AND ABS(A.BIRTH_DATE ::DATE - B.BIRTH_DATE ::DATE)<=1
--variant 3
UPDATE TBL_A A SET TABLE_B_ID = B.ID
FROM TBL_B B
WHERE levenshtein_less_equal (A.NAME ,B.NAME,2)<=2
AND levenshtein_less_equal (A.SURNAME ,B.SURNAME,2)<=2
AND ABS(A.BIRTH_DATE ::DATE - B.BIRTH_DATE ::DATE)<=1
All of these options had very bad performance ( near about 7 hour). I tried creating indexes but didn't get much speed up
CREATE INDEX ind_a_name ON TBL_A USING gist(NAME trm_gist_ops);
CREATE INDEX ind_a_Surname ON TBL_A USING gist(SURNAME trm_gist_ops);