Hi how can I optimse my two queries for average and margin calculation?
The table has over 3 million records.
On a small data set the following queries work fine but on the big data set the queries block because of time out.
Table script:
CREATE TABLE `Prices` (`country` text,`Date` int,`items` varchar(250),`priceEuro` int,`url` varchar(250))
PriceAVG View calculation:
CREATE VIEW
PriceAVG AS
Select distinct
Prices.country,
Prices.items,
(SELECT ROUND(AVG(priceEuro)) FROM Prices AS T1 WHERE T1.items = Prices.items AND T1.country = Prices.country) AS average
FROM Prices;
Margin calculation:
CREATE VIEW
Margin AS
SELECT
Prices.country,
Prices.items,
Prices.priceEuro,
PriceAVG.average,
(PriceAVG.average - Prices.priceEuro) AS margin,
Prices.url
FROM Prices
INNER JOIN PriceAVG ON Prices.items = PriceAVG.items AND Prices.country = PriceAVG.country
WHERE EXISTS (SELECT 1 FROM PriceAVG WHERE Prices.items = PriceAVG.items AND PriceAVG.country = 'Canada' )
ORDER BY `margin` DESC
2
Answers
Simplified version of your data might look like
The avg can be calculated in a subquery and joined to main query like this
If this is too simple for you consider adding representative sample data as text to the question.
Not sure why you are using a sub-select
Should calculate the average per country/items. For the margin it is probably easiest to use a window function: