Optimize DISTINCT + ORDER BY
Following Slack discussion : https://apache-pinot.slack.com/archives/C011C9JHN7R/p1623782718117700
Context Documents: 100M Segments: 90
Query Getting DISTINCT values for a low cardinality column (45 distinct STRINGs)
Query select DISTINCT(kpi) from kpis takes ~10ms
While select DISTINCT(kpi) from kpis ORDER BY kpi takes more than 300ms
Please have a look at the discussion by @mayankshriv and @kishoreg for more technical details
I would like to take this up please. Please. assign this to me
Done
Thanks!
So I investigated this and noted that in case there is no ORDER BY present, there is a possibility that we will early terminate during a DISTINCT operation. However, in case of ORDER BY being present, DistinctCombineOperator will not allow early termination, thus forcing all segments to be processed.
I do not see how we can avoid this, since ORDER BY will have to ensure that the results are ordered globally, irrespective of the limit.
I think a better way would be to support DISTINCT operation to work on top of dictionary, in case the conditions are matched. I will work on the PR
IIRC, the result returned in both cases was the same, so early terminate may not apply for this case, so I think something else is going on. @wuwenw @Jackie-Jiang can you take a look on the latency difference.
@atris Yes, it would be great to add dictionary based distinct support if you are up for it.
Chatted with @atris offline, there's indeed a bailout that stops scanning records after we hit the record limit during the combine phase of distinct operator in the absence of order-by. Since there is no ordering specified, the engine bails out as soon as it collects enough docs as specified in the LIMIT.
It is just a fluke that the results matched even though we didn't scan all records. For other use cases, early bailout in case of no order-by and small limit may result in incorrect results.
@MrNeocore As an experiment, can you try a really large value of limit in your query? I think the first query performance will also degrade.
@mayankshriv I don't have that big table available right now, but I'll try it out whenever possible :)
@mayankshriv I can confirm a degradation in performance when using a large LIMIT value without ORDER BY
Performance remains better than with ORDER BY though. (~250ms vs 350ms)
Interestingly, adding the same LIMIT to the query with ORDER BY improves performance
In summary (same context as before):
select DISTINCT(kpi) from kpis-> less than 10ms (numDocsScanned: 100000 [out of 101250000])select DISTINCT(kpi) from kpis LIMIT 1000000-> ~250 ms (numDocsScanned: ALL)select DISTINCT(kpi) from kpis ORDER BY kpi-> ~350ms (numDocsScanned: ALL)select DISTINCT(kpi) from kpis ORDER BY kpi LIMIT 1000000-> ~250 ms (numDocsScanned: ALL)
@MrNeocore Not surprised. In the first case, we are still early terminating although with a larger limit as compared to the previous iteration. It is faster than ORDER BY query due to the explanation above.
Should we close this ticket as expected behaviour?
This issue is not impacting my use cases much at all, but should "technically understandable" be considered "expected behavior" (from a user perspective) ?
Of course if it seems there's no way to improve / fix the issue, then let's close it, as "known behavior" maybe ?
Maybe I'm just being pedantic lol
I would say that we document this behaviour and make users aware of this scenario. WDYT?
Since we have agreement, marking the issue as closed.