Count(*) vs Count(1) - SQL Server

后端 未结 13 2058
醉梦人生
醉梦人生 2020-11-21 05:21

Just wondering if any of you people use Count(1) over Count(*) and if there is a noticeable difference in performance or if this is just a legacy h

13条回答
  •  独厮守ぢ
    2020-11-21 05:59

    COUNT(1) is not substantially different from COUNT(*), if at all. As to the question of COUNTing NULLable COLUMNs, this can be straightforward to demo the differences between COUNT(*) and COUNT()--

    USE tempdb;
    GO
    
    IF OBJECT_ID( N'dbo.Blitzen', N'U') IS NOT NULL DROP TABLE dbo.Blitzen;
    GO
    
    CREATE TABLE dbo.Blitzen (ID INT NULL, Somelala CHAR(1) NULL);
    
    INSERT dbo.Blitzen SELECT 1, 'A';
    INSERT dbo.Blitzen SELECT NULL, NULL;
    INSERT dbo.Blitzen SELECT NULL, 'A';
    INSERT dbo.Blitzen SELECT 1, NULL;
    
    SELECT COUNT(*), COUNT(1), COUNT(ID), COUNT(Somelala) FROM dbo.Blitzen;
    GO
    
    DROP TABLE dbo.Blitzen;
    GO
    

提交回复
热议问题