在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

MySQL中count(*)、count(1)、count(col)三者的区别是什么

139次阅读
没有评论

共计 2589 个字符,预计需要花费 7 分钟才能阅读完成。

自动写代码机器人,免费开通

MySQL 中 count(*)、count(1)、count(col) 三者的区别是什么?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1、表结构:

dba_jingjing@3306 [rds_test] CREATE TABLE `test_count` ( -  `c1` varchar(10) DEFAULT NULL,
 -  `c2` varchar(10) DEFAULT NULL,
 -  KEY `idx_c1` (`c1`)
 -  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.11 sec)

2、插入测试数据:

dba_jingjing@3306 [rds_test] insert into test_count values(1,10);
Query OK, 1 row affected (0.03 sec)
dba_jingjing@3306 [rds_test] insert into test_count values(abc,null);
ERROR 1054 (42S22): Unknown column  abc  in  field list 
dba_jingjing@3306 [rds_test] insert into test_count values(abc ,null);
Query OK, 1 row affected (0.04 sec)
dba_jingjing@3306 [rds_test] insert into test_count values(null,null);
Query OK, 1 row affected (0.04 sec)
dba_jingjing@3306 [rds_test] insert into test_count values(368rhf8fj ,null);
Query OK, 1 row affected (0.03 sec)
dba_jingjing@3306 [rds_test] select * from test_count;
+-----------+------+
| c1 | c2 |
+-----------+------+
| 1 | 10 |
| abc | NULL |
| NULL | NULL |
| 368rhf8fj | NULL |
+-----------+------+
4 rows in set (0.00 sec)

测试:

dba_jingjing@3306 [rds_test] select count(*) from test_count;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
 EXPLAIN: {
  query_block : {
  select_id : 1,
  message :  Select tables optimized away 
 1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306 [rds_test] select count(1) from test_count;
+----------+
| count(1) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
 EXPLAIN: {
  query_block : {
  select_id : 1,
  message :  Select tables optimized away 
 1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306 [rds_test] select count(c1) from test_count;
+-----------+
| count(c1) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
  table : {
  table_name :  test1 ,
  access_type :  index ,
  key :  idx_c1 ,
  used_key_parts : [
  c1 
 ],
  key_length :  33 ,

那么这里面的 key_length : 33 , 为什么是 33 呢,什么是二级索引?见下节

count(*) 和 count(1) 是没有区别的,而 count(col) 是有区别的

执行计划有特点:可以看出它没有查询索引和表,有时候会出现 select tables optimized away 不会查表,速度会很快

Extra 有时候会显示“Select tables optimized away”,意思是没有更好的可优化的了。

官方解释 For explains on simple count queries (i.e. explain select count(*) from people) the extra
  section will read Select tables optimized away.
  This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.

—MySQL 对于“Select tables optimized away”的含义, 不是 没有更好的可优化的了 , 官方解释中关键的地方在于:
 MySQL can read the result directly

所以, 合理的解释是: 

  1 数据已经在内存中可以直接读取; 

  2 数据可以被认为是一个经计算后的结果, 如函数或表达式的值; 

  3 一旦查询的结果被优化器 预判 可以不经执行就可以得到结果, 所以才有 not need to perform the select .

关于 MySQL 中 count(*)、count(1)、count(col) 三者的区别是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计2589字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 囯产精品一区二区三区线 | 亚洲午夜久久久精品电影院 | 国产成人免费高清在线观看 | 亚洲精品装饰一二三有限公司 | 欧美一级毛片大片免费播放 | 色老头福影院韩国激情影院 | a级片免费在线观看 | 丝袜国产在线观看 | 亚洲一区二区综合 | 中文字幕一区二区三区精彩视频 | 欧美亚洲免费久久久 | 999国产精品视频 | 日本在线观看一区二区三区 | 香港经典a毛片免费观看播放 | 福利一区在线视频 | 亚洲最大天堂无码精品区 | 780pp亚洲情艺中心 | 日韩精品专区av无码 | 在线观看黄色小视频 | 污污内射久久一区二区欧美日韩 | 亚洲精品无码专区在线 | 中文字幕熟妇人妻在线视频 | 日韩欧群交p片内射中文 | 亚洲人在线视频 | 成 人 黄 色 免费 网站无毒 | 91视频综合网 | 男女猛烈激情xx00免费视频 | 国产欧美日韩久久久久 | 欧美精品一区二区在线观看 | 国产亚洲精品aaaa片小说 | 一级在线免费观看 | 日韩欧美国产精品 | 亚洲国产午夜精品理论片在线播放 | 久久在线看 | 99久久国产| 久久精品99精品免费观看 | 亚洲色偷拍另类无码专区 | 久久久久亚洲av成人无码网站 | 日本一级级特黄特色大片 | 免费国内精品久久久久影院 | 日韩精品无码一区二区三区免费 |