DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE bigstrings
( col1 varchar
, col2 varchar
, col3 varchar
);
INSERT INTO bigstrings(col1, col2, col3) VALUES
('abc1','def1','ghi1')
,('abc1','g1','g2')
,('g3','g1','g4')
;
CREATE TABLE allstrings
( num BIGSERIAL NOT NULL PRIMARY KEY
, string varchar NOT NULL UNIQUE
);
CREATE TABLE nostrings
( col1 BIGINT REFERENCES allstrings(num)
, col2 BIGINT REFERENCES allstrings(num)
, col3 BIGINT REFERENCES allstrings(num)
);
INSERT INTO allstrings( string)
SELECT DISTINCT col1 FROM bigstrings bs
-- not needed on empty allstrings table.
-- WHERE NOT EXISTS ( SELECT * FROM allstrings nx WHERE nx.string = bs.col1)
;
INSERT INTO allstrings( string)
SELECT DISTINCT col2 FROM bigstrings bs
WHERE NOT EXISTS ( SELECT * FROM allstrings nx WHERE nx.string = bs.col2)
;
INSERT INTO allstrings( string)
SELECT DISTINCT col3 FROM bigstrings bs
WHERE NOT EXISTS ( SELECT * FROM allstrings nx WHERE nx.string = bs.col3)
;
INSERT INTO nostrings(col1,col2,col3)
SELECT s1.num, s2.num, s3.num
FROM bigstrings bs
JOIN allstrings s1 ON s1.string = bs.col1
JOIN allstrings s2 ON s2.string = bs.col2
JOIN allstrings s3 ON s3.string = bs.col3
;
SELECT * FROM nostrings;
结果:
col1 | col2 | col3
------+------+------
2 | 3 | 6
2 | 4 | 7
1 | 4 | 5
(3 rows)