mysql中对一个表的id建立了唯一索引,那么查询的select count(*) ,select count(1) ,select count(id),select count(列名)
1.mysql中对一个表的id建立了唯一索引,那么查询的select count(*) ,select count(1) ,select count(id),select count(列名) 的查询结果又什么不同,及索引的使用情况
先说结论:
①对id 建立索引,select count(*) ,select count(1) 和select count(id) 这三者等效,会统计为null的数据
都会走index 级别的索引,并且不会回表,
因为只做统计数据,没有用到其他字段(Extra的 Using index 就说明了 这种情况)
② select count(某个列名),只统计该列名不为null的情况,如果没有对该列名,建立索引,那么就会全表
用途:在数据量特别大的时候测试,两个表之间有没有关系,就用count(主键) 基本上不用的等待就可以 查到想要的结果(尤其是在表数据量特别大的时候特别有用)
比如:
# 最新认知(重要) :inner join 取交集不是 数目取交集,eg: boys 只有一个 id 为 1 的数据,但是 girl有 8 条 boy_id 为 1 的记录,
# 最后的的结果 数量就是 8,大于 boys 表的一条数据(唯一确定的是小于等于 笛卡尔集 (男生表count*女生表count))
# 8 有女朋友的男生信息
select count(boys.id) from boys inner join girl on boys.id = girl.boy_id ;
以下是测试sql和脚本:
create table sql_test.boys
(id int not null,name varchar(20) null,sex varchar(20) default '男' null comment '默认男',age varchar(20) null,address int null,constraint boys_id_uindexunique (id)
)comment '男生表';select * from boys ;
# +--+----+---+---+-------+
# |id|name|sex|age|address|
# +--+----+---+---+-------+
# |1 |慕容皝 |男 |22 |NULL |
# |2 |慕容冲 |男 |33 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |4 |慕容博 |男 |55 |NULL |
# |5 |慕容雪 |女 |55 |NULL |
# |6 |慕容复 |男 |55 |NULL |
# |7 |慕容龙城|女 |55 |NULL |
# +--+----+---+---+-------+# 因为我对id 建立了唯一索引
# 7 type : index ;key :boys_id_uindex ;Extra :Using index(ps:不需要表,只做统计)
select count(*) from boys;
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type |possible_keys|key |key_len|ref |rows|filtered|Extra |
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |1 |SIMPLE |boys |NULL |index|NULL |boys_id_uindex|4 |NULL|8 |100 |Using index|
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# 7
select count(1) from boys;
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type |possible_keys|key |key_len|ref |rows|filtered|Extra |
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |1 |SIMPLE |boys |NULL |index|NULL |boys_id_uindex|4 |NULL|8 |100 |Using index|
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# 说明: select count(*) from boys 和 select count(1) from boys; 两条sql 都走了索引,
# 并且没有回表,说明 和下面的sql等效(说明:boys 上 对 id 建立了 唯一索引)
# 7
select count(id) from boys;
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type |possible_keys|key |key_len|ref |rows|filtered|Extra |
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+
# |1 |SIMPLE |boys |NULL |index|NULL |boys_id_uindex|4 |NULL|8 |100 |Using index|
# +--+-----------+-----+----------+-----+-------------+--------------+-------+----+----+--------+-----------+# 0 type:all ; 因为 address 没加索引,走的是 全表扫描
select count(address) from boys;
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----+
# |id|select_type|table|partitions|type|possible_keys|key |key_len|ref |rows|filtered|Extra|
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----+
# |1 |SIMPLE |boys |NULL |ALL |NULL |NULL|NULL |NULL|8 |100 |NULL |
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----+# 增加一条数据select * from boys ;
# +--+----+---+----+-------+
# |id|name|sex|age |address|
# +--+----+---+----+-------+
# |1 |慕容皝 |男 |22 |NULL |
# |2 |慕容冲 |男 |33 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |4 |慕容博 |男 |55 |NULL |
# |5 |慕容雪 |女 |55 |NULL |
# |6 |慕容复 |男 |55 |NULL |
# |7 |慕容龙城|女 |55 |NULL |
# |8 |慕容吹雪|男 |NULL|NULL |
# +--+----+---+----+-------+# 8
select count(*) from boys;
# 8
select count(1) from boys;
# 0
select count(address) from boys;
# 7
select count(age) from boys;# 题外话: 如果要导出 性别为男生的数据 到另一个表 sql select * from boys where sex = 男 同时dataGrip 查询结果右上角 extractors(翻译:提取者;抽出器;提取器)
# 改为sql insert 模式 :例子如下:
select * from boys where sex = '男' ;
# 提取器的为 insert sql 后 查询的结果如下:
# INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (1, '慕容皝', '男', '22', null);
# INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (2, '慕容冲', '男', '33', null);
# INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (3, '慕容垂', '男', '44', null);
# INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (4, '慕容博', '男', '55', null);
# INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (6, '慕容复', '男', '55', null);# 建表语句
create table sql_test.boys
(id int not null,name varchar(20) null,sex varchar(20) default '男' null comment '默认男',age varchar(20) null,address int null,constraint boys_id_uindexunique (id)
)comment '男生表';# 用datagrip 查询结果 的 提取器 导出 sql语句
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (1, '慕容皝', '男', '22', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (2, '慕容冲', '男', '33', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (3, '慕容垂', '男', '44', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (4, '慕容博', '男', '55', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (5, '慕容雪', '女', '55', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (6, '慕容复', '男', '55', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (7, '慕容龙城', '女', '55', null);
INSERT INTO sql_test.boys (id, name, sex, age, address) VALUES (8, '慕容吹雪', '男', null, null);
查看表关系的sql(尤其是在数据量非常大的情况下使用,非常有效)
select * from girl;
# +--+----+------+---+
# |id|name|boy_id|sex|
# +--+----+------+---+
# |1 |黄蓉 |1 |女 |
# |2 |李莫愁 |2 |女 |
# |3 |陆无双 |2 |女 |
# |4 |程英 |3 |女 |
# |5 |周紫墨 |3 |女 |
# |6 |宋小婉 |3 |女 |
# |7 |侯小妹 |3 |女 |
# |8 |小黄蓉 |3 |女 |
# +--+----+------+---+# 8 有女朋友的男生信息
select count(boys.id) from boys inner join girl on boys.id = girl.boy_id ;
# 最新认知(重要) :inner join 取交集不是 数目取交集,eg: boys 只有一个 id 为 1 的数据,但是 girl有 8 条 boy_id 为 1 的记录,
# 最后的的结果 数量就是 8,大于 boys 表的一条数据(唯一确定的是小于等于 笛卡尔集 (男生表count*女生表count))
select boys.* from boys inner join girl on boys.id = girl.boy_id ;
# +--+----+---+---+-------+
# |id|name|sex|age|address|
# +--+----+---+---+-------+
# |1 |慕容皝 |男 |22 |NULL |
# |2 |慕容冲 |男 |33 |NULL |
# |2 |慕容冲 |男 |33 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# |3 |慕容垂 |男 |44 |NULL |
# +--+----+---+---+-------+
# 8
select count(*) from boys inner join girl on boys.id = girl.boy_id ;# +--+-----------+-----+----------+------+--------------+--------------+-------+--------------------+----+--------+-----------+
# |id|select_type|table|partitions|type |possible_keys |key |key_len|ref |rows|filtered|Extra |
# +--+-----------+-----+----------+------+--------------+--------------+-------+--------------------+----+--------+-----------+
# |1 |SIMPLE |girl |NULL |ALL |NULL |NULL |NULL |NULL |7 |100 |Using where|
# |1 |SIMPLE |boys |NULL |eq_ref|boys_id_uindex|boys_id_uindex|4 |sql_test.girl.boy_id|1 |100 |Using index|
# +--+-----------+-----+----------+------+--------------+--------------+-------+--------------------+----+--------+-----------+create table girl
(id int null,name varchar(20) null,boy_id int null,sex varchar(40) null
)comment '女性表';# 给 boy_id 建立索引后create index girl_boy_id_indexon sql_test.girl (boy_id);# 给 boy_id 建立唯一 索引后
# 8
select count(boys.id) from boys inner join girl on boys.id = girl.boy_id ;
# +--+-----------+-----+----------+------+-----------------+-----------------+-------+--------------------+----+--------+------------------------+
# |id|select_type|table|partitions|type |possible_keys |key |key_len|ref |rows|filtered|Extra |
# +--+-----------+-----+----------+------+-----------------+-----------------+-------+--------------------+----+--------+------------------------+
# |1 |SIMPLE |girl |NULL |index |girl_boy_id_index|girl_boy_id_index|5 |NULL |7 |100 |Using where; Using index|
# |1 |SIMPLE |boys |NULL |eq_ref|boys_id_uindex |boys_id_uindex |4 |sql_test.girl.boy_id|1 |100 |Using index |
# +--+-----------+-----+----------+------+-----------------+-----------------+-------+--------------------+----+--------+------------------------+
mysql中对一个表的id建立了唯一索引,那么查询的select count(*) ,select count(1) ,select count(id),select count(列名)相关推荐
- MySql中把一个表的数据插入到另一个表中的实现代码--转
MySql中把一个表的数据插入到另一个表中的实现代码 司的一个项目,做报表--要关联的表结构比较多,最后决定把要用的数据集合到一张新表中,需要用到以下的sql语法......分享下: web开发中,我 ...
- MySQL中三种表关系的建立
一:约束条件之主键 1.什么是主键? 从约束条件的角度,主键是非空且唯一的字段 not null unique create table t1(id int not null unique,name ...
- mysql唯一索引一个表_mysql一个表可以有几个唯一索引?
匿名用户 1级 2017-12-01 回答 创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复.唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值.如果能确定某个数据列将只包 ...
- MySQL 中的共享表空间与独立表空间如何选择
对于 InnoDB 存储引擎来说,它可以将每张表存放于独立的表空间,即 tablename.ibd 文件;也可以将数据存放于 ibdata 的共享表空间,一般命名是 ibdataX,后面的 X 是一个 ...
- mysql中视图和表的区别及联系_MySQL中Update、select联用操作单表、多表,及视图与临时表的区别...
一.MySQL中使用从表A中取出数据来更新表B的内容 例如:要update表data中的一些列属性,但是修改属性的内容来源是来自表chanpin.SQL语言中不要显示的出现select关键字 upda ...
- MySQL学习笔记(六)-MySQL中库和表的管理
MySQL学习笔记(六)-MySQL中库和表的管理 作者:就叫易易好了 日期:2020/11/23 1 2 DDL即数据定义语言 创建:create 修改:alter 删除:drop 库和表的管理: ...
- MySQL 中的共享表空间与独立表空间,用哪个好呢?
作者 | 江南一点雨 责编 | 晋兆雨 1.独立表空间与共享表空间 2.该用哪一个? 2.1 独立表空间 2.2 共享表空间 3.迁移 好啦,那我们今天就开始学习 MySQL 中另外一个非 ...
- mysql 操作表的例子,mysql中库和表的简单操作总结(附示例)
本篇文章给大家带来的内容是关于mysql中库和表的简单操作总结(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一. 库的操作 1.创建数据库 创建数据库: create d ...
- mysql中的派生表
派生表是从SELECT语句返回的虚拟表.派生表类似于临时表,但是在SELECT语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤.当SELECT语句的FROM子句中使用独立子查询时,我们将 ...
最新文章
- 2016.7.14最新cocoapods最新安装教程
- 全球及中国冠状动脉内支架行业运行现状与十四五发展状况分析报告2022版
- 2019年末逆向复习系列之努比亚Cookie生成逆向分析
- android Animation 动画。淡出动画
- jQuery Portamento 滑动定位
- vb 获取系统声音的电平_专业音响系统中常见问题,看看你懂几个?
- 烘烤店LOGO在线设计制作教程
- 问题:anaconda: command not found 解决方案
- vue中怎么清空tab选项卡的缓存_vuejs 内置组件component实现tab切换懒加载和表单输入框内容的清空...
- 喜马拉雅音频批量下载源码
- 功能对等四个原则_功能对等四原则
- 如何在Word中绘制流程图
- [ProblemSolving]教育网下载速度慢,如何解决?
- oppoa9处理器怎么样_oppo a9是什么处理器
- 勤做眼保健操,减轻眼睛疲劳
- Nexmo 短信平台接口 遇到的坑
- win10:打印机无故脱机、打印机缺纸故障处理
- 小巧 mvc servlet 通过反射 跳转 bussiness层
- 11. 求解一元二次方程
- 数据结构与算法-普利姆算法(Prim) | 尚硅谷韩顺平