1

Here's the table in Postgresql:

name, ts, value
A, 2017-05-28, 1
A, 2017-05-27, 5
A, 2017-05-26, 2
...
B, 2017-05-28, 9
B, 2017-05-28, 12
...

The size of the table will be over 10 million. I'm trying to execute select count(distinct(name)) from "table"; and it takes me over 240s without ending. Could anyone give some suggestions regarding the way to optimise this scenario, like adding partition like Hive or adding index (which needs to be unique, but the name is duplicate across multiple records). Thanks!

1

1 Answer 1

1

For some reason, Postgres does not optimize count(distinct name) very well. (Intriguingly, Hive -- which has a very different optimizer -- has a similar problem.)

Try running the query this way:

select count(*)
from (select distinct name
      from t
     ) t;

I don't think an index will help, but you can always try using one on t(name).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.