当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql count提高方法总结

mysql count提高方法总结

2019年05月28日  | 移动技术网IT编程  | 我要评论

mysql想必很多程序猿都是不陌生的。很多人在纠结count的用法,怎么样才能查询效果最好。今天来谈谈小编的一些看法,仅供参考。

1、咱们先准备建一个表,并准备好测试数据方便后续步骤测试

以innodb引擎表为例

建表语句如下

create table test.test

(

    a varchar(50) not null comment 'ddfdf',

    b varchar(15) not null comment 'fds',

    c varchar(20) not null comment 'asda',

    d varchar(8) not null comment 'ads',

    e longblob not null comment 'asda',

    f varchar(2000) comment 'ads',

    g varchar(8) not null comment 'assd',

    h date not null comment 'adsad',

    z varchar(10) not null comment 'adsd'

)

engine=innodb default charset=utf8;

2、登陆到mysql并且换数据库

执行建表语句如下图所示

3、然后是准备测试数据,简单查一下是否有数据,如下图所示

4、接下来开始做测试

在没有where条件的情况下

有人觉得count(*)比count(字段)要快,还有人觉得count(字段)比count(*)要快?

那到底那个快,咱们一试便知,请看下图

根据图中结果显示,明显count(字段)快一些

5、那在有where条件的情况呢?count(*)和count(字段)谁更快?

请看下图执行效果

count(*)快一点,可能是数据量太少看不出太明显的效果

6、当然可以通过查看执行计划进行分析

在执行的sql前面加上desc或者explain即可,如下图所示

最后总结一下1.在没有where条件下,建议count(字段)2.在有where条件的情况下,建议count(*)最后总结一下1.在没有where条件下,建议count(字段)2.在有where条件的情况下,建议count(*)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网