(root@node01) > select count(*) from customer where year(c_since)=2020;
+----------+
| count(*) |
+----------+
| 702 |
+----------+
1 row in set (0.46 sec)
(root@node01) > explain select count(*) from customer where year(c_since)=2020;
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | customer | NULL | ALL | NULL | NULL | NULL | NULL | 287382 | 100.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
(root@node01) > alter table customer add key idx_customer_since((year(c_since)));
Query OK, 0 rows affected (6.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@node01) > select count(*) from customer where year(c_since)=2020;
+----------+
| count(*) |
+----------+
| 702 |
+----------+
1 row in set (0.01 sec)
(root@node01) > explain select count(*) from customer where year(c_since)=2020;
+----+-------------+----------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | customer | NULL | ref | idx_customer_since | idx_customer_since | 5 | const | 702 | 100.00 | NULL |
+----+-------------+----------+------------+------+--------------------+--------------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
标签:customer,8.0,since,count,索引,+----------+,MySQL,NULL,select
From: https://blog.51cto.com/dbprofessional/7251435