SELECT COUNT(1) FROM dbo.tab800krowsSELECT COUNT(1),FKID FROM dbo.tab800krows GROUP BY FKID
SELECT COUNT(*) FROM dbo.tab800krowsSELECT COUNT(*),FKID FROM dbo.tab800krows GROUP BY FKID
CREATE TABLE [dbo].[employee]([Id] [bigint] IDENTITY(1,1) NOT NULL,[Name] [nvarchar](50) NOT NULL,CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED([Id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
Employee表中的记录总数:178090131(~1.78亿行)
第一个查询:
Set Statistics Time OnGoSelect Count(*) From EmployeeGoSet Statistics Time OffGo
第一次查询结果:
SQL Server parse and compile time:CPU time = 0 ms, elapsed time = 35 ms.
(1 row(s) affected)
SQL Server Execution Times:CPU time = 10766 ms, elapsed time = 70265 ms.SQL Server parse and compile time:CPU time = 0 ms, elapsed time = 0 ms.
第二个问题:
Set Statistics Time OnGoSelect Count(1) From EmployeeGoSet Statistics Time OffGo
第二次查询结果:
SQL Server parse and compile time:CPU time = 14 ms, elapsed time = 14 ms.
(1 row(s) affected)
SQL Server Execution Times:CPU time = 11031 ms, elapsed time = 70182 ms.SQL Server parse and compile time:CPU time = 0 ms, elapsed time = 0 ms.
首先,select count(1) from table与select count(*) from table之间没有语义差异。它们在所有情况下返回相同的结果(如果没有,这是一个bug)。如其他答案所述,select count(column) from table在语义上不同,并不总是返回与count(*)相同的结果。