简体   繁体   English

提高不同查询性能

[英]Improve distinct query performance

Any idea of how we can improve this query execution ?知道我们如何改进这个查询执行吗? (maybe with some pre-aggregation)? (也许有一些预聚合)?

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;

I tried the below but it didn't help -我尝试了以下但没有帮助 -

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;

There's nothing wrong with your first query - though, it could have been where country = 'US' - but optimizer (as far as Oracle is concerned) is smart enough to figure it out.您的第一个查询没有任何问题-尽管,它可能是where country = 'US' -但是优化器(就 Oracle 而言)足够聪明,可以解决这个问题。

Is the country column indexed? country列是否已编入索引? If not, do that.如果没有,那就这样做。

Also, gather statistics on the table.此外,收集表上的统计信息。

It would probably help if you posted some more info, eg number of rows involved, explain plan as it shows figures that mean something.如果您发布更多信息,例如涉及的行数,解释计划,因为它显示的数字具有意义,这可能会有所帮助。

For this query:对于此查询:

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;

You want an index on the table.你想要一个表上的索引。 There are several approaches.有几种方法。 One reasonable choice is: pixel_data_opt(country, segment, userid) .一种合理的选择是: pixel_data_opt(country, segment, userid)

I would suggest rewriting the query as:我建议将查询重写为:

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;

and using the above index.并使用上述索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM