浅谈MySQL中count(*) 与 count(1)的问题

浅谈MySQL select count(*) 与 count(1)
最近看到同事在讨论MySQL关于count(1)、count(*)的执行效率等的问题,感兴趣去搜索并且自己做了一些实验,这里只探讨innnodb存储引擎

一、 首先看下MySQL5.6官方文档对于count函数的说明
COUNT(expr)

Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement. The result is a BIGINT value.

COUNT(1) is only subject to the same optimization if the first column is defined as NOT NULL.

MySQL官方文档上对于count(1)和count()的比较没有过多的说明,只是说在第一个列为 NOT NULL时,count(1)与count()走同样的优化。

很多人说count(1)比count()快,因为count()操作会多一步转换操作,会将翻译成一个固定值,转换成count(1)类型 通过查阅相关资料发现现在随着MySQL版本的升级count(1)与count()的已经几乎相同。

二、 count(*)或count(1)的执行计划比较
1、 表结构
创建一个一千多万行记录的表
| huayu_test1 | CREATE TABLE huayu_test1 (
waybill_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '?????',
waybill_no varchar(20) NOT NULL COMMENT '???',
post_date datetime NOT NULL COMMENT '????????',
order_id varchar(50) DEFAULT NULL COMMENT '?????????',
batch_no varchar(50) DEFAULT NULL COMMENT '???',
biz_product_id varchar(20) NOT NULL COMMENT '??????',
biz_product_no varchar(20) NOT NULL COMMENT '??????',
biz_product_name varchar(50) NOT NULL COMMENT '??????',
sender_type varchar(20) DEFAULT NULL COMMENT '0 ?? 1????',
sender_id varchar(50) DEFAULT NULL COMMENT '??????',
sender_no varchar(50) DEFAULT NULL COMMENT '??????(??????)',
sender varchar(200) DEFAULT NULL COMMENT '??????',
sender_linker varchar(200) DEFAULT NULL,
sender_fixtel varchar(50) DEFAULT NULL COMMENT '??????',
sender_mobile varchar(50) DEFAULT NULL COMMENT '??????',
sender_addr varchar(200) DEFAULT NULL COMMENT '??????',
sender_country_no varchar(20) DEFAULT NULL COMMENT '??????',
sender_country_name varchar(50) DEFAULT NULL COMMENT '??????',
sender_province_no varchar(20) DEFAULT NULL COMMENT '??????',
sender_province_name varchar(50) DEFAULT NULL COMMENT '??????',
sender_city_no varchar(20) DEFAULT NULL COMMENT '??????',
sender_city_name varchar(50) DEFAULT NULL COMMENT '??????',
sender_county_no varchar(20) DEFAULT NULL COMMENT '??????',
sender_county_name varchar(50) DEFAULT NULL COMMENT '??????',
sender_notes varchar(1000) DEFAULT NULL COMMENT '????',
receiver_no varchar(50) DEFAULT NULL COMMENT '??????????????',
receiver varchar(200) DEFAULT NULL COMMENT '??????',
receiver_linker varchar(200) DEFAULT NULL,
receiver_fixtel varchar(50) DEFAULT NULL COMMENT '??????',
receiver_mobile varchar(50) DEFAULT NULL COMMENT '??????',
receiver_addr varchar(200) DEFAULT NULL COMMENT '??????',
receiver_country_no varchar(20) DEFAULT NULL COMMENT '??????',
receiver_country_name varchar(50) DEFAULT NULL COMMENT '??????',
receiver_province_no varchar(20) DEFAULT NULL COMMENT '??????',
receiver_province_name varchar(50) DEFAULT NULL COMMENT '??????',
receiver_city_no varchar(20) DEFAULT NULL COMMENT '??????',
receiver_city_name varchar(50) DEFAULT NULL COMMENT '??????',
receiver_county_no varchar(20) DEFAULT NULL COMMENT '??????',
receiver_county_name varchar(50) DEFAULT NULL COMMENT '??????',
insurance_flag char(1) DEFAULT NULL COMMENT '????????????1:?? 2:?? 3:??',
insurance_amount decimal(12,2) DEFAULT NULL COMMENT '??????',
pickup_type varchar(20) DEFAULT NULL COMMENT '?????0 ???????1 ??????',
payment_mode varchar(20) DEFAULT NULL COMMENT '????(???) 1:??? 2:??? 3:??? 4:??????? 5:?? 6:?/??? 7:???',
real_weight decimal(8,0) DEFAULT NULL COMMENT '????',
fee_weight decimal(8,0) DEFAULT NULL,
volume_weight decimal(8,0) DEFAULT NULL,
length decimal(8,0) DEFAULT NULL,
width decimal(8,0) DEFAULT NULL,
height decimal(8,0) DEFAULT NULL,
quantity int(11) DEFAULT NULL,
packaging varchar(20) DEFAULT NULL,
package_material varchar(20) DEFAULT NULL,
goods_desc varchar(200) DEFAULT NULL,
contents_quantity int(11) DEFAULT NULL,
cod_flag char(1) DEFAULT NULL,
cod_amount decimal(12,2) DEFAULT NULL,
receipt_flag char(1) DEFAULT NULL,
receipt_waybill_no varchar(20) DEFAULT NULL,
receipt_fee_amount decimal(12,2) DEFAULT NULL,
insurance_premium_amount decimal(12,2) DEFAULT NULL,
valuable_flag char(1) DEFAULT NULL,
cargo_total_price decimal(12,2) DEFAULT NULL,
cargo_total_purchasing_price decimal(12,2) DEFAULT NULL,
allow_fee_flag char(1) DEFAULT NULL,
is_feed_flag char(1) DEFAULT NULL,
manual_fee_type char(1) DEFAULT NULL,
fee_date datetime DEFAULT NULL,
discount_rate decimal(6,2) DEFAULT NULL,
settlement_mode varchar(20) DEFAULT NULL,
payment_state char(1) DEFAULT NULL,
payment_date datetime DEFAULT NULL,
payment_id varchar(50) DEFAULT NULL,
manage_org_code varchar(20) DEFAULT NULL,
postage_suite_code varchar(20) DEFAULT NULL,
fee_area_suite_code varchar(20) DEFAULT NULL,
fee_area_code varchar(20) DEFAULT NULL,
fee_area_name varchar(20) DEFAULT NULL,
is_advance_flag char(1) DEFAULT NULL,
deliver_type char(1) DEFAULT NULL,
deliver_sign varchar(50) DEFAULT NULL,
deliver_date char(1) DEFAULT NULL,
deliver_notes varchar(1000) DEFAULT NULL,
deliver_pre_date date DEFAULT NULL,
battery_flag char(1) DEFAULT NULL,
is_jinguan varchar(20) DEFAULT NULL,
workbench varchar(20) DEFAULT NULL,
electronic_preferential_no varchar(50) DEFAULT NULL,
electronic_preferential_amount decimal(12,2) DEFAULT NULL,
pickup_attribute char(1) DEFAULT NULL,
adjust_type varchar(20) DEFAULT NULL,
postage_revoke decimal(12,2) DEFAULT NULL,
print_flag char(1) DEFAULT NULL,
print_date datetime DEFAULT NULL,
print_times int(11) DEFAULT NULL,
declare_source varchar(20) DEFAULT NULL,
declare_type varchar(20) DEFAULT NULL,
declare_curr_code varchar(20) DEFAULT NULL,
create_user_name varchar(50) DEFAULT NULL,
modify_user_name varchar(50) DEFAULT NULL,
volume decimal(8,0) DEFAULT NULL COMMENT '??',
contents_type_no varchar(20) DEFAULT NULL COMMENT '??????',
contents_type_name varchar(200) DEFAULT NULL COMMENT '??????',
contents_weight decimal(8,0) DEFAULT NULL COMMENT '??????',
transfer_type varchar(20) DEFAULT NULL COMMENT '????',
postage_total decimal(12,2) NOT NULL COMMENT '???=????+????',
postage_standard decimal(12,2) DEFAULT NULL COMMENT '????',
postage_paid decimal(12,2) DEFAULT NULL COMMENT '????',
postage_other decimal(12,2) DEFAULT NULL COMMENT '????',
is_deleted char(1) DEFAULT '0' COMMENT '?????n0??n1??',
create_user_id bigint(20) DEFAULT NULL COMMENT '???id',
gmt_created datetime DEFAULT NULL COMMENT '????',
modify_user_id bigint(20) DEFAULT NULL COMMENT '???id',
gmt_modified datetime DEFAULT NULL COMMENT '????',
reserved1 bigint(20) DEFAULT NULL COMMENT '????1',
reserved2 bigint(20) DEFAULT NULL COMMENT '????2',
reserved3 bigint(20) DEFAULT NULL COMMENT '????3',
reserved4 varchar(200) DEFAULT NULL COMMENT '????4',
reserved5 varchar(200) DEFAULT NULL COMMENT '????5',
reserved6 varchar(200) DEFAULT NULL COMMENT '????6',
reserved7 varchar(200) DEFAULT NULL COMMENT '????7',
reserved8 varchar(200) DEFAULT NULL COMMENT '????8',
reserved9 datetime DEFAULT NULL COMMENT '????9',
reserved10 text COMMENT '????10',
logistics_order_no bigint(50) DEFAULT NULL,
inner_channel varchar(20) DEFAULT NULL,
base_product_id bigint(20) DEFAULT NULL,
base_product_no varchar(20) DEFAULT NULL,
base_product_name varchar(20) DEFAULT NULL,
is_special_marketing char(1) DEFAULT NULL,
product_type varchar(20) DEFAULT NULL,
biz_product_type varchar(20) DEFAULT NULL,
product_reach_area char(1) DEFAULT NULL,
contents_attribute char(1) DEFAULT NULL,
contents_cargo_no varchar(1000) DEFAULT NULL,
cmd_code varchar(20) DEFAULT NULL,
manual_charge_reason varchar(200) DEFAULT NULL,
time_limit char(1) DEFAULT NULL,
io_type varchar(20) DEFAULT NULL,
ecommerce_no varchar(20) DEFAULT NULL,
waybill_type varchar(20) DEFAULT NULL,
pre_waybill_no varchar(50) DEFAULT NULL,
post_batch_id varchar(50) DEFAULT NULL,
biz_occur_date datetime DEFAULT NULL,
post_org_id bigint(20) DEFAULT NULL,
post_org_no varchar(50) DEFAULT NULL,
org_drds_code varchar(50) DEFAULT NULL,
post_org_simple_name varchar(50) DEFAULT NULL,
post_org_product_name varchar(20) DEFAULT NULL,
post_person_id bigint(20) DEFAULT NULL,
post_person_no varchar(50) DEFAULT NULL,
post_person_name varchar(50) DEFAULT NULL,
post_person_mobile varchar(50) DEFAULT NULL,
sender_warehouse_id bigint(20) DEFAULT NULL,
sender_warehouse_name varchar(200) DEFAULT NULL,
sender_safety_code varchar(50) DEFAULT NULL,
sender_im_type varchar(20) DEFAULT NULL,
sender_im_id varchar(50) DEFAULT NULL,
sender_id_type varchar(20) DEFAULT NULL,
sender_id_no varchar(50) DEFAULT NULL,
sender_id_encrypted_code varchar(50) DEFAULT NULL,
sender_agent_id_type varchar(20) DEFAULT NULL,
sender_agent_id_no varchar(50) DEFAULT NULL,
sender_id_encrypted_code_agent varchar(50) DEFAULT NULL,
sender_addr_additional varchar(200) DEFAULT NULL,
sender_district_no varchar(20) DEFAULT NULL,
sender_postcode varchar(20) DEFAULT NULL,
sender_gis varchar(20) DEFAULT NULL,
registered_customer_no varchar(50) DEFAULT NULL,
receiver_type char(1) DEFAULT NULL,
receiver_id bigint(20) DEFAULT NULL,
receiver_warehouse_id bigint(20) DEFAULT NULL,
receiver_warehouse_name varchar(200) DEFAULT NULL,
receiver_safety_code varchar(50) DEFAULT NULL,
receiver_im_type varchar(20) DEFAULT NULL,
receiver_im_id varchar(50) DEFAULT NULL,
receiver_addr_additional varchar(200) DEFAULT NULL,
receiver_district_no varchar(20) DEFAULT NULL,
receiver_postcode varchar(20) DEFAULT NULL,
receiver_gis varchar(20) DEFAULT NULL,
receiver_notes varchar(1000) DEFAULT NULL,
customer_manager_id bigint(20) DEFAULT NULL,
customer_manager_no varchar(50) DEFAULT NULL,
customer_manager_name varchar(50) DEFAULT NULL,
salesman_id bigint(20) DEFAULT NULL,
salesman_no varchar(50) DEFAULT NULL,
salesman_name varchar(50) DEFAULT NULL,
order_weight decimal(8,0) DEFAULT NULL,
post_org_name varchar(200) DEFAULT NULL,
PRIMARY KEY (waybill_id),
KEY auto_shard_key_post_date (post_date),
KEY auto_shard_key_waybill_no (waybill_no)
) ENGINE=InnoDB AUTO_INCREMENT=568964219 DEFAULT CHARSET=utf8 COMMENT='???????' |

注意:该表上有三个索引,(一个主键索引,两个二级索引)
PRIMARY KEY (waybill_id),
KEY auto_shard_key_post_date (post_date),
KEY auto_shard_key_waybill_no (waybill_no)

2、查看表的数据量以及执行计划

1、查看执行计划

mysql> select count(1) from huayu_test1;
count(1)
15861881

1 row in set (2.16 sec)

mysql> select count(*) from huayu_test1;
count(*)
15861881

1 row in set (2.16 sec)

mysql> explain select count(1) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: auto_shard_key_post_date
  key_len: 5
      ref: NULL
     rows: 15412951
    Extra: Using index

1 row in set (0.00 sec)

mysql> explain select count(*) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: auto_shard_key_post_date
  key_len: 5
      ref: NULL
     rows: 15412951
    Extra: Using index

1 row in set (0.00 sec)

通过执行计划发现count(1)和count(*)没有区别,后续就不再说明。该查询是走了一个二级索引

2、将该二级索引删除再次执行查询语句

mysql> explain select count(*) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: auto_shard_key_waybill_no
  key_len: 62
      ref: NULL
     rows: 14061153
    Extra: Using index

1 row in set (0.00 sec)

mysql> explain select count(1) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: auto_shard_key_waybill_no
  key_len: 62
      ref: NULL
     rows: 14061153
    Extra: Using index

1 row in set (0.00 sec)

mysql> select count(*) from huayu_test1;
count(*)
15861881

1 row in set (12.70 sec)

mysql> select count(1) from huayu_test1;
count(1)
15861881

1 row in set (2.27 sec)

mysql> select count(*) from huayu_test1;
count(*)
15861881

1 row in set (2.26 sec)

发现执行计划走的是另外一个二级索引,并且执行时间相比第一次略有增加。

(这里还有一个问题,就是删除之前走的那条索引重新执行select count(*)后,第一次执行的时间比较长,后续就正常了)

3、将上一个执行计划中用到的二级索引页删除,执行语句

mysql> explain select count(*) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: PRIMARY
  key_len: 8
      ref: NULL
     rows: 14061153
    Extra: Using index

1 row in set (0.00 sec)

mysql> explain select count(1) from huayu_test1G
1. row **

       id: 1

select_type: SIMPLE

    table: huayu_test1
     type: index

possible_keys: NULL

      key: PRIMARY
  key_len: 8
      ref: NULL
     rows: 14061153
    Extra: Using index

1 row in set (0.00 sec)

mysql> select count(*) from huayu_test1;
count(*)
15861881

1 row in set (39.66 sec)

mysql> select count(1) from huayu_test1;
count(1)
15861881

1 row in set (54.20 sec)

mysql> select count(*) from huayu_test1;
count(*)
15861881

1 row in set (1 min 6.71 sec)

mysql> select count(1) from huayu_test1;
count(1)
15861881

1 row in set (1 min 6.90 sec)

这次是采用到了主键索引,花费的时间也是超过了一分多钟

3、原理分析

MySQL查询优化器目标就是为了减少SQL的执行时间,那为什么会用二级索引而不去用主键索引呢,因为在统计行数的操作中涉及到磁盘IO问题,降低磁盘IO问题就大大的减少执行时间。IO带宽是一定的,索引占用的空间越小产生的IO次数就越少。而innodb的主键索引包括key,事务id和rollpointer,而二级索引包括key和主键id,所以使用二级索引的开销会比较少。所以innodb的select count( )操作一般都是通过二级索引来进行统计操作

InnoDB的主键索引采用聚簇索引存储,使用的是B+Tree作为索引结构,但是叶子节点存储的是key和数据本身。
InnoDB的二级索引不使用聚蔟索引,叶子节点存储的是KEY和主键值。因此,通过二级索引查询首先查到是主键值,然后InnoDB再根据查到的主键值通过主键索引找到相应的数据块。

三、那count(1)中”1”是什么意思呢?
1、下面是几个测试:

mysql> select count(waybill_id) from huayu_test1;
count(waybill_id)
15861881

1 row in set (2.53 sec)

mysql> select count(2) from huayu_test1;
count(2)
15861881

1 row in set (2.16 sec)

mysql> select count(0) from huayu_test1;
count(0)
15861881

1 row in set (2.16 sec)

mysql> select count(111) from huayu_test1;
count(111)
15861881

1 row in set (2.16 sec)

mysql> select count(222) from huayu_test1;
count(222)
15861881

1 row in set (2.14 sec)

mysql> select count(999) from huayu_test1;
count(999)
15861881

1 row in set (2.14 sec)

mysql> desc huayu_test1g
Field Type Null Key Default Extra
waybill_id bigint(20) NO PRI NULL auto_increment
waybill_no varchar(20) NO MUL NULL
post_date datetime NO MUL NULL
order_id varchar(50) YES NULL
batch_no varchar(50) YES NULL
biz_product_id varchar(20) NO NULL
biz_product_no varchar(20) NO NULL
biz_product_name varchar(50) NO NULL
sender_type varchar(20) YES NULL
sender_id varchar(50) YES NULL
sender_no varchar(50) YES NULL
sender varchar(200) YES NULL
sender_linker varchar(200) YES NULL
sender_fixtel varchar(50) YES NULL
sender_mobile varchar(50) YES NULL
sender_addr varchar(200) YES NULL
sender_country_no varchar(20) YES NULL
sender_country_name varchar(50) YES NULL
sender_province_no varchar(20) YES NULL
sender_province_name varchar(50) YES NULL
sender_city_no varchar(20) YES NULL
sender_city_name varchar(50) YES NULL
sender_county_no varchar(20) YES NULL
sender_county_name varchar(50) YES NULL
sender_notes varchar(1000) YES NULL
receiver_no varchar(50) YES NULL
receiver varchar(200) YES NULL
receiver_linker varchar(200) YES NULL
receiver_fixtel varchar(50) YES NULL
receiver_mobile varchar(50) YES NULL
receiver_addr varchar(200) YES NULL
receiver_country_no varchar(20) YES NULL
receiver_country_name varchar(50) YES NULL
receiver_province_no varchar(20) YES NULL
receiver_province_name varchar(50) YES NULL
receiver_city_no varchar(20) YES NULL
receiver_city_name varchar(50) YES NULL
receiver_county_no varchar(20) YES NULL
receiver_county_name varchar(50) YES NULL
insurance_flag char(1) YES NULL
insurance_amount decimal(12,2) YES NULL
pickup_type varchar(20) YES NULL
payment_mode varchar(20) YES NULL
real_weight decimal(8,0) YES NULL
fee_weight decimal(8,0) YES NULL
volume_weight decimal(8,0) YES NULL
length decimal(8,0) YES NULL
width decimal(8,0) YES NULL
height decimal(8,0) YES NULL
quantity int(11) YES NULL
packaging varchar(20) YES NULL
package_material varchar(20) YES NULL
goods_desc varchar(200) YES NULL
contents_quantity int(11) YES NULL
cod_flag char(1) YES NULL
cod_amount decimal(12,2) YES NULL
receipt_flag char(1) YES NULL
receipt_waybill_no varchar(20) YES NULL
receipt_fee_amount decimal(12,2) YES NULL
insurance_premium_amount decimal(12,2) YES NULL
valuable_flag char(1) YES NULL
cargo_total_price decimal(12,2) YES NULL
cargo_total_purchasing_price decimal(12,2) YES NULL
allow_fee_flag char(1) YES NULL
is_feed_flag char(1) YES NULL
manual_fee_type char(1) YES NULL
fee_date datetime YES NULL
discount_rate decimal(6,2) YES NULL
settlement_mode varchar(20) YES NULL
payment_state char(1) YES NULL
payment_date datetime YES NULL
payment_id varchar(50) YES NULL
manage_org_code varchar(20) YES NULL
postage_suite_code varchar(20) YES NULL
fee_area_suite_code varchar(20) YES NULL
fee_area_code varchar(20) YES NULL
fee_area_name varchar(20) YES NULL
is_advance_flag char(1) YES NULL
deliver_type char(1) YES NULL
deliver_sign varchar(50) YES NULL
deliver_date char(1) YES NULL
deliver_notes varchar(1000) YES NULL
deliver_pre_date date YES NULL
battery_flag char(1) YES NULL
is_jinguan varchar(20) YES NULL
workbench varchar(20) YES NULL
electronic_preferential_no varchar(50) YES NULL
electronic_preferential_amount decimal(12,2) YES NULL
pickup_attribute char(1) YES NULL
adjust_type varchar(20) YES NULL
postage_revoke decimal(12,2) YES NULL
print_flag char(1) YES NULL
print_date datetime YES NULL
print_times int(11) YES NULL
declare_source varchar(20) YES NULL
declare_type varchar(20) YES NULL
declare_curr_code varchar(20) YES NULL
create_user_name varchar(50) YES NULL
modify_user_name varchar(50) YES NULL
volume decimal(8,0) YES NULL
contents_type_no varchar(20) YES NULL
contents_type_name varchar(200) YES NULL
contents_weight decimal(8,0) YES NULL
transfer_type varchar(20) YES NULL
postage_total decimal(12,2) NO NULL
postage_standard decimal(12,2) YES NULL
postage_paid decimal(12,2) YES NULL
postage_other decimal(12,2) YES NULL
is_deleted char(1) YES 0
create_user_id bigint(20) YES NULL
gmt_created datetime YES NULL
modify_user_id bigint(20) YES NULL
gmt_modified datetime YES NULL
reserved1 bigint(20) YES NULL
reserved2 bigint(20) YES NULL
reserved3 bigint(20) YES NULL
reserved4 varchar(200) YES NULL
reserved5 varchar(200) YES NULL
reserved6 varchar(200) YES NULL
reserved7 varchar(200) YES NULL
reserved8 varchar(200) YES NULL
reserved9 datetime YES NULL
reserved10 text YES NULL
logistics_order_no bigint(50) YES NULL
inner_channel varchar(20) YES NULL
base_product_id bigint(20) YES NULL
base_product_no varchar(20) YES NULL
base_product_name varchar(20) YES NULL
is_special_marketing char(1) YES NULL
product_type varchar(20) YES NULL
biz_product_type varchar(20) YES NULL
product_reach_area char(1) YES NULL
contents_attribute char(1) YES NULL
contents_cargo_no varchar(1000) YES NULL
cmd_code varchar(20) YES NULL
manual_charge_reason varchar(200) YES NULL
time_limit char(1) YES NULL
io_type varchar(20) YES NULL
ecommerce_no varchar(20) YES NULL
waybill_type varchar(20) YES NULL
pre_waybill_no varchar(50) YES NULL
post_batch_id varchar(50) YES NULL
biz_occur_date datetime YES NULL
post_org_id bigint(20) YES NULL
post_org_no varchar(50) YES NULL
org_drds_code varchar(50) YES NULL
post_org_simple_name varchar(50) YES NULL
post_org_product_name varchar(20) YES NULL
post_person_id bigint(20) YES NULL
post_person_no varchar(50) YES NULL
post_person_name varchar(50) YES NULL
post_person_mobile varchar(50) YES NULL
sender_warehouse_id bigint(20) YES NULL
sender_warehouse_name varchar(200) YES NULL
sender_safety_code varchar(50) YES NULL
sender_im_type varchar(20) YES NULL
sender_im_id varchar(50) YES NULL
sender_id_type varchar(20) YES NULL
sender_id_no varchar(50) YES NULL
sender_id_encrypted_code varchar(50) YES NULL
sender_agent_id_type varchar(20) YES NULL
sender_agent_id_no varchar(50) YES NULL
sender_id_encrypted_code_agent varchar(50) YES NULL
sender_addr_additional varchar(200) YES NULL
sender_district_no varchar(20) YES NULL
sender_postcode varchar(20) YES NULL
sender_gis varchar(20) YES NULL
registered_customer_no varchar(50) YES NULL
receiver_type char(1) YES NULL
receiver_id bigint(20) YES NULL
receiver_warehouse_id bigint(20) YES NULL
receiver_warehouse_name varchar(200) YES NULL
receiver_safety_code varchar(50) YES NULL
receiver_im_type varchar(20) YES NULL
receiver_im_id varchar(50) YES NULL
receiver_addr_additional varchar(200) YES NULL
receiver_district_no varchar(20) YES NULL
receiver_postcode varchar(20) YES NULL
receiver_gis varchar(20) YES NULL
receiver_notes varchar(1000) YES NULL
customer_manager_id bigint(20) YES NULL
customer_manager_no varchar(50) YES NULL
customer_manager_name varchar(50) YES NULL
salesman_id bigint(20) YES NULL
salesman_no varchar(50) YES NULL
salesman_name varchar(50) YES NULL
order_weight decimal(8,0) YES NULL
post_org_name varchar(200) YES NULL

188 rows in set (0.00 sec)

通过测试可以发现,count( )中的id,并没有特殊的含义,也不是按照第几列统计行数,因为通过表结构可以发现总共有188个列,但是count(999)跟count(1)的结果是一样的。

2、重新建一个有空值的表来测试:

mysql> show create table test2;
test2 CREATE TABLE test2 (

id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |

mysql> select * from test2;
id name
1 asd
2 hahah
3 www
4 http
5 java
6 NULL

6 rows in set (0.00 sec)

mysql> select count(*) from test2;
count(*)
6

1 row in set (0.00 sec)

mysql> select count(1) from test2;
count(1)
6

1 row in set (0.00 sec)

mysql> select count(2) from test2;
count(2)
6

1 row in set (0.00 sec)

mysql> select count(name) from test2;
count(name)
5

1 row in set (0.00 sec)

mysql> select count(id) from test2;
count(id)
6

1 row in set (0.00 sec)

mysql> select count(999) from test2;
count(999)
6

1 row in set (0.00 sec)
3、通过测试发现,count( 1)中,1只是一个固定值,没有什么具体的意义,更不是指第一个列,也可以看成一个虚值,count(*)和count(1)操作会统计表中列的行数,包括NULL列,count(col)操作会统计指定列的行数,不包括NULL值。

上一篇:mysql 基本操作指南之mysql函数


下一篇:RDS实例的性能测试报告----基础总结篇