Do you belong to the group of people who think that Count(distinct…) in a chart is a slow, single-threaded operation that should be avoided?
If so, I can tell you that you are wrong.
Well - it used to be single-threaded and slow, but that was long ago. It was fixed already for – I think – version 9, but the rumor of its slowness lives on like an urban myth that refuses to die. Today the calculation is multi-threaded and optimized.
To prove that Count(distinct…) is faster than what many people think, I constructed a test which categorically shows that it is not slower – it is in fact a lot faster than the alternative solutions.
I created a data model with a very large fact table: 1M, 3M, 10M, 30M and 100M records. In it, I created a secondary key, with a large number of distinct values: 1%, 0.1% and 0.01% of the number of records in the fact table.
The goal was to count the number of distinct values of the secondary key when making a selection. There are several ways that this can be done:
Use count distinct in the fact table: Count(distinct[Secondary ID])
Use count on a second table that just contains the unique IDs: Count([Secondary ID Copy])
Use sum on a field that just contains ‘1’ in the second table: Sum([Secondary ID Count])
I also created a dimension ("Dim" in the “Dim Table”) with 26 values, also randomly assigned to the data in the fact table. Then I recorded the response times for three charts, each using “Dim” as dimension and one of the three expressions above. I made this for four different selections.
Then I remade all measurements using “Dim ID” as dimension, i.e. I moved also the dimension to the fact table. Finally, I loaded all the recorded data into QlikView and analyzed it.
The first obvious result is that the response time increases with the number of records in the fact table. This is hardly surprising…
…so I need to compensate for this: I divide the response times with the number of fact table records and get a normalized response time in picoseconds:
This graph is extremely interesting. It clearly shows that if I use a Count(distinct…) on the fact table, I have a response time that is considerably smaller than if I make a count or a sum in a dimensional table. The table below shows the numbers.
Finally, I calculated the ratios between the response times for having the dimension in the fact table vs. in a dimensional table, and the same ratio for making the aggregation in the fact table vs. in a dimensional table.
This graph shows the relative response time I get by moving the dimension or the aggregation into the fact table. For instance, at 100M records, the response time from a fact table aggregation (i.e. a Count(distinct…)) is only 20% of an aggregation that is made in a dimensional table.
This is the behavior on my mock-up data on my four-core laptop with 16GB. If you make a similar test, you may get a slightly different result since the calculations depend very much on both hardware and the data model. But I still think it is safe to say that you should not spend time avoiding the use of Count(distinct…) on a field in the fact table.
In fact, you should consider moving your ID to the fact table if you need to improve the performance. Especially if you have a large fact table.
Absolutely. I was just trying to refocus the discussion and it looked to me like it was straying into a general COUNT(DISTINCT) is better than SUM(Counter) thread. Your point is "when plotting against a dimension". When counting Fact records and anything else that isn't repeating values (i.e. fully distinct) then Sum(Counter) is faster (in my tests anyway).
THANK YOU for posting this! I have been struggling with creating a Flag counter field for a project I am working on to avoid using Distinct Counts. You have simplified my life! I am sticking with my distinct count so I can mark this metric off as complete
If you need to count the number of distinct values of something, you should not be afraid to use Count(distinct...). Further, you should definetely not aim for a kludgy solution by creating an additional table that really isn't necessary.
But which solution to choose depends on which dimension you have in the chart.
This is probably not a new way to present distinct values by (in this case by customer)
Customer
Start_Date
End_Date
1
01.11.1995
01.06.1996
1
05.08.2000
10.10.2001
1
11.01.2010
When presenting a Straight or Pivot table with distinct customer I often use to move the dates from Dimension to expression and in this case make a MAX(Start_Date) because I am interesting to show the latest contract.