0
Kein Experte für SQL, aber ich habe es geschafft, diese Abfrage funktioniert und liefert die Ergebnisse, die ich brauche. Der Trick besteht darin, dass es funktioniert. Die beiden fraglichen Tabellen enthalten jeweils ca. 6 mil Datensätze. Es läuft derzeit um ca. 3 Minuten, das ist der Weg, wo ich es brauche.Postgres Mehrere Joins
SELECT p.id,
match.weight
FROM store_promotions p
LEFT JOIN
(SELECT *
FROM
(SELECT id,
(facets.weight::int * 1.5) AS weight
FROM store_promotions promos
JOIN -- this will return all promos, but add weight of 1.5 to the ones that are better matched to customer
(SELECT (jsonb_array_elements(product) ->> 'key') AS barcode,
(jsonb_array_elements(product) ->> 'doc_count') AS weight
FROM customer_transaction_facets
WHERE account_id = '1234567890') facets ON promos.products @> to_jsonb(facets.barcode::text)
UNION SELECT id,
(facets.weight::int * .75) AS weight -- this will return all promos, but add weight of .75 for department matches to the ones that are better matched to customer
FROM store_promotions promos
JOIN
(SELECT (jsonb_array_elements(department) ->> 'key') AS department,
(jsonb_array_elements(department) ->> 'doc_count') AS weight
FROM customer_transaction_facets
WHERE account_id = '1234567890') facets ON promos.departments @> to_jsonb(facets.department::text)) matches) AS MATCH ON p.id = match.id WHERE storeid = '637'