Entwickler mit Tablet erklären Code
Adrian, Kiya | 14.02.2024

Der Unterschied zwischen Select Count(*) und Select Count(id)

Web > Der Unterschied zwischen Select Count(*) und Select Count(id)

Often it is said to use count(id) as it reduces the amount of data being processed. As an index exists on it, it is considered faster and more efficient.

This assumption is incorrect. In reality, the database uses the smallest secondary non-null index.

count(*) basically means nothing more than Count anything as fast as you can. It leaves the decision of optimization to the database.

count(id) is actually ignored. The database's optimizer ignores the id.

Why does it exist then? count() is an aggregation function. As per SQL standard, an aggregation function must have a parameter, even when it's not always necessary.

Reference links:

Content
  • Is count(id) faster than count(*)?
  • What does count(*) actually mean?
  • Why does exist count(id)?
Adrian Görisch
Adrian (Softwareentwickler)

... arbeitet als zuverlässiger Full-Stack-Softwareentwickler, aktuell hauptsächlich im Backend mit NestJS, ECMAScript und diversen Datenbanktechnologien. Besonders großen Wert legt er auf ordentliche... mehr anzeigen

Github
Kiya

... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund