zabbix数据库优化(未完)

姑且当这个是对zabbix数据库弄乱了index修复过程,因为也没去细对比zabbix默认数据库index。

补充一段关于数据库索引的信息:

Index Condition Pushdown (ICP)是mysql使用索引从表中检索行数据的一种优化方式。

创建索引的目的是快速从整体集合中选择性地读取满足条件的一部分集合。为了实现以最少的索引满足对某个表的多样化的数据读取要求,应当为每个索引分配合理的任务。

    1)在允许的情况下,对具有较好离散度的列单独创建索引,这样可以提高该索引的使用弹性;

    2)对于离散度较差的列,通过对多列进行合理的组合来创建组合索引,虽然这样做在很大程度上降低了各个列的使用弹性,但是却可以发挥多个列的综合效应。

using index 和using where只要使用了索引我们基本都能经常看到,而using index condition则是在mysql5.6后新加的新特性,我们先来看看mysql文档对using index condition的描述

附上mysql文档链接:https://dev.mysql.com/doc/refman/5.7/en/index-condition-pushdown-optimization.html

简单来说,mysql开启了ICP的话,可以减少存储引擎访问基表的次数

下面来简单的介绍一下这三者的区别

using index :使用覆盖索引的时候就会出现

using where:在查找使用索引的情况下,需要回表去查询所需的数据

using index condition:查找使用了索引,但是需要回表查询数据

using index & using where:查找使用了索引,但是需要的数据都在索引列中能找到,所以不需要回表查询数据

以上四点就能看出它们之前的区别,或许有部分人都存在疑惑 using index & using where 和using index condition那个比较好,从上面的的解释中就能看出是前者比较好,毕竟不需要回表查询数据,效率上应该比较快的

下面是在stackoverflow中找到的答案:

附上stackoverflow链接:https://stackoverflow.com/questions/28759576/mysql-using-index-condition-vs-using-where-using-index
--------------------- 
实例处理一

[root@gt-zabbix mariadb]# tail -f slow.log

# Time: 190803 19:49:08
# User@Host: zabbix[zabbix] @ localhost []
# Thread_id: 91  Schema: zabbix  QC_hit: No
# Query_time: 2.138774  Lock_time: 0.000105  Rows_sent: 51907  Rows_examined: 182133
SET timestamp=1564832948;
select i.itemid,f.functionid,f.function,f.parameter,t.triggerid from hosts h,items i,functions f,triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and h.status in (0,1) and t.flags<>2;

上面一次查询18万条。

MariaDB [zabbix]> use zabbix;
MariaDB [zabbix]> explain select i.itemid,f.functionid,f.function,f.parameter,t.triggerid from hosts h,items i,functions f,triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and h.status in (0,1) and t.flags<>2\g

 

 

MariaDB [zabbix]> show index from triggers;

 看到列flags没有建立索引,

为列flags添加索引

MariaDB [zabbix]> alter table triggers add index idx_f(flags);

 

MariaDB [zabbix]> explain select i.itemid,f.functionid,f.function,f.parameter,t.triggerid from hosts h,items i,functions f,triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and h.status in (0,1) and t.flags<>2;

看到优化后triggers使用idx_f 索引,检索行数由于26078减少为16165,extra由using where变为using where; Using index

MariaDB [zabbix]> select SQL_NO_CACHE i.itemid,f.functionid,f.function,f.parameter,t.triggerid from hosts h,items i,functions f,triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and h.status in (0,1) and t.flags<>2;

看到执行过程只花0.19s。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值