【MySQL】count(*) count(字段) count(1) count(distinct)的区别

准备

  1. 创建库表
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
CREATE TABLE `test``count_demo` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
  `status` enum('0','1') COLLATE utf8mb4_general_ci NOT NULL DEFAULT '0',
  `description` varchar(5000) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
  1. 创建存储过程
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `add_count_demos`$$
 
CREATE DEFINER=CURRENT_USER PROCEDURE `add_count_demos`(IN item INTEGER)
BEGIN
BEGIN
DECLARE counter INT;
SET counter = num;
WHILE counter >= 1 DO
INSERT INTO count_demo (id,name,status,description) VALUES(null,MD5(RAND()), 1, REPEAT('abcAB', RAND()*1000));
SET counter = counter - 1;
END WHILE;
END$$
 
DELIMITER ;
  1. 生成数据
call add_count_demos(100000)

简介

count() 是MySQL内置函数,用来统计字段中非NULL的数量

  • count(*) 统计表的行数
  • count(1)count(0)的效果都是一样的,返回表的行数
  • count(字段) 统计字段中非NULL的数量
  • count(distinct 字段) 统计字段中不重复,且不为NULL的数量

innoDB中的性能区别

效率从高到低:
count(*) ≈ count(1) > count(主键id) > count(字段)
建议使用count(*)

  • count(*) 做了内部优化,会自动使用索引
  • count(1) innoDB遍历全表,但不取出字段值
  • count(主键) innoDB遍历全表,并取出主键值
  • count(字段) innoDB遍历全表,判断是否为NULL
mysql> explain select count(*) from count_demo;;
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type | table      | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | count_demo | NULL       | index | NULL          | PRIMARY | 8       | NULL | 233420 |   100.00 | Using index |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+

explain select count(1) from count_demo;;
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type | table      | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | count_demo | NULL       | index | NULL          | PRIMARY | 8       | NULL | 233420 |   100.00 | Using index |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+

explain select count(id) from count_demo;
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type | table      | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | count_demo | NULL       | index | NULL          | PRIMARY | 8       | NULL | 233420 |   100.00 | Using index |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+

explain select count(name) from count_demo;
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------+
| id | select_type | table      | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------+
|  1 | SIMPLE      | count_demo | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 233420 |   100.00 | NULL  |
+----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+-------+

你可能感兴趣的:(【MySQL】count(*) count(字段) count(1) count(distinct)的区别)