对于索引优化真的是门课题,先来研究下最平常的问题,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. python3 tkinter 桌面软件教程

    效果图 """"brid布局""" from tkinter import * import tkinter.filedialog ...

  2. ballerina 学习十八 事务编程

    事务在分布式开发,以及微服务开发中是比较重要的 ballerina 支持 本地事务.xa 事务.分布式事务 ,但是具体的服务实现起来需要按照ballerian 的事务模型 infection agre ...

  3. 【转】Windows消息投递流程:一般窗口消息投递(WM_LBUTTONCLICK)

    原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182646 本例通过在单文档程序的视图中添加WM_LBUTTONCLICK消息处理函数 ...

  4. Panabit Live CD使用说明

    为了方便更多的用户实际使用Panabit,省去安装FreeBSD和Panabit等步骤,自2007年9月,即Panabit V7.09起,Panabit网站发布Panabit标准版安装包时,同步发布相 ...

  5. FT5X06 如何应用在10寸电容屏

    硬件搭起来看现象,如下图: 红色区域是FT5406上报有效数据的范围(1280*600),以左上角为原点 ,X轴方向上报数据的最大值1280,Y轴方向上报的最大数据是600..但是我用的LG的10.1 ...

  6. unity里面的gameobject和transform的关系

    一切都是物体(gameobject),而transform是物体的一个基本属性类,包含位置,旋转,缩放,三个基本属性,两者之间可以互相转换 查找物体,建议用transform,GameObject无法 ...

  7. 关于 ImageLoader 说的够细了。。。

    简介ImageLoader(一) 分类: android 开源及第三方项目2014-05-30 12:14 14126人阅读 评论(0) 收藏 举报 ImageLoader 使用该开源项目的之前,先给 ...

  8. Android开源项目汇总

    Android很多优秀的开源项目,很多UI组件以及经典的HTTP 访问开源,都能给我们带来一些自己项目的启迪或者引用. 下面简单介绍一下自己收藏的一些项目内容. 项目: Apollo音乐播放器:And ...

  9. configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

    编译php出现错误: configure: error: Please reinstall the libcurl distribution - easy.h should be in <cur ...

  10. Linux下添加php的zip模块

    今天早上开发的人员过来跟我说,测试机上的XX项目报了个错: include(ZipArchive.php): failed to open stream: No such file or direct ...