I have a big table (100M records) with keywords like these:
('water'),
('mineral water'),
('water bottle'),
('big bottle of water'),
('coke'),
('pepsi')
and I want to select all records excluding keywords where there is a regex match with at least one record of another table.
For example, the exclusion table contains:
- water
- wine
- glass
So I have to select all records from keywords table but excluding all those with a phrase match:
- keyword that are equal to 'water' or 'wine' or 'glass'
- keyword that starts with 'water' or 'wine' or 'glass'
- keyword that ends with 'water' or 'wine' or 'glass'
- keyword that contains 'water' or 'wine' or 'glass' in the middle between two spaces "waterize" do not to be excluded.
Here a pseudo-sql. Desidered output are only records: "coke", "pepsi".
CREATE TABLE keywords (
query TEXT
);
CREATE TABLE negatives (
text TEXT
);
INSERT INTO keywords
(query)
VALUES
('water'),
('mineral water'),
('water bottle'),
('big bottle of water'),
('coke'),
('pepsi');
INSERT INTO negatives (text) VALUES ('water', 'glass', 'wine');
SELECT *
FROM keywords
WHERE NOT (
query ~~ ('% ' || 'water' || ' %') OR
query ~~ ( 'water' || ' %') OR
query ~~ ('% ' || 'water') OR
query ~~ ('water')
)
https://www.db-fiddle.com/f/4ufuFAXKf7mi5yefNQqoXM/33
This needs to be performance efficient because keywords table is very large (100M records) and "exclusion" table is very small (<100 records)