对于索引优化真的是门课题,先来研究下最平常的问题,innodb引擎下 怎么让count(*)快一点。

  首先需要清楚

  innodb 默认是对主键建立聚簇索引,如果没有主键,那就是对具有唯一且非空值的索引来代替,如果也没有,innodb内部就会自己维护一个这样的索引。

聚簇索引存的是索引+数据,二级索引存的是对主键即聚簇索引的指向。

  所以通过上面的猜想

  1.表中聚簇索引如果有varchar,text的字段,如果存储数据比较多,聚簇索引就会非常的大,就会照成大量数据页分裂。(这里数据页是什么,我也理解的不是很深刻,请自行百度)

  2.根据二级索引去count(*) 会快很多。

  想到就干,开始实践

#只有基本的字段
create table a1(
id int not null auto_increment,
user_name varchar(20) not null,
user_pwd char(32) not null,
regtime datetime not null default CURRENT_TIMESTAMP,
primary key (id)
)charset=utf8; #有个varchar(255)这样存大数据的字段
create table a2(
id int not null auto_increment,
user_name varchar(20) not null,
user_pwd char(32) not null,
description varchar(255) not null comment '描述',
img_url varchar(255) not null comment '头像地址',
regtime datetime not null default CURRENT_TIMESTAMP,
primary key (id)
)charset=utf8;  
#建立存储过程写入50w条数据
DELIMITER //
create procedure insert_a(in t int)
begin
set @num=1;
if t=2 then
set @description = '工作式的自我介绍的内容,应当包括本人姓名、供职的单位及其部门、担负的职务或从事的具体工作等三项。他们叫作工作式自我介绍内容的三要素,通常缺一不可。其中,第一项姓名,应当一口报出,不可有姓无名,或有名无姓。第二项供职的单位及其部门,有可能最好全部报出,具体工作部门有时也可以暂不报出。第三项担负的职务或从事的具体工作,有职务最好报出职务,职务较低或者无职务,则可报出所从事的具体工作。';
set @img_url = 'https://gss1.bdstatic.com/5eN1dDebRNRTm2_p8IuM_a/res/img/0617jd.jpg';
end if; while @num<=500000 do
if t=1 then
insert into a1 (user_name,user_pwd) values(concat('user',@num),'e10adc3949ba59abbe56e057f20f883ezc');
else
insert into a2 (user_name,user_pwd,description,img_url) values(concat('user',@num),'e10adc3949ba59abbe56e057f20f883ezc',@description,@img_url);
end if;
set @num=@num+1;
end while;
end // DELIMITER ; CALL insert_a(1);
CALL insert_a(2);

  准备工作都已经做好了

  1.首先通过以下2条语句来测试下

 1.count(*) , a2中description存入了比较大的数据
select sql_no_cache count(*) from a1; #平均0.12 sec
select sql_no_cache count(*) from a2; #平均0.65 sec

  2.接下来给user_name加上普通索引

 alter table a1 add index user_name (`user_name`);
alter table a2 add index user_name (`user_name`);

  执行以下语句来验证

 select sql_no_cache count(*) from a1 where user_name>'user0';
select sql_no_cache count(*) from a2 where user_name>'user0';

  

  通过测试,对a1的count(*)变慢了,但是a2的count(*)快了几倍。

  以上都是自己猜想实验,也许中间是其他原因导致猜想测试不准确,欢迎高手指点。

  参考:http://www.t086.com/article/5083

innodb count优化测试的更多相关文章

  1. MySQL · 引擎特性 · InnoDB COUNT(*) 优化(?)

    http://mysql.taobao.org/monthly/2016/06/10/ 在5.7版本中,InnoDB实现了新的handler的records接口函数,当你需要表上的精确记录个数时,会直 ...

  2. mysql 5.7 innodb count count(*) count(1) 大数据 查询慢 耗时多 优化

    原文:mysql 5.7 innodb count count(*) count(1) 大数据 查询慢 耗时多 优化 问题描述 mysql 5.7 innodb 引擎 使用以下几种方法进行统计效率差不 ...

  3. 14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB

    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB 14.6.11.1 Configuring Persisten ...

  4. mysql innodb 性能优化

    建议参数: max_connections=800 key_buffer_size=512M query_cache_size=128M sort_buffer_size=64M table_open ...

  5. Mysql Innodb 引擎优化-内存、日志、IO、其他相关参数

    介绍: InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色增加 ...

  6. (转) mysql数据库引擎:MyISAM和InnoDB(性能优化)

    转自 http://yuwensan126.iteye.com/blog/1138022 Mysql 数据库中,最常用的两种引擎是innordb和myisam.Innordb的功能要比myiasm强大 ...

  7. Innodb IO优化-配置优化

    作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究. 对于数据库来讲 ...

  8. mysql innodb count(*)速度慢且不准确的解决办法

    innodb引擎在统计方面和myisam是不同的,Myisam内置了一个计数器,所以在使用 select count(*) from table 的时候,直接可以从计数器中取出数据.而innodb必须 ...

  9. Innodb性能优化之参数设置

    现在,Innodb是Mysql最多使用的存储引擎.其性能一直广受关注.本文通过基本的参数设置来提高其性能. innodb_buffer_pool_size 缓冲池大小.这是innodb参数中最重要的设 ...

随机推荐

  1. nginx 只容许域名访问禁止掉 ip 访问

    在原有 nginx server 的基础上再加上相同端口的配置 server {            listen 80 default_server;            server_name ...

  2. jenkins执行shell命令,有时会提示“Command not found”

    这个问题其实就是环境变量没有配准确 (1)检查你在Jenkins中设置的maven是否准确,可以通过[new job]按钮查看新建job中是否有maven选项,没有就是你配置的不准确 如果你下载的插件 ...

  3. StreamSets 部署 Pipelines 到 SDC Edge

    可以使用如下方法: 下载edge 运行包并包含pipeline定义文件. 直接发布到edge 设备. 在data colelctor 机器配置并配置了edge server 地址(主要需要网络可访问) ...

  4. Python编程核心内容 ---- Function(函数)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 截至上篇随笔<Python数据结构之四——set(集合)>,Python基础知识也介绍好了.接下来准备干 ...

  5. swing版网络爬虫-丑牛迷你采集器2.0

    swing版网络爬虫-丑牛迷你采集器2.0 http://www.javacoo.com/code/704.jhtml 整合JEECMS http://bbs.jeecms.com/fabu/3186 ...

  6. sqlnet.ora限制客户端IP访问

    实现功能: 只允许某几个IP访问数据库服务端(白名单): $ORACLE_HOME/network/admin/sqlnet.ora 添加2个主要参数 TCP.VALIDNODE_CHECKING=y ...

  7. application/json 和 application/x-www-form-urlencoded的区别

    public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...

  8. Selenium2+python自动化63-简易项目搭建

    前言 到unittest这里基本上可以搭建一个简易的项目框架了,我们可以用一条run_main.py脚本去控制执行所有的用例,并生成报告,发送邮件一系列的动作 一.新建工程 1.打开pycharm左上 ...

  9. 九、创建Slave节点

    通常情况下,我们的项目会由多个模块或者系统组成,不同模块可能会分别部署在不同的服务器,比如mod1部署在ser1,mod2部署在ser2上: 之前的文档是描述了将jenkins也部署在ser1上,当m ...

  10. 新的方法(Set<T>)实现mvc的crud

    model层的属性为: public partial class UserInfo { public int Uid { get; set; } public string UName { get; ...