mysql5.6.30之count(*)详解
来源:互联网 发布:什么是数据猿 编辑:程序博客网 时间:2024/06/05 19:19
一:count
统计目标列不为空的数据行。
例:
mysql> select * from t6; +------+----------+ | id | name | +------+----------+ | 1 | ziOLI | | 2 | xiaoguo | | 3 | xiaowang | | 4 | | | 5 | NULL | +------+----------+
如果是count(*)那么就会统计不为空最多的那个列。统计的数据包含为“”不包含为null的数据。
例:
mysql> select count(name) from t6; +-------------+ | count(name) | +-------------+ | 4 | +-------------+
拓展:count(*)与索引
- 版本:mysql:5.6.30
- 引擎:innodb
一: 何时走索引:
创建表test插入测试数据:
mysql> create table test(id int(10),name varchar(10));Query OK, 0 rows affected (0.02 sec)mysql> insert into test values(1,"dfd"),(2,"fdfd");Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0
无索引查询执行计划:
mysql> explain select count(*) from test\G;*************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 Extra: NULL1 row in set (0.00 sec)
添加主键测试count(id)
mysql> alter table test add primary key(id);Query OK, 0 rows affected (0.05 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> explain select count(id) from test\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: indexpossible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 2 Extra: Using index1 row in set (0.00 sec)
结论:走了主键索引
测试count(*)
mysql> explain select count(*) from test\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: indexpossible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 2 Extra: Using index1 row in set (0.00 sec)
结论:即使我没有指定id字段还是走了主键索引
二:5.6环境下count(*)走主键索引和辅助索引的效率的对比
表结构
mysql> desc sbtest;+-------+---------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+---------------------+------+-----+---------+----------------+| aid | bigint(20) unsigned | NO | PRI | NULL | auto_increment || id | int(10) unsigned | NO | MUL | 0 | || k | int(10) unsigned | NO | MUL | 0 | || c | char(120) | NO | | | || pad | char(60) | NO | | | |+-------+---------------------+------+-----+---------+----------------+
插入1000000行数据
测试:
一:mysql> explain select count(*) from sbtest;+----+-------------+--------+-------+---------------+------+---------+------+--------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+------+---------+------+--------+-------------+| 1 | SIMPLE | sbtest | index | NULL | k | 4 | NULL | 964496 | Using index |+----+-------------+--------+-------+---------------+------+---------+------+--------+-------------+1 row in set (0.00 sec)mysql> select count(*) from sbtest;+----------+| count(*) |+----------+| 1010101 |+----------+1 row in set (0.56 sec)二:mysql> explain select count(*) from sbtest where id>=0;+----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+| 1 | SIMPLE | sbtest | range | id | id | 4 | NULL | 482248 | Using where; Using index |+----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+1 row in set (0.04 sec)mysql> select count(*) from sbtest where id>=0;+----------+| count(*) |+----------+| 1010101 |+----------+1 row in set (0.55 sec)三:mysql> explain select count(*) from sbtest where aid>=0;+----+-------------+--------+-------+---------------+---------+---------+------+--------+--------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+---------+---------+------+--------+--------------------------+| 1 | SIMPLE | sbtest | range | PRIMARY | PRIMARY | 8 | NULL | 482248 | Using where; Using index |+----+-------------+--------+-------+---------------+---------+---------+------+--------+--------------------------+1 row in set (0.00 sec)mysql> select count(*) from sbtest where aid>=0;+----------+| count(*) |+----------+| 1010101 |+----------+1 row in set (2.52 sec)
部分数据:
aid id k c pad 1 1 1 dfgfgf dfdfd 2 2 1 dfgfgf dfdfd 3 3 1 dfgfgf dfdfd 4 4 1 dfgfgf dfdfd以上测试每次都会重启mysql,因而不存在缓存
- 结论:在指定条件的情况下主见索引依然没有附属索引快,在不指定的条件的情况下mysql很聪明,选择了走了我数据量较少的索引k。
0 0
- mysql5.6.30之count(*)详解
- mysql5.6之my.cnf配置详解
- mysql5.6配置文件详解(二)
- mysql5.6配置文件详解(一)
- mysql5.2.26安装详解(windows)
- MySQL5.6新特性之crash-safe详解
- Spark API 详解/大白话解释 之 RDD、partition、count、collect
- MySQL5.6 备份之mysqldump(一)
- python之单词统计(words count)
- LeetCode之Count Binary Substrings(Kotlin)
- MYSQL5.0安装详解
- MySQL5.6配置文件详解
- Mysql5.6配置文件详解
- MySQL5.7安装详解
- MySQL5.6配置文件详解
- MySQL5.7 安装详解
- count与count_if详解
- COUNT()函数详解
- 第八周 项目2-用对象数组操作长方柱类
- Linux下openoffice安装以及启动服务
- 如何写一个覆盖状态栏的全屏的QML应用
- 【剑指offer】二进制中的1的个数
- SQL语言的分类及表格数据的基本操作
- mysql5.6.30之count(*)详解
- List 篇
- Sicily1561——Prime
- Map 篇
- 软件架构设计-五视图方法论
- 使用ListView显示信息列表
- Java中数组的特性
- AsyncHttpClient请求网络数据小节!
- Xcode SCM系统 使用Git存储库机制之 学习笔记