COUNT(*) and COUNT(1): Performance Battle - SQL in Sixty Seconds 176

COUNT(*) and COUNT(1): Performance Battle - SQL in Sixty Seconds 176

Pinal Dave

3 года назад

18,324 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@sambadpathak1356
@sambadpathak1356 - 22.06.2021 19:18

Count(0)

Ответить
@DouglasWStevens
@DouglasWStevens - 22.06.2021 19:24

count ('chocula') 'cereal counting
count ('de Monet') 'It's good to be the king
count ('Dracula') 'Halloween
count ('Vertigo') 'DC comics
count ('Dooku') 'Come to the dark side

Ответить
@paweb.3108
@paweb.3108 - 22.06.2021 19:28

Count(1) is more efficient because you need one key press less ([1] vs [shift][8])

Ответить
@ranagupta8151
@ranagupta8151 - 22.06.2021 19:31

How the count(*) and count(1) perform when there is load on server

Ответить
@ChristopherWolfftickytong
@ChristopherWolfftickytong - 22.06.2021 19:41

I always wondered but never knew for sure. Thanks for clearing that up

Ответить
@ThierryC2373
@ThierryC2373 - 22.06.2021 19:42

So, basically what is the difference between them and what is the point of using count(1) if it performs exactly as count(*)?

Ответить
@debabratabsc
@debabratabsc - 22.06.2021 20:06

Count * will take more time when there are many number of rows with large data types. Reason is simple, Data page will be more compared to count 1
Thanks!

Ответить
@mriveraalvarez
@mriveraalvarez - 22.06.2021 20:06

I tested it against a dataset that is missing a non clustered index. (Real world scenario for some of us)


select count(*) from logging.log where severity = 'Error'
select count(1) from logging.log where severity = 'Error'


Results 1,729,448

What's interesting is that i got different results from the 2 sample runs.

(1 row affected)
Table 'Log'. Scan count 17, logical reads 4137818, physical reads 11, read-ahead reads 4141000, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)
Table 'Log'. Scan count 17, logical reads 4145935, physical reads 4, read-ahead reads 4141382, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.


(1 row affected)
Table 'Log'. Scan count 17, logical reads 4148384, physical reads 1, read-ahead reads 3998292, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)
Table 'Log'. Scan count 17, logical reads 4138450, physical reads 275, read-ahead reads 4084650, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Ответить
@achilleskocaeli
@achilleskocaeli - 22.06.2021 20:23

Thanks Pinal, you are the best

Ответить
@YogeshSharma-um3ov
@YogeshSharma-um3ov - 22.06.2021 20:34

You are champion sir! Keep up the good work

Ответить
@vansree
@vansree - 22.06.2021 21:29

count(*) is faster irrespective of clustered or non-clustered index.

difference is only in few milli-seconds.

Note: In this case, count(*) and count(1) execution plan is almost the same.

I tested on two fact tables (20M+ rows) and one dim table (2M+ rows) ...

Ответить
@TheRealRonMoses
@TheRealRonMoses - 22.06.2021 22:51

I'm a big fan of COUNT(DISTINCT 1) myself. ;)

Ответить
@independent408
@independent408 - 22.06.2021 23:12

If your table has Clustered index built on TWO columns - count(1) performance will be worse.

Ответить
@DavidJones-ff1gh
@DavidJones-ff1gh - 22.06.2021 23:38

iirc you have to go back to SQL 6.5/7.0/2000 era to see the benefit with count(1) vs count(*)

Ответить
@ashu130676
@ashu130676 - 23.06.2021 00:19

Its always good to watch you, good learning all the time .. Thanks Pinal ..

Ответить
@TheRealBalloonHead
@TheRealBalloonHead - 23.06.2021 01:01

SELECT 8, how many times have we all done that by mistake?
Thanks again Pinal!

Ответить
@ManeshBPandu
@ManeshBPandu - 23.06.2021 04:23

Thanks Pinal, nice approach. I love all your articles there is one question, may be you have already answered

select columns from tablename where column in (@comaseperatorvalues) --> this is not working

select columns from tablename where column in ('1','2','3') --> this is working can i know the correct reason please

Ответить
@muzinkuhlu2592
@muzinkuhlu2592 - 23.06.2021 10:10

We Love You Pinal Dave

Ответить
@niteshsharma0229
@niteshsharma0229 - 23.06.2021 11:24

Hello Pinal sir. I have a question that if I have deployed some plan guide with MAXDOP 8 and later I changed to value to 1. Cureently I am not seeing any issue on my db but I want to know should I recreate those plan guide with configuration new configuration or not .?
Please advice

Ответить
@ErJanardhanJethi
@ErJanardhanJethi - 23.06.2021 16:00

How to get records row wise iteration??
Example I have 100 records..
I want to get top 10 rows at each iteration like 1 to 10 ,11 to 20...

Please let me if this is not correct forum to ask..

Ответить
@surajviswakarma254
@surajviswakarma254 - 23.06.2021 18:40

Hello Sir

I want to learn about Statistics, load, memory. Sql Standards. Query I can write but i am lacking with this basic.

From where I can start

Ответить
@ederaldomacedo9257
@ederaldomacedo9257 - 24.06.2021 04:48

Nice :)

Ответить
@jabkowy
@jabkowy - 24.06.2021 13:54

Hi Pinal. Thanks for this video. I have an interesting question. Does the database instance have a unique identifier that can be checked with a query?

Ответить
@moisesbas
@moisesbas - 24.06.2021 21:18

It depends on sql server version.

Ответить
@SVThatway
@SVThatway - 24.06.2021 21:19

A long time ago it did matter (like oracle 6 maybe 7), all RDBS I know of today (and year) have optimised count(*) to do the same thing. look at explain plans

Ответить
@lakshmisrinivaspallapothu3141
@lakshmisrinivaspallapothu3141 - 24.06.2021 22:58

Hi Pinal If there are n number of nested loop joins in the execution plan of the query what are the ways to optimize it and make it run quickly ?

Ответить
@diederikduck9823
@diederikduck9823 - 25.06.2021 13:34

count(id) -- counts rows where column 'id' is not null

Ответить
@MrRejiAb
@MrRejiAb - 23.07.2021 12:51

When i tried count(*) and count(1) on Production.Product table in AdventureWorks2012 database count(1) performed better that count(*)
Count(*):Physical reads 1,CPU time = 16 ms, elapsed time = 8 ms.
Count(1):Physical reads 0, CPU time = 0 ms, elapsed time = 0 ms.

Ответить
@vamshikrishna5298
@vamshikrishna5298 - 07.08.2021 21:01

Thank you for making this video.I recently subscribed to your channel. All the videos are very helpful.

Ответить
@thehouse2620
@thehouse2620 - 28.01.2022 08:58

I would have thought that more columns in the table would mean more pages read. Instead, MS docs says

COUNT(*) does not require an expression parameter because by definition, it does not use information about any particular column. COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

Ответить
@parveensahrawat9662
@parveensahrawat9662 - 16.12.2023 16:23

Hi Sir, if we are not referring/using cluster index anywhere in any query can drop cluster index on that table

Ответить