Databases 4 min read

How count(1), count(*) and count(column) Differ in Execution

The article explains that count(1) and count(*) produce identical results and similar execution plans, while count(column) skips NULL values; it also compares their performance under various table schemas and provides a MySQL example demonstrating these differences.

Smart Sea Tide
Smart Sea Tide
Smart Sea Tide
How count(1), count(*) and count(column) Differ in Execution

When a table is large, analysis shows count(1) can be slightly slower than count(*), but the execution plans are the same. After statistics are gathered, count(1) may be a bit faster for data under 10k rows, though the difference is minimal.

Both count(1) and count(*) count all rows, including rows where columns are NULL. count(column) counts only non‑NULL values of that column, so NULL rows are ignored.

count(1) counts every record, including NULLs.

count(column) counts occurrences of the column, ignoring NULLs.

Performance observations:

If the column is a primary key, count(column) is fastest.

If the column is not a primary key, count(1) is faster than count(column).

When a table has multiple columns and no primary key, count(1) outperforms count(*).

If a primary key exists, counting the primary key column gives the best performance.

For a single‑column table, count(*) is optimal.

Example:

create table counttest (
  name char(1),
  age  char(2)
);
insert into counttest values
  ('a','14'),('a','15'),('a','15'),('b',NULL),
  ('b','16'),('c','17'),('d',NULL),('e','');
select name, count(name), count(1), count(*), count(age), count(distinct age)
  from counttest group by name;

The result shows that count(name), count(1) and count(*) all return the same row count, while count(age) ignores the NULL ages, and count(distinct age) reports distinct non‑NULL ages.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PerformanceSQLDatabaseMySQLCOUNT
Smart Sea Tide
Written by

Smart Sea Tide

Sharing cutting‑edge big data and AI technologies, with occasional lifestyle insights.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.