How can I make this SQL query more efficient?
SELECT
(SELECT COUNT(*) FROM table WHERE price < 10) AS priceUnder10,
(SELECT COUNT(*) FROM table WHERE price BETWEEN 10 AND 20) AS price10to20,
(SELECT COUNT(*) FROM table WHERE price > 20) AS priceOver20,
(SELECT COUNT(*) FROM table WHERE colour = 'Red') AS colourRed,
(SELECT COUNT(*) FROM table WHERE colour = 'Green') AS colourGreen,
(SELECT COUNT(*) FROM table WHERE colour = 'Blue') AS colourBlue;
I already have indexes in the columns priceand colour, therefore, I am looking for the best way to aggregate data.
I have reviewed the use of GROUP BY, HAVINGand independent connections and window functions, but can not decide how to achieve the same result.
Any suggestions that are very much appreciated.
source
share