![](/img/trans.png)
[英]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.