[英]Is it possible to improve the performance of query with distinct and multiple joins?
[英]Improve distinct query performance
知道我们如何改进这个查询执行吗? (也许有一些预聚合)?
SELECT p.segment, country, count(distinct userid)
from pixel_data_opt p
WHERE country in ('US')
and segment is not null
GROUP BY p.segment, country;
我尝试了以下但没有帮助 -
select segment, country,sum(cnt)
from
(SELECT p.segment, country, userid,count(*) as cnt
from pixel_data_opt p
WHERE country in ('US')
and segment is not null
GROUP BY p.segment, country,userid
)
group by 1,2;
您的第一个查询没有任何问题-尽管,它可能是where country = 'US'
-但是优化器(就 Oracle 而言)足够聪明,可以解决这个问题。
country
列是否已编入索引? 如果没有,那就这样做。
此外,收集表上的统计信息。
如果您发布更多信息,例如涉及的行数,解释计划,因为它显示的数字具有意义,这可能会有所帮助。
对于此查询:
SELECT p.segment, country, count(distinct userid)
FROM pixel_data_opt p
WHERE country in ('US') AND
segment is not null
GROUP BY p.segment, country;
你想要一个表上的索引。 有几种方法。 一种合理的选择是: pixel_data_opt(country, segment, userid)
。
我建议将查询重写为:
SELECT p.segment, 'US' as country, count(distinct userid)
FROM pixel_data_opt p
WHERE country in ('US') AND
segment is not null
GROUP BY p.segment;
并使用上述索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.