Getting Approximate Count in Microsoft SQL Server

Written By
Gregory Larsen
Gregory Larsen
Feb 4, 2019

With the introduction of SQL Server 2019, there is now a new, faster way to get a list of distinct values in a column. This new way is using the APPROX_COUNT_DISTINCT function. This new function does not return the exact number of distinct values, but instead, as the function name suggests, it only returns an approximate count. It does this by using the HyperLogLog algorithm.

This new function is faster than the COUNT(*) distinct function because it uses less memory. This new function can estimate the number of distinct values greater than 1,000,000,000, while using less than 1.5 KB or memory. Because it uses less memory, less data is spilled to tempdb than the COUNT(*) function. But there is a cost associated using this new function, and that is accuracy.

The APPROX_COUNT_DISTINCT function does not return the actual number of rows with each distinct value, but instead returns an approximate count. The approximate count might be higher or lower than the actual number. According to Microsoft’s documentation, 97% of the time the APPROX_COUNT_DISTINCT function will be within the 2% of the actual value.

If you have a really large table, want a count of distinct value and can live with an approximate count then you might be able to get a performance boost with this new function. Below is an example of how to use this new function:

SELECT APPROX_COUNT_DISTINCT(MyColumn) 
FROM [dbo].[MyTable];
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.

Recommended for you...

Best Online Courses to Learn SQL
Ronnie Payne
Sep 23, 2022
Best Courses for Database Administrators
Ronnie Payne
Jul 22, 2022
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
How Many Databases Can You Name?
Brad Jones
May 11, 2020
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.