只是想知道你们中的任何一个人是否Count(1)过度使用过Count(*),如果性能有显着差异,或者这只是从过去几天带来的遗留习惯?
(具体数据库是SQL Server 2005.)
是否有所作为,如果你这样做count(*)VS count(column-name)在这两个例子?
我倾向于总是写作,count(*)因为它似乎更符合我的想法,它是一个集合函数,如果这是有道理的.
但我不确定它是否在技术上最好,因为我倾向于看到没有*经常写的示例代码.
计数(*):
select customerid, count(*), sum(price)
from items_ordered
group by customerid
having count(*) > 1;
Run Code Online (Sandbox Code Playgroud)
与count(列名):
SELECT customerid, count(customerid), sum(price)
FROM items_ordered
GROUP BY customerid
HAVING count(customerid) > 1;
Run Code Online (Sandbox Code Playgroud)