Summary of the differences between count(*), count(1) and count(col) in MySQL

Summary of the differences between count(*), count(1) and count(col) in MySQL

Preface

The count function is used to count the records in a table or array. count(*) returns the number of retrieved rows, regardless of whether it contains NULL values. I feel like everyone is discussing the difference in count recently, so I’ll write it down as well: Welcome to leave a message to discuss. Without further ado, let’s take a look at the detailed introduction.

1. Table structure:

dba_jingjing@3306>[rds_test]>CREATE TABLE `test_count` (
 -> `c1` varchar(10) DEFAULT NULL,
 -> `c2` varchar(10) DEFAULT NULL,
 -> KEY `idx_c1` (`c1`)
 -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.11 sec)

2. Insert test data:

dba_jingjing@3306>[rds_test]>insert into test_count values(1,10);
Query OK, 1 row affected (0.03 sec)

dba_jingjing@3306>[rds_test]>insert into test_count values(abc,null);
ERROR 1054 (42S22): Unknown column 'abc' in 'field list'
dba_jingjing@3306>[rds_test]>insert into test_count values('abc',null);
Query OK, 1 row affected (0.04 sec)

dba_jingjing@3306>[rds_test]>insert into test_count values(null,null);
Query OK, 1 row affected (0.04 sec)

dba_jingjing@3306>[rds_test]>insert into test_count values('368rhf8fj',null);
Query OK, 1 row affected (0.03 sec)

dba_jingjing@3306>[rds_test]>select * from test_count;
+-----------+------+
| c1 | c2 |
+-----------+------+
| 1 | 10 |
| abc | NULL |
| NULL | NULL |
| 368rhf8fj | NULL |
+-----------+------+
4 rows in set (0.00 sec)

test:

dba_jingjing@3306>[rds_test]>select count(*) from test_count;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
   EXPLAIN: {
  "query_block": {
   "select_id": 1,
   "message": "Select tables optimized away"
  1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(1) from test_count;
+----------+
| count(1) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
   EXPLAIN: {
  "query_block": {
   "select_id": 1,
   "message": "Select tables optimized away"
  1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(c1) from test_count;
+-----------+
| count(c1) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
   "table": {
    "table_name": "test1",
    "access_type": "index",
    "key": "idx_c1",
    "used_key_parts": [
     "c1"
    ],
    "key_length": "33",

So why is "key_length": "33" 33? What is a secondary index? See next section

There is no difference between count(*) and count(1), but there is a difference between count(col)

The execution plan has characteristics: it can be seen that it does not query indexes and tables, and sometimes select tables optimized away will appear, which will not query tables and will be very fast.

Extra sometimes displays "Select tables optimized away", which means there is nothing better to optimize.

For explains on simple count queries (ie explain select count(*) from people) the extra
section will read "Select tables optimized away."
This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.

---MySQL's meaning of "Select tables optimized away" is not "nothing better can be optimized". The key point in the official explanation is:
MySQL can read the result directly

So, a reasonable explanation is:

1 The data is already in memory and can be read directly;

2 Data can be considered as a result of a calculation, such as the value of a function or expression;

3 Once the query result is "predicted" by the optimizer, the result can be obtained without execution, so there is "no need to perform the select".

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • This article explains the difference between MySQL count(*), count(1), and count(col)

<<:  Linux debugging tools that developers and operators must look at [Recommended]

>>:  Let's talk about what JavaScript's URL object is

Recommend

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...

How to start a transaction in MySQL

Preface This article mainly introduces how to sta...

Two ways to build Docker images

Table of contents Update the image from an existi...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

Detailed explanation of Vue's calculated properties

1. What is a calculated attribute? In plain words...

Detailed explanation of the loading rules of the require method in node.js

Loading rules of require method Prioritize loadin...

How to implement two-way binding function in vue.js with pure JS

Table of contents First, let's talk about the...

Detailed explanation of the adaptive adaptation problem of Vue mobile terminal

1. Create a project with vue ui 2. Select basic c...

Detailed explanation of meta tags (the role of meta tags)

No matter how wonderful your personal website is,...

MySQL green decompression version installation and configuration steps

Steps: 1. Install MySQL database 1. Download the ...

Sharing tips on using Frameset to center the widescreen

Copy code The code is as follows: <frameset co...

The HTML 5 draft did not become a formal standard

<br />Yesterday I saw at W3C that the new HT...

MySQL DML statement summary

DML operations refer to operations on table recor...

Summary of MySQL usage specifications

1. InnoDB storage engine must be used It has bett...