In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `product` ADD INDEX `product_idx_id` (`id`);
ALTER TABLE `purchases_log` ADD INDEX `purchases_log_idx_store_id` (`store_id`);
ALTER TABLE `purchases_log` ADD INDEX `purchases_log_idx_user_id` (`user_id`);
ALTER TABLE `user` ADD INDEX `user_idx_id` (`id`);
SELECT
u.id user,
p.id product_purchased,
p.name product_name,
pl.store_id store,
COUNT(*) occurrences,
total_spent,
total_product_purchased,
pl.registration
FROM
purchases_log pl
JOIN
user u
ON pl.user_id = u.id
JOIN
product p
ON pl.product_id = p.id
JOIN
(
SELECT
pl.user_id,
SUM(pl.price) total_spent,
COUNT(pl.product_id) total_product_purchased
FROM
purchases_log pl
GROUP BY
pl.user_id
ORDER BY
NULL
) t1
ON u.id = t1.user_id
WHERE
pl.store_id IN (
1, 2, 3
)
AND occurrences > 1
GROUP BY
user,
product_name
ORDER BY
pl.user_id ASC,
pl.registration ASC