Skip to content

Instantly share code, notes, and snippets.

@x86demon
Created September 24, 2019 15:46
Show Gist options
  • Select an option

  • Save x86demon/0e49892afa65e5c74bc56d5e73b8ec75 to your computer and use it in GitHub Desktop.

Select an option

Save x86demon/0e49892afa65e5c74bc56d5e73b8ec75 to your computer and use it in GitHub Desktop.
-- Create materialized view to store all price list prices as MD5 hash
CREATE MATERIALIZED VIEW prices AS select price_list_id, MD5(array_agg(pp.quantity::text || pp.unit_code || pp.value::text || pp.currency ORDER BY pp.quantity, pp.unit_code, pp.value::text, pp.currency)::text) as pmd5 FROM oro_price_product pp GROUP BY price_list_id;
-- Add index for md5 to improve performance
CREATE INDEX pmd5_idx ON prices(pmd5);
-- SELECT base price list, number of duplicated price lists and concatenated ids of duplicates
select p1.price_list_id, COUNT(p2.price_list_id), array_agg(p2.price_list_id ORDER BY p2.price_list_id) from prices p1 INNER JOIN prices p2 on p1.pmd5 = p2.pmd5 AND p1.price_list_id <> p2.price_list_id WHERE p1.price_list_id = (SELECT MIN(price_list_id) FROM prices WHERE p1.pmd5 = pmd5) GROUP BY p1.price_list_id ORDER BY p1.price_list_id;
-- SELECT overall number of unique price lists
SELECT COUNT(DISTINCT pmd5) from prices;
-- DROP VIEW
DROP MATERIALIZED VIEW prices;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment