Ramdas Jagtap
07/08/2025, 6:01 AMSELECT COUNT(*)
FROM (
SELECT DISTINCT id
FROM table
WHERE responsetype IN ('Sent', 'SENT', 'failed')
)
We would really appreciate your help in tuning our Druid cluster for better performance.John Kowtko
07/08/2025, 12:53 PMselect count(distinct id) from table where ...
However you are doing the exact distinct count, which does require a nested group-by. In your case it is likely high cardinality, which is contributing to the high query latency. Can I assume you also have maxOnDiskStorage set, which is allowing spillover, so the query will complete, it will just take longer to run?
Don't know if you are aware, but if you don't need an exact count, Druid can use sketches for approximate count distinct, using my version of your query above ... just set the query context "useApproximateCountDistinct": true
If you need an exact distinct count then it will be harder ... one thing you could try if your cardinality isn't orders of magnitude larger than the system will allow, is to shard your queries into non-overlapping subsets and add the results together, e.g.
select
(select count(distinct id) from table
where responsetype IN ('Sent', 'SENT', 'failed')
and id < 'N')
+
(select count(distinct id) from table
where responsetype IN ('Sent', 'SENT', 'failed')
and id >= 'N')Ben Krug
07/08/2025, 9:56 PMSELECT COUNT(DISTINCT id)
FROM table
WHERE responsetype IN ('Sent', 'SENT', 'failed')
2. Do you mean that you have exactly two nodes, and 7B records?
If so, have you considered adding nodes?
3. Do you run autocompaction and automatic metadata cleanup? I don't know how many segments you have, but these can help performance.