You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes count distinct can make the plan too complex. So it will be good to split out the count distinct into a separate subplan and join. Roughly in SQL:
SELECT COUNT(DISTINCT x) v1, COUNT(DISTINCT y) AS v2, approx_set(x) as s1, Approx_set(y) s2 FROM T group by k
can be done as
SELECT * FROM
(SELECT k, COUNT(DISTINCT x) v1, COUNT(DISTINCT y) AS v2 FROM T group by k)
JOIN
(SELECT k, approx_set(x) as s1, Approx_set(y) s2 FROM T group by k)
USING(k)
(Need to take care of nulls - maybe by coalescing or something)
The text was updated successfully, but these errors were encountered:
Or one can do full join/union and do another arbitrary(v) v on all the values for more robustness and ease of handling nulls
SELECT k, arbitrary(v1) v1, arbitrary(v2) v2, arbitrary(s1) s1, arbitrary(s2) s2, FROM
(
SELECT k, COUNT(DISTINCT x) v1, COUNT(DISTINCT y) AS v2, null s1, null s2 FROM T group by k
union all
SELECT k, null v1, null v2, approx_set(x) as s1, Approx_set(y) s2 FROM T group by k
)
group by k
kaikalur
changed the title
Split out distincts and other distinct aggs too - when mixed with other aggs - into a seperate step and join
Split out count distincts and other distinct aggs too - when mixed with other aggs - into a seperate step and join
Dec 10, 2024
Sometimes count distinct can make the plan too complex. So it will be good to split out the count distinct into a separate subplan and join. Roughly in SQL:
SELECT COUNT(DISTINCT x) v1, COUNT(DISTINCT y) AS v2, approx_set(x) as s1, Approx_set(y) s2 FROM T group by k
can be done as
SELECT * FROM
(SELECT k, COUNT(DISTINCT x) v1, COUNT(DISTINCT y) AS v2 FROM T group by k)
JOIN
(SELECT k, approx_set(x) as s1, Approx_set(y) s2 FROM T group by k)
USING(k)
(Need to take care of nulls - maybe by coalescing or something)
The text was updated successfully, but these errors were encountered: