4

SQL SERVER - SUM(1) vs COUNT(*) - Performance Observation - SQL Authority with P...

 3 years ago
source link: https://blog.sqlauthority.com/2021/06/25/sql-server-sum1-vs-count-performance-observation/?utm_campaign=sql-server-sum1-vs-count-performance-observation
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SQL SERVER – SUM(1) vs COUNT(*) – Performance Observation

I have received lots of comments and emails about my recent two SQL in Sixty Seconds Videos  COUNT(*) and Index – SQL in Sixty Seconds #175 and COUNT(*) and COUNT(1): Performance Battle – SQL in Sixty Seconds #176. One question that kept on coming up was SUM(1) vs COUNT(*) – Performance Observation. Well, let us see that today.

SQL SERVER - SUM(1) vs COUNT(*) - Performance Observation PerformanceObservation-800x391

Lots of people find it surprising that COUNT(*) and COUNT(1) gave exactly the same performance. Many even asked if which one is better SUM(1) or COUNT(*). The answer is very simple and straightforward. Both are equal.

I have run the demonstration which I have earlier shared here.

During comparison of SELECT I ran the following query:

SET STATISTICS IO ON
GO
SELECT COUNT(*)
FROM TestTable
GO
SELECT COUNT(1)
FROM TestTable
GO
SELECT SUM(1)
FROM TestTable
GO

Now when you compare the performance of the three queries, it is actually the same and they all use the same Narrow Index.

SQL SERVER - SUM(1) vs COUNT(*) - Performance Observation sum1

Additionally, when you compare the statistics IO you will see exactly the same information.

SQL SERVER - SUM(1) vs COUNT(*) - Performance Observation sum2

Essentially SUM(1) vs COUNT(*)  are exactly the same regarding Performance Observation.

Well, that’s it for today. If you liked this video, please do not forget to subscribe to my YouTube Channel – SQL in Sixty Seconds.

Here are my few recent videos and I would like to know what is your feedback about them.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Pinal Dave is an SQL Server Performance Tuning Expert and independent consultant with over 17 years of hands-on experience. He holds a Masters of Science degree and numerous database certifications.

Pinal has authored 13 SQL Server database books and 40 Pluralsight courses. To freely share his knowledge and help others build their expertise, Pinal has also written more than 5,500 database tech articles on his blog at https://blog.sqlauthority.com.

Pinal is an experienced and dedicated professional with a deep commitment to flawless customer service. If you need help with any SQL Server Performance Tuning Issues, please feel free to reach out at [email protected].

Pinal is also a CrossFit Level 1 Trainer (CF-L1) and CrossFit Level 2 Trainer (CF-L2).


Nupur Dave is a social media enthusiast and an independent consultant. She primarily focuses on the database domain, helping clients build short and long term multi-channel campaigns to drive leads for their sales pipeline.


Exclusive Newsletter

Is your SQL Server running slow and you want to speed it up without sharing server credentials? In my Comprehensive Database Performance Health Check, we can work together remotely and resolve your biggest performance troublemakers in less than 4 hours.

Once you learn my business secrets, you will fix the majority of problems in the future.


Have you ever opened any PowerPoint deck when you face SQL Server Performance Tuning emergencies? SQL Server Performance Tuning Practical Workshop is my MOST popular training with no PowerPoint presentations and 100% practical demonstrations.

Essentially I share my business secrets to optimize SQL Server performance.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK