


count Function
COUNT(expression): Returns the total number of queried records. The expression parameter is a field or * sign.
Test
MySQL version: 5.7.29
Create a user table and insert one million pieces of data, of which half a million rows in the gender field are null Value of
CREATE TABLE `users` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `name` varchar(32) DEFAULT NULL COMMENT '名称', `gender` varchar(20) DEFAULT NULL COMMENT '性别', `create_date` datetime DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`Id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='用户表';
count(*)
Prior to MySQL 5.7.18, InnoDB processed statements by scanning the clustered index. SELECT COUNT(*) Starting in MySQL 5.7.18, InnoDB handles SELECT COUNT(*) statements by traversing the smallest available secondary index, unless the index or optimizer hint instructs the optimizer to use a different index. If the secondary index does not exist, the clustered index is scanned.
The general meaning is that if there is a secondary index, use the secondary index. If there are multiple secondary indexes, the smallest secondary index will be preferred to reduce costs. If there is no secondary index, use the clustered index.
The following tests are used to verify these views.
First of all, query the execution plan when there is only the primary key index of Id.
You can see that the type is index, which means that the index is used. The key is PRIMARY, which means the primary key index is used, key_len=8.
Next, add an index to the name field, and use the execution plan again to view
You can see that the index is also used, but the index uses name The index of the field, key_len=99.
Then add an index to the create_date field while retaining the name field index, and check the execution plan again
#You can see that this time the The create_date field is indexed, key_len=6.
No matter which index is used above, the total number of rows finally queried is one million, regardless of whether they contain NULL values.
count(1)
count(1) has the same query results as count(*), and ultimately returns one million pieces of data, regardless of whether they contain NULL values.
count(col)
count(col) counts the value of a certain column, which is divided into three situations:
count(id): counts id
The query results are the same as count(*), and ultimately one million pieces of data are returned.
count(index col): Statistics of indexed fields
Use count(name) For query, the execution plan is as follows:
You can see that the index field is used for statistics, and the index is also hit.
Set the name field in a column to NULL, and then perform a count query. The result returns 999999
Then set the NULL value of this column to an empty string, and then Perform a count query, and the result returns 1000000
. Therefore, in summary, simply using the index field to count the number of rows can hit the index, and only count the number of rows that are not NULL values.
count(normal col): Count fields without indexes
If you count fields without indexes, the index will not be used, and only the number of rows that are not NULL values will be counted.
Count(1) and count(*) trade-off
I don’t know where I saw or heard about it before, count(1) is better than count(*) count(*) is highly efficient, which is a wrong perception. There is a saying on the official website, InnoDB handles SELECT COUNT( *) and SELECT COUNT(1) operations in the same way. There is no performance difference.
Translation The bottom line is that InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way, with no performance difference.
For MyISAM tables, COUNT(*) is optimized to return very quickly if retrieving from one table, no other columns are retrieved and there is no clause. This optimization only applies to MyISAM tables because for this The storage engine stores the exact number of rows and can be accessed very quickly. COUNT(1) does the same optimization only if the first column is defined as NOT NULL. ----From MySQL official website
These optimizations are based on the premise that there is no where and group by.
It is also mentioned in the Alibaba development specifications
So if you can use count(*) during development, just use count(*).
The above is the detailed content of What is the difference between count(*), count(1), and count(col) in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use
