Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EXPLAIN ANALYZE
  2. SELECT products.*,
  3.        category_names.category_name_combined AS category_name_combined,
  4.        COALESCE(product_data.total_sales, 0) AS total_sales,
  5.        COALESCE(product_data.total_costs, 0) AS total_costs,
  6.        COALESCE(product_data.cpo, 0) AS cpo,
  7.        best_base_price,
  8.        best_full_price,
  9.        best_shop_name,
  10.        parent_name,
  11.        count(*) OVER() AS total_count
  12. FROM "products"
  13. LEFT JOIN category_products ON category_products.product_id = products.id
  14. LEFT JOIN categories ON categories.id = category_products.category_id
  15. LEFT JOIN
  16.   (SELECT string_agg(categories.name, '|||| ') AS category_name_combined,
  17.           products.id
  18.    FROM "products"
  19.    LEFT JOIN category_products ON category_products.product_id = products.id
  20.    LEFT JOIN categories ON categories.id = category_products.category_id
  21.    WHERE "products"."company_id" = 119
  22.      AND "products"."deleted_at" IS NULL
  23.    GROUP BY products.id) category_names ON products.id = category_names.id
  24. LEFT JOIN LATERAL
  25.   (SELECT product_id,
  26.           COALESCE(SUM(product_performance_data.sales_conversions), 0) AS total_sales,
  27.           COALESCE(SUM(product_performance_data.costs), 0) AS total_costs,
  28.           (CASE (COALESCE(SUM(product_performance_data.sales_conversions), 0))
  29.                WHEN 0 THEN 0
  30.                ELSE (COALESCE(SUM(product_performance_data.costs), 0)) / (COALESCE(SUM(product_performance_data.sales_conversions), 0))
  31.            END) AS cpo
  32.    FROM "product_performance_data"
  33.    WHERE (product_id = products.id)
  34.      AND (DAY >= DATE('"2013-10-14T13:11:23.963Z"')
  35.           AND DAY <= DATE('"2017-02-28T14:15:58.385Z"'))
  36.    GROUP BY "product_performance_data"."product_id") product_data ON products.id = product_data.product_id
  37. LEFT JOIN LATERAL
  38.   (SELECT product_id,
  39.           base_price AS best_base_price,
  40.           full_price AS best_full_price,
  41.           RAW->>'shop_name' AS best_shop_name
  42.    FROM "google_shopping_prices"
  43.    WHERE (product_id = products.id)
  44.    ORDER BY full_price ASC LIMIT 1) product_prices ON products.id = product_prices.product_id
  45. LEFT JOIN LATERAL
  46.   (SELECT id,
  47.           name AS parent_name
  48.    FROM "products"
  49.    WHERE "products"."company_id" = 119
  50.      AND (shop_product_id = products.parent_id) LIMIT 1) product_parents ON products.id = product_parents.id
  51. WHERE "products"."company_id" = 119
  52.   AND "products"."deleted_at" IS NULL
  53.   AND (full_price_in_cents > (best_full_price * 100)::int)  # If you remove this line, it takes ~700ms. with this line, 400s
  54. GROUP BY products.id,
  55.          category_name_combined,
  56.          total_sales,
  57.          total_costs,
  58.          cpo,
  59.          best_base_price,
  60.          best_full_price,
  61.          best_shop_name,
  62.          parent_name
  63. ORDER BY products.name ASC,
  64.          products.name ASC LIMIT 100
  65. OFFSET 0 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement