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:
WITH t1 AS (SELECT
a.id,
AVG(standard_qty) std_avg,
AVG(poster_qty) pos_avg,
AVG(glossy_qty) gloss_avg
FROM
accounts a
JOIN
orders o
ON a.id = o.account_id
GROUP BY
1
ORDER BY
NULL), t2 AS (SELECT
MAX(t1.std_avg) max_std_avg,
MAX(t1.pos_avg) max_pos_avg,
MAX(t1.gloss_avg) max_gloss_avg
FROM
t1) SELECT
std_id,
t2.max_std_avg,
pos_id,
t2.max_pos_avg,
glos_id,
t2.max_gloss_avg
FROM
(SELECT
(SELECT
t1.id std_id
FROM
t1,
t2
WHERE
t1.std_avg = t2.max_std_avg),
(SELECT
t1.id pos_id
FROM
t1,
t2
WHERE
t1.pos_avg = t2.max_pos_avg),
(SELECT
t1.id glos_id
FROM
t1,
t2
WHERE
t1.gloss_avg = t2.max_gloss_avg)) foo,
t1,
t2