The New SQL Server Approximate Count Distinct Function | Database Journal

The New SQL Server Approximate Count Distinct Function

Written By
Gregory Larsen
Gregory Larsen
Feb 3, 2020
2 minute read

With the introduction of SQL Server 2019, Microsoft introduced a new method to count distinct values. This new method of counting is to use the new function called APPROX_COUNT_DISTINCT(). As the name suggests, this new function doesn’t return the actual count of distinct values, but instead returns an approximate count of the distinct values. By using this new function, you might find your big analytic queries, that count distinct values, will run faster and use less resources.

This function was introduced to solve the memory issues associated with counting distinct values where a large number of distinct values exists. When a large number of distinct values exist, SQL Server at some point is no longer be able to maintain counting distinct values in memory alone. When distinct values can’t be maintained in memory, the database engine need to spill to tempdb. The spilling to tempdb is a costly operations, and the therefore slows down the counting process.

The implementation of APPROX_COUNT_DISTINCT() has a much smaller memory footprint than the tried and true COUNT(DISTINCT) function. Per documentation this new function can estimate the number of distinct values of greater than 1,000,000,000 where the accuracy of the calculated approximate distinct count value is within 2% of the actual distinct count value. And it does this using less than 1.5 KB of memory. If your business need doesn’t require an accurate count value, and is willing to live with little less accuracy provide your distinct count query runs faster, than you might want to check out this new function. Here is an example of using this new function to get an approximate counts:

SELECT APPROX_COUNT_DISTINCT(Locations) AS NumOfLocations, 
WHERE DATEPART (month,BillingDate) IN (1,2,3) 
GROUP BY DATEPART (month,BillingDate);
       DATEPART (month,BillingDate) AS BillingMonth
FROM [dbo].[BillingInfo] 

# # #

» See All Articles by Columnist Gregory A. Larsen

Gregory Larsen

Gregory A. Larsen is a DBA at Washington State Department of Health (DOH). Greg is responsible for maintaining SQL Server and other database management software. Greg works with customers and developers to design and implement database changes, and solve database/application related problems. Greg builds homegrown solutions to simplify and streamline common database management tasks, such as capacity management.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.