Hello. There is a request to select data for statistics on goods. It is necessary to group the number of orders, views, the amount of orders and other data. With the extraction of no questions.
The main question is grouping by day (even by week and month)
SELECT SUM(orders_payed.seller_summ) AS seller_summ, SUM(orders_payed.partner_summ) AS partner_summ, SUM(orders_payed.cnt) AS orders_payed_count, SUM(visits.cnt) AS count_visits FROM products LEFT JOIN (SELECT product_parent_id, SUM(IF(seller_id=2, seller_summ, 0)) AS seller_summ, SUM(IF(partner_id=2, partner_summ, 0)) AS partner_summ, COUNT(1) AS cnt FROM orders WHERE status=1 AND DATE(payed_at) = "2017-02-16" GROUP BY product_parent_id ) AS orders_payed ON orders_payed.product_parent_id = products.id LEFT JOIN (SELECT product_parent_id, COUNT(1) AS cnt FROM product_visits WHERE DATE(created_at) = "2017-02-16" GROUP BY product_parent_id ) AS visits ON visits.product_parent_id = products.id WHERE products.active = 1 AND products.active_partners_system = 1 AND products.parent_id IS NULL AND products.id IN (...) Roughly speaking, now I have to make 7 such requests changing dates, I need to do one request for 7 days (weeks, months). Those. display statistics for all selected products line by line (by day).
I understand that you need to group in join-s and somehow combine it all.
Thanks to all!
PS: Request reduced to save space)
The issue was resolved by creating an additional cache table.
An additional table that accumulates data in the necessary section from other tables (using triggers) is the best option for storing large amounts of information in order to speed up the work of queries.