mysql 性能优化索引、缓存、分表、分布式实现方式。
系统针对5000台终端测试结果
索引
目标:优化查询速度3秒以内 需要优化。尽量避免使用select * 来查询对象。使用到哪些属性值就查询出哪些使用即可
首页页面:
设备-组织查询 优化 避免使用select * 来查询device对象 优化前耗时 200毫秒 优化后耗时19毫秒
设备-在线 优化 避免使用select * 来查询device对象 优化前耗时 210毫秒 优化后耗时11毫秒
设备-版本 优化 避免使用select * 来查询device对象 优化前耗时 230毫秒 优化后耗时10毫秒
终端检查报告页面:
查看报告详细报告 优化前耗时3603毫秒 优化后耗1103毫秒
优化方式:
1:ccp_infraction 建立联合索引 checkItemId和reportId
create index ccp_infraction_index on ccp_infraction (reportId,checkItemId) ;
查看报告所有结果和人工判定 优化前耗时10300毫秒 优化后耗时1973毫秒
优化方式:添加联系索引
create index idx_1 on ccp_artificial (reportId,isInfraction);
create index idx_2 on ccp_terminalinfo(reportId);
create index idx_3 on ccp_currenttimerep(reportId);
create index idx_4 on ccp_hostrep (reportId);
create index idx_5 on ccp_currentaccountrep(reportId);
create index idx_6 on ccp_accountrep(reportId,isInfraction);
create index idx_7 on ccp_hardwarerep(reportId,isInfraction);
create index idx_8 on ccp_networkrep (reportId);
create index idx_9 on ccp_softwarerep(reportId,isInfraction);
create index idx_10 on ccp_diskrep (reportId,isInfraction);
create index idx_11 on ccp_partitioninforep(reportId);
create index idx_12 on ccp_oneKeyInforep(reportId,isInfraction);
create index idx_13 on ccp_usbinforep(reportId,isInfraction);
create index idx_14 on ccp_printerrep (reportId,isInfraction);
create index idx_15 on ccp_cdromrep(reportId,isInfraction);
create index idx_16 on ccp_wpdrep (reportId);
create index idx_17 on ccp_usedNetSoftWarerep(reportId,isInfraction);
create index idx_18 on ccp_dialingrep(reportId,isInfraction);
create index idx_19 on ccp_browserrep(reportId,isInfraction);
create index idx_20 on ccp_emailrep(reportId,isInfraction);
create index idx_21 on ccp_downloadrep(reportId,isInfraction);
create index idx_22 on ccp_imrep(reportId,isInfraction);
create index idx_23 on ccp_cloudDiskInforep(reportId,isInfraction);
create index idx_24 on ccp_sharefolderrep(reportId,isInfraction);
create index idx_25 on ccp_secsoftwarerep(reportId,isInfraction);
create index idx_26 on ccp_patchrep (reportId);
create index idx_27 on ccp_noPatchRep(reportId,isInfraction);
create index idx_28 on ccp_securitymanagementrep(reportId,isInfraction);
create index idx_29 on ccp_osrep(reportId,isInfraction);
create index idx_30 on ccp_vmwarerep(reportId,isInfraction);
create index idx_31 on ccp_wirelessrep(reportId,isInfraction);
create index idx_32 on ccp_monitorProtectrep(reportId,isInfraction);
create index idx_33 on ccp_firewarerep(reportId,isInfraction);
create index idx_34 on ccp_remoteInfo(reportId);
create index idx_35 on ccp_autoPlay(reportId,isInfraction);
create index idx_36 on ccp_autoUpdate(reportId,isInfraction);
create index idx_37 on ccp_portinforep(reportId,isInfraction);
create index idx_38 on ccp_procedurerep(reportId,isInfraction);
create index idx_39 on ccp_servicerep(reportId,isInfraction);
create index idx_40 on ccp_systemlogrep(reportId,isInfraction);
create index idx_41 on ccp_poweronandoffrep(reportId);
create index idx_42 on ccp_accountsecurityrep(reportId,isInfraction);
create index idx_43 on ccp_pwandacpolicyrep(reportId,isInfraction);
create index idx_44 on ccp_seclogrep(reportId,isInfraction);
create index idx_45 on ccp_userrightrep(reportId,isInfraction);
create index idx_46 on ccp_userpolicyrep(reportId);
create index idx_47 on ccp_grouppolicyrep(reportId);
create index idx_48 on ccp_fileinforep(reportId,isInfraction);
create index idx_49 on ccp_fileencryptrep(reportId);
create index idx_50 on ccp_delFile (reportId);
create index idx_51 on ccp_delFileCheck (reportId,isInfraction);
create index idx_52 on ccp_deepUSBCheck(reportId,isInfraction);
create index idx_53 on ccp_deepURLCheck(reportId,isInfraction);
create indexidx_54 on ccp_sectorCheck (reportId,isInfraction);
任务检查报告:查看报告;优化前耗时7660毫秒,优化后耗时3370毫秒
优化方式:
1:添加索引
create index index_ccp_device_policy on ccp_device_policy(policyId,mark);
create indexindex_ccp_checkreport on ccp_checkreport(policyId,planReportTime);
查询检查项数据:优化前耗时8300毫秒 优化后耗时2073毫秒
优化方式:数据表中添加索引,引用上面sql索引语句
注意:联合索引reportId,isInfraction其实是支持2个索引,reportId和reportId,isINfraction
缓存
MySQL查询缓存的使用
开启查询缓存:
set global query_cache_type = 1; 使用命令开启查询缓存
setglobal query_cache_size = 134217728; 缓存区的大小 设置太小不会生效
show variables like "%query_cache%";
根据query_cache_type这个变量来决定的。
这个变量有三个取值:0,1,2,分别代表了off、on、demand。
mysql默认为开启on
查询 SHOW STATUS LIKE'Qcache_hits'; value为0
查询后Qcache_hits变化
ex:
关闭查询缓存:
C:\Program Files(x86)\MySQL\MySQL Server 5.0 中 my.ini配置
query_cache_type = 0
query_cache_size = 0
或者
set global query_cache_type = 0; 使用命令关闭查询缓存
setglobal query_cache_size = 0;
查询 SHOW STATUS LIKE'Qcache_hits'; value为0
使用查询:select count(*) fromccp_printerrep;
SHOW STATUS LIKE 'Qcache_hits';value值还是为0
show status like"%Qcache%";
开启查询缓存测试:
select * from ccp_softwarerep;
第一次查询耗时:1.434s
第二次查询耗时:0.629s
mysql默认 query_cache 是打开的
你使用 show global variables like '%query_cache%';
确认一下有没有打开,如果打开了。第一次查询读数据文件,第二次就会走query_cache,所以就会很快。当然 ,如果数据更新了,要重新再缓存。
第二种情况 ,表引擎使用innodb.第一次查询也会走数据文件,第二次直接走buffer_pool,也比直接查询数据文件要快
上面的说法是否正确。
本人测试:
--开启查询缓存 在my.ini文件中配置 并重启服务
query_cache_type = 1;
query_cache_size = 134217728;
select * from ccp_usbinforep 语句条数:145011条
--第一次执行语句用时1.181s; 第二次执行用时0.265s
Qcache_hits 为 211 而且会增加说明是在使用查询缓存
--关闭查询缓存 在my.ini 中配置
query_cache_size=0;
query_cache_type = 0;
select * from ccp_usbinforep 语句条数:145011条
第一次执行语句用时0.294s;第二次执行用时0.283s
Qcache_hits为0
以上实验说明,好像mysql缓存不管怎么配置对sql查询的影响时间影响不大,那么配置的意义在于什么呢。
或者以上我哪些配置和理解错误?
如果缓存起作用,就用缓存,缓存关闭就是要表引擎 那么有没缓存影响大么。请大家简单说下自己的理解
查询时,数据库引擎会判断,如果数据在内存中,则会从内存读取数据,如果数据不在内存在,则先从硬盘读到内存,然后再供查询。
所以第一次查的时候,根据你的语句,数据库引擎会把一些数据从硬盘读到内存,第二次再查的时候,就从内存读数据,就快了很多了搜索。
如果这是正确的,那么mysql的缓存有什么意义。。。
网友意见:query cache是第一道缓存,命中则直接返回,否则进入存储引擎层面处理
关掉query cache,直接进入到存储引擎层面,innodb本身也是有缓存机制的
都是从内存中取结果,执行效率差别不大。我的数据库引擎是innodb的,所以不好测试。如果是其他的可以测试看看
分表
mysql数据库中分表优化。
1:使用Mysql的Merge存储引擎实现分表查询
CREATE TABLE t1 ( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
CREATE TABLE t2 ( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
CREATE TABLE total (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20)) ENGINE=MERGE UNION=(t1,t2)INSERT_METHOD=LAST;
对应定期分表的情况下,只要定期相应的增加一个基础表,再修改merge表中的 union 就行了(ALTER TABLE tbl_name UNION=(...))。
如在增加一个表(需和其他基础表一样的结构):
定期增加表逻辑;可以使用MySQL定时创建表的SQL语句
资料网站: http://blog.csdn.NET/qingtian2002/article/details/23825113
mysql中每隔一段时间自动执行一次sql语句
如:table201606 然后执行ALTER TABLE total UNION=(t1,t2,t3,table201606);注意INSERT_METHOD=LAST;
表示新来的数据会插入到生成的新表中。
CREATE TABLE t3( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
ALTER TABLE total UNION=(t1,t2,t3)
INSERT_METHOD=LAST;表示插入的方法,INSERT_METHOD的值可以是 FIRST(插入第一个表),LAST(最后一个表),NO(不能插入)
查询的时候,和平常一样
select * from total where ....
merge表会自动找到相应的基础表进行查询。
注意问题:id可能重复问题
mysql版本必须高于5.0版本
ex:定时创建表
drop event e_createuser;
drop PROCEDURE create_user;
show variables like '%sche%';
set global event_scheduler =1;
create procedure create_user()
BEGIN
set @sql_create_table = concat(
'CREATE TABLE IF NOT EXISTS user', date_format(now(),'%Y%m'),
"(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`sex` int(1) NOT NULL DEFAULT '0',
PRIMARYKEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8AUTO_INCREMENT=1");
PREPARE sql_create_table FROM @sql_create_table;
EXECUTE sql_create_table;
END
create event if not existse_createuser
on schedule every 30 second
on completion preserve
do call create_user();
%Y%m%d%H%i%s 表示年月日时分秒20160630102310
每隔30秒将执行存储过程test,将当前时间更新到examinfo表中id=14的记录的endtime字段中去.
关闭事件任务
alter event e_createuser ON
COMPLETION PRESERVE DISABLE;
开户事件任务
alter event e_createuser ON
COMPLETION PRESERVE ENABLE;
以上测试均成功,测试环境为mysql 5.1
分布式集群
2: 做mysql集群,例如:利用mysql cluster ,mysql proxy,mysql replication,drdb等等
有人会问mysql集群,根分表有什么关系吗?虽然它不是实际意义上的分表,但是它启到了分表的作用,做集群的意义是什么呢?为一个数据库减轻负担,说白了就是减少sql排队队列中的sql的数量,举个例子:有10个sql请求,如果放在一个数据库服务器的排队队列中,他要等很长时间,如果把这10个sql请求,分配到5个数据库服务器的排队队列中,一个数据库服务器的队列中只有2个,这样等待时间是不是大大的缩短了呢?这已经很明显了。所以我把它列到了分表的范围以内,我做过一些mysql的集群:
Linux mysql proxy 的安装,配置,以及读写分离
mysql replication 互为主从的安装及配置,以及数据同步
优点:扩展性好,没有多个分表后的复杂操作(PHP代码)
缺点:单个表的数据量还是没有变,一次操作所花的时间还是那么多,硬件开销大。
集群资料:http://www.th7.cn/db/mysql/201409/72633.shtml
mysql 性能优化索引、缓存、分表、分布式实现方式。的更多相关文章
- MySQL性能优化(五):分表
原文:MySQL性能优化(五):分表 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbi ...
- Mysql性能优化三(分表、增量备份、还原)
接上篇Mysql性能优化二 对表进行水平划分 如果一个表的记录数太多了,比如上千万条,而且需要经常检索,那么我们就有必要化整为零了.如果我拆成100个表,那么每个表只有10万条记录.当然这需要数据在逻 ...
- MYSQL性能优化分享(分库分表)
1.分库分表 很明显,一个主表(也就是很重要的表,例如用户表)无限制的增长势必严重影响性能,分库与分表是一个很不错的解决途径,也就是性能优化途径,现在的案例是我们有一个1000多万条记录的用户表mem ...
- MySQL 性能优化系列之一 单表预处理
MySQL 性能优化系列之一 单表预处理 背景介绍 我们经常在写多表关联的SQL时,会想到 left jion(左关联),right jion(右关联),inner jion(内关联)等. 但是,当表 ...
- MySQL DBA教程:Mysql性能优化之缓存参数优化
在平时被问及最多的问题就是关于 MySQL 数据库性能优化方面的问题,所以最近打算写一个MySQL数据库性能优化方面的系列文章,希望对初中级 MySQL DBA 以及其他对 MySQL 性能优化感 ...
- MySQL性能优化---索引
一.什么是索引 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里 ...
- Mysql性能优化之缓存参数优化
数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所以,要优化 ...
- MySQL性能优化方法二:表结构优化
原文链接:http://isky000.com/database/mysql-perfornamce-tuning-schema 很多人都将 数据库设计范式 作为数据库表结构设计“圣经”,认为只要按照 ...
- MySQL性能优化——索引
原文地址:http://blog.codinglabs.org/articles/theory-of-mysql-index.html InnoDB使用B+Tree作为索引结构 最左前缀原理与相关优化 ...
随机推荐
- 魔兽 如何屏蔽F1键弹出帮助菜单
如下图所示,我在任何时候按F1键,都会自动弹出Windows帮助和支持,事实上这个功能很鸡肋,从来没用过,但是玩魔兽的时候确实必须的,F1控制英雄的,呵呵. 方法还是有的,在任务管理器中找到这个应 ...
- Apple Swift编程语言新手教程
文件夹 1 简单介绍 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 7 枚举与结构 1 简单介绍 今天凌晨Apple刚刚公布了Swift编程 ...
- 用Java开发50个棋类游戏
眼下已经开发完了两个 1A2B 24点 打算开发以下的.直接在QQ上玩. QQ机器人已经有了.我们直接写业务即可.有兴趣的參与.机器人婷婷体验群 Java技术交流 207224939 四棋 小枪大炮 ...
- .net的程序的逆向分析。
背景:碰到一个由c#写的exe,由于之前没有分析过.net的程序,记录下分析流程. 1)peid加载判断类型,可以看出没有加壳. 2)搜索c#的反编译以及调试工具. 1.NET.Reflector以及 ...
- ldd LD_TRACE_LOADED_OBJECTS
1 该环境变量设置为1的话,只会打印所执行的程序的依赖,即所依赖的动态链接库
- mysql将查询结果导出csv文件的方法into outfile
例句: select * from table_name into outfile '/tmp/tmp.csv' fields terminated by ','; 详解: ① into outf ...
- html5 画图板
8个最新炫酷的HTML5动画应用 http://www.php100.com/html/it/qianduan/2015/0107/8281.html 另外一个画板demo http://www.os ...
- 基于ASP.Net Core开发一套通用后台框架记录-(数据库设计(权限模块))
写在前面 本系列博客是本人在学习的过程中搭建学习的记录,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 前期我不会公开源码,我想是一点点敲代码,不然复制.粘贴那就没意思了. ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- [转]c++中的string常用函数用法总结
标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...