What to count when counting all rows MySQL

István Pálinkás

Is there any difference in the performance, or the background execution behavior when counting * , any custom field ( n ), or the primary id in MySQL?

What exactly does * refer to in the query, and how does it differ from those two other ways?

SELECT COUNT( * ) FROM t;
SELECT COUNT( id ) FROM t;
SELECT COUNT( n ) FROM t;

UPDATE:

Assume, that neither id nor n is null at any record.

Bashar Abutarieh
COUNT(*) will include NULLS
COUNT(column_or_expression) won't.

This means COUNT(any_non_null_column) will give the same as COUNT(*) of course because there are no NULL values to cause differences.

Generally, COUNT(*) should be better because any index can be used because COUNT(column_or_expression) may not be indexed or SARGable

From ANSI-92 (look for "Scalar expressions 125")

Case:

a) If COUNT(*) is specified, then the result is the cardinality of T.

b) Otherwise, let TX be the single-column table that is the result of applying the to each row of T and eliminating null values. If one or more null values are eliminated, then a completion condition is raised: warning- null value eliminated in set function.

The same rules apply to SQL Server and Sybase too at least

Note: COUNT(1) is the same as COUNT(*) because 1 is a non-nullable expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL counting all rows instead of counting individual rows

From Dev

Get count of all affected rows when executing multiple statements in MySql

From Dev

MySQL Order by count of rows

From Dev

MySQL: select last (max) value from column and count of all rows

From Dev

Retrieving all data while COUNTING the rows

From Dev

MySql counting all those tupples where in Date Rows containing (NULL) and or '0000-00-00' values

From Dev

Counting and knowing what rows are displayed using jQuery

From Dev

mysql: count duplicate rows from all the groups

From Dev

MySQL Count All Rows and Count All Rows Where field1 = 1

From Dev

MySQL COUNT(*) not counting result rows

From Dev

Mysql Count and Sum Rows

From Dev

PHP PDO counting all table rows in Database

From Dev

Count function not counting all rows in db table

From Dev

SQL: counting the number of rows and returning all rows with max count: invalid use of group function

From Dev

Mysql update all rows value with count of same table column

From Dev

mysql count the sum of all rows

From Dev

counting duplicate rows in mysql

From Dev

SQL counting all rows instead of counting individual rows

From Dev

Count all rows in a column

From Dev

Is it possible to avoid the "AS" parameter when counting total rows of a mysql table? (php)

From Dev

How to count all rows from mysql but display only 2 results and count all results

From Dev

MySQL - Count all rows for each day of week but for each sender

From Dev

What to count when counting all rows MySQL

From Dev

MySQL filter containing string count when more than two rows

From Dev

MySql counting all those tupples where in Date Rows containing (NULL) and or '0000-00-00' values

From Dev

Counting and knowing what rows are displayed using jQuery

From Dev

MySQL Count All Rows and Count All Rows Where field1 = 1

From Dev

PHP PDO counting all table rows in Database

From Dev

Counting specific rows mysql + express

Related Related

  1. 1

    SQL counting all rows instead of counting individual rows

  2. 2

    Get count of all affected rows when executing multiple statements in MySql

  3. 3

    MySQL Order by count of rows

  4. 4

    MySQL: select last (max) value from column and count of all rows

  5. 5

    Retrieving all data while COUNTING the rows

  6. 6

    MySql counting all those tupples where in Date Rows containing (NULL) and or '0000-00-00' values

  7. 7

    Counting and knowing what rows are displayed using jQuery

  8. 8

    mysql: count duplicate rows from all the groups

  9. 9

    MySQL Count All Rows and Count All Rows Where field1 = 1

  10. 10

    MySQL COUNT(*) not counting result rows

  11. 11

    Mysql Count and Sum Rows

  12. 12

    PHP PDO counting all table rows in Database

  13. 13

    Count function not counting all rows in db table

  14. 14

    SQL: counting the number of rows and returning all rows with max count: invalid use of group function

  15. 15

    Mysql update all rows value with count of same table column

  16. 16

    mysql count the sum of all rows

  17. 17

    counting duplicate rows in mysql

  18. 18

    SQL counting all rows instead of counting individual rows

  19. 19

    Count all rows in a column

  20. 20

    Is it possible to avoid the "AS" parameter when counting total rows of a mysql table? (php)

  21. 21

    How to count all rows from mysql but display only 2 results and count all results

  22. 22

    MySQL - Count all rows for each day of week but for each sender

  23. 23

    What to count when counting all rows MySQL

  24. 24

    MySQL filter containing string count when more than two rows

  25. 25

    MySql counting all those tupples where in Date Rows containing (NULL) and or '0000-00-00' values

  26. 26

    Counting and knowing what rows are displayed using jQuery

  27. 27

    MySQL Count All Rows and Count All Rows Where field1 = 1

  28. 28

    PHP PDO counting all table rows in Database

  29. 29

    Counting specific rows mysql + express

HotTag

Archive