Comments - COUNT DISTINCT

1 year, 10 months ago Archie Cobbs

Question: Is COUNT DISTINCT optimized to take advantage of sorted values?

For example, if I have a sorted column LAST_NAME, then SELECT COUNT(DISTINCT(LAST_NAME)) FROM PEOPLE can be performed in a constant amount of memory: if you iterate the rows in LAST_NAME order, then you're guaranteed that all distinct last name values appear contiguously, so there's no need to remember which one's you've already seen.

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.