Databases 5 min read

Does COUNT(1) Really Outperform COUNT(*) in MySQL? The Truth Revealed

This article debunks the myth that COUNT(1) is faster than COUNT(*), explains how MySQL engines handle these functions, compares their performance with COUNT(column), and offers practical tips for avoiding costly full‑table counts.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Does COUNT(1) Really Outperform COUNT(*) in MySQL? The Truth Revealed

Many developers hear that COUNT(1) is faster than COUNT(*) in MySQL, but the reality is different; this article investigates the claim.

For MyISAM tables the total row count is stored, so SELECT COUNT(*) can return the value instantly. Since MySQL 5.5 the default engine is InnoDB, which uses MVCC and cannot cache the exact row count, so each count query must scan rows.

The official MySQL documentation states that SELECT COUNT(*) and SELECT COUNT(1) are processed identically, with no performance difference.

According to the SQL standard, COUNT(expr) returns the number of non‑NULL values of expr in the result set, while COUNT(*) counts all rows regardless of NULLs. Because the constant 1 is always true, COUNT(1) behaves the same as COUNT(*).

InnoDB may optimise the count by using the smallest secondary index; if none exists it falls back to the clustered index, reducing I/O overhead. COUNT(column) must read each row, check the column for NULL, and may need an extra lookup for the primary key, making it slower. The performance ordering is: COUNT(*) = COUNT(1) > COUNT(primary key) > COUNT(column).

Because full‑table counts can be expensive, it is advisable to cache predictable count results using Redis or a dedicated MySQL count table, while being mindful of consistency.

Understanding these nuances helps you avoid unnecessary full scans and design more efficient database queries.

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.

performancesqlInnoDBmysqlDatabase Optimization_count
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.