Get started with 33% off your first certification using code: 33OFFNEW

Count(*) or Count(1) - Which Should You Use?

5 min read
Published on 19th August 2024

When working with databases, one common task is counting the number of rows in a table. Two popular methods to achieve this are using COUNT(*) and COUNT(1). However, there’s a lot of debate among developers and database administrators about which one is better. In this article, we'll dive deep into the differences between COUNT(*) and COUNT(1), exploring their performance implications, use cases, and the myths surrounding them. By the end, you'll have a clear understanding of when to use each method.

Understanding COUNT(*) and COUNT(1)

Before we dive into the differences, let's start by understanding what COUNT(*) and COUNT(1) do.

  • COUNT(*): This query counts all rows in a table, including those with NULL values in any column. It doesn’t matter how many columns are in the table or whether they contain data; COUNT(*) simply returns the total number of rows.

  • COUNT(1): This query counts the number of rows where the value 1 is present, which, practically, means counting all rows in the table. The 1 in this case is a constant and doesn’t refer to any specific column. Like COUNT(*), it includes all rows, regardless of whether they contain NULL values or not.

Common Misconceptions

One of the most persistent myths in the world of SQL is that COUNT(1) is faster than COUNT(*). The reasoning often given is that COUNT(1) involves counting a constant, and therefore the database engine can optimize it more efficiently than counting all columns with COUNT(*).

However, this belief is generally unfounded. Modern SQL database engines are highly optimized, and they understand that both COUNT(*) and COUNT(1) perform the same underlying operation: counting the number of rows in a table. As a result, they typically execute both queries using similar execution plans, leading to negligible differences in performance.

Performance Analysis

To better understand the performance implications, let's take a look at how modern SQL databases handle these queries.

Execution Plans

When you run a query in a relational database management system (RDBMS), the system generates an execution plan. This plan outlines the steps the database engine will take to retrieve and process the data.

For both COUNT(*) and COUNT(1), most RDBMS will generate the same execution plan. Here's why:

  • COUNT(*): The database engine interprets this as a request to count all rows in the table, and it uses a fast table scan to do so. It does not need to check the contents of each column, only that a row exists.

  • COUNT(1): The database engine sees the 1 as a constant expression that doesn’t depend on any column in the table. Like COUNT(*), it performs a fast table scan to count the rows.

Let's consider an example using a simple table:

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(255),
    department VARCHAR(255)
);

INSERT INTO employees (id, name, department) VALUES
(1, 'Alice', 'HR'),
(2, 'Bob', 'IT'),
(3, 'Charlie', 'Finance'),
(4, 'Diana', 'HR');

Running EXPLAIN on the following queries will give you insight into how the database processes them:

EXPLAIN SELECT COUNT(*) FROM employees;
EXPLAIN SELECT COUNT(1) FROM employees;

In most modern databases like MySQL, PostgreSQL, and SQL Server, the execution plan for both queries will be nearly identical. The database scans the table and counts the number of rows, without checking individual column values.

Database-Specific Optimizations

While the general principle that COUNT(*) and COUNT(1) are equivalent holds true, it's worth noting that certain database engines may implement specific optimizations.

For example:

  • MySQL: MySQL treats COUNT(*) and COUNT(1) identically, generating the same execution plan for both. The performance difference between the two is negligible.

  • PostgreSQL: PostgreSQL also treats both queries similarly, with no significant performance difference.

  • SQL Server: In SQL Server, COUNT(*) and COUNT(1) are processed in the same way, and there is no notable difference in execution time.

In summary, the choice between COUNT(*) and COUNT(1) typically has no impact on performance. The database engine's optimizations ensure that both queries run efficiently.

Use Cases and Best Practices

Now that we've established that COUNT(*) and COUNT(1) perform similarly, let's explore the scenarios in which you might prefer one over the other.

When to Use COUNT(*)

  • Counting All Rows: When you need to count all rows in a table, regardless of whether they contain NULL values or not, COUNT(*) is the most straightforward and widely understood option.

  • Simplicity and Readability: COUNT(*) is often preferred for its simplicity and clarity. It’s immediately clear that you are counting all rows, making the query easier to read and understand.

When to Use COUNT(1)

  • Consistency in Queries: If you’re writing queries where you frequently count based on specific conditions or columns (e.g., COUNT(column_name)), using COUNT(1) might offer consistency in your query style. However, this is purely a stylistic choice, not a performance optimization.

  • Legacy Code and Standards: In some older codebases or specific coding standards, COUNT(1) may be preferred. If you’re maintaining such code, it makes sense to stick with the established conventions.

The Verdict: COUNT(*) vs. COUNT(1)

Ultimately, the decision between COUNT(*) and COUNT(1) should be based on readability and coding standards rather than performance considerations. In most cases, COUNT(*) is the better choice due to its simplicity and clarity.

However, if your team or project has a convention that favors COUNT(1), there’s no harm in using it. Just be aware that any performance differences between the two are negligible in modern databases.

Myths Busted

To wrap up, let’s bust a few common myths:

  1. Myth: COUNT(1) is faster than COUNT(*).

    • Reality: Both queries are processed similarly by modern database engines, with no significant performance difference.
  2. Myth: COUNT(*) counts all columns, while COUNT(1) counts rows.

    • Reality: Both COUNT(*) and COUNT(1) count rows, not columns. COUNT(*) does not actually examine each column’s value.
  3. Myth: Using COUNT(1) improves performance by avoiding column checks.

    • Reality: Database engines optimize both queries to avoid unnecessary column checks, so the performance is the same.

Final Thoughts

When deciding between COUNT(*) and COUNT(1), focus on clarity and consistency in your SQL code. Performance-wise, the difference is negligible, thanks to the optimizations in modern SQL engines. Therefore, prioritize readability and follow the conventions of your team or project. In most cases, COUNT(*) is the preferred choice due to its simplicity.

Whether you’re working on a small project or managing large-scale databases, understanding the nuances of these SQL functions can help you write more efficient and maintainable code. So, next time you need to count rows in a table, you'll know exactly which method to use—and why.

Explore Further

For more in-depth information, check out these resources:

By following these guidelines, you'll ensure that your SQL queries are both effective and easy to understand.