MySQL Index Condition Pushdown(ICP)性能优化方法实例_MySQL教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:MySQL order by性能优化方法实例
这篇文章主要介绍了MySQL order by性能优化方法实例,本文讲解了MySQL中order by的原理和优化order by的三种方法,需要的朋友可以参考下 前言 工作过程中,各种业务需求在访问数据库的时候要求有order by排序。有时候不必要的或者不合理的排序操作很可能导致数据库系统崩

   这篇文章主要介绍了MySQL Index Condition Pushdown(ICP)性能优化方法实例,本文讲解了概念介绍、原理、实践案例、案例分析、ICP的使用限制等内容,需要的朋友可以参考下

  一 概念介绍

  Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式。

  a 当关闭ICP时,index 仅仅是data access 的一种访问方式,存储引擎通过索引回表获取的数据会传递到MySQL Server 层进行where条件过滤。

  b 当打开ICP时,如果部分where条件能使用索引中的字段,MySQL Server 会把这部分下推到引擎层,可以利用index过滤的where条件在存储引擎层进行数据过滤,而非将所有通过index access的结果传递到MySQL server层进行where过滤.

  优化效果:ICP能减少引擎层访问基表的次数和MySQL Server 访问存储引擎的次数,减少io次数,提高查询语句性能。

  二 原理

  Index Condition Pushdown is not used:

  1 Get the next row, first by reading the index tuple, and then by using the index tuple to locate and read the full table row.

  2 Test the part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.

  Index Condition Pushdown is used

  1 Get the next row s index tuple (but not the full table row).

  2 Test the part of the WHERE condition that applies to this table and can be checked using only index columns.

  If the condition is not satisfied, proceed to the index tuple for the next row.

  3 If the condition is satisfied, use the index tuple to locate and read the full table row.

  4 est the remaining part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.

  三 实践案例

  a 环境准备

  数据库版本 5.6.16

  关闭缓存

  代码如下:

  set query_cache_size=0;

  set query_cache_type=OFF;

  测试数据下载地址

  b 当开启ICP时

  代码如下:

  mysql> SET profiling = 1;

  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> select * from employees where first_name='Anneke' and last_name like '%sig' ;

  +--------+------------+------------+-----------+--------+------------+

  | emp_no | birth_date | first_name | last_name | gender | hire_date |

  +--------+------------+------------+-----------+--------+------------+

  | 10006 | 1953-04-20 | Anneke | Preusig | F | 1989-06-02 |

  +--------+------------+------------+-----------+--------+------------+

  1 row in set (0.00 sec)

  mysql> show profiles;

  +----------+------------+--------------------------------------------------------------------------------+

  | Query_ID | Duration | Query |

  +----------+------------+--------------------------------------------------------------------------------+

  | 1 | 0.00060275 | select * from employees where first_name='Anneke' and last_name like '%sig' |

  +----------+------------+--------------------------------------------------------------------------------+

  3 rows in set, 1 warning (0.00 sec)

  此时情况下根据MySQL的最左前缀原则, first_name 可以使用索引,last_name采用了like 模糊查询,不能使用索引。

  c 关闭ICP

   代码如下:

  mysql> set optimizer_switch='index_condition_pushdown=off';

  Query OK, 0 rows affected (0.00 sec)

  mysql> SET profiling = 1;

  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> select * from employees where first_name='Anneke' and last_name like '%sig' ;

  +--------+------------+------------+-----------+--------+------------+

  | emp_no | birth_date | first_name | last_name | gender | hire_date |

  +--------+------------+------------+-----------+--------+------------+

  | 10006 | 1953-04-20 | Anneke | Preusig | F | 1989-06-02 |

  +--------+------------+------------+-----------+--------+------------+

  1 row in set (0.00 sec)

  mysql> SET profiling = 0;

  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> show profiles;

  +----------+------------+--------------------------------------------------------------------------------+

  | Query_ID | Duration | Query |

  +----------+------------+--------------------------------------------------------------------------------+

  | 2 | 0.00097000 | select * from employees where first_name='Anneke' and last_name like '%sig' |

  +----------+------------+--------------------------------------------------------------------------------+

  6 rows in set, 1 warning (0.00 sec)

  当开启ICP时 查询在sending data环节时间消耗是 0.000189s

   代码如下:

  mysql> show profile cpu,block io for query 1;

  +----------------------+----------+----------+------------+--------------+---------------+

  | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |

  +----------------------+----------+----------+------------+--------------+---------------+

  | starting | 0.000094 | 0.000000 | 0.000000 | 0 | 0 |

  | checking permissions | 0.000011 | 0.000000 | 0.000000 | 0 | 0 |

  | Opening tables | 0.000025 | 0.000000 | 0.000000 | 0 | 0 |

  | init | 0.000044 | 0.000000 | 0.000000 | 0 | 0 |

  | System lock | 0.000014 | 0.000000 | 0.000000 | 0 | 0 |

  | optimizing | 0.000021 | 0.000000 | 0.000000 | 0 | 0 |

  | statistics | 0.000093 | 0.000000 | 0.000000 | 0 | 0 |

  | preparing | 0.000024 | 0.000000 | 0.000000 | 0 | 0 |

  | executing | 0.000006 | 0.000000 | 0.000000 | 0 | 0 |

  | Sending data | 0.000189 | 0.000000 | 0.000000 | 0 | 0 |

  | end | 0.000019 | 0.000000 | 0.000000 | 0 | 0 |

  | query end | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |

  | closing tables | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |

  | freeing items | 0.000034 | 0.000000 | 0.000000 | 0 | 0 |

  | cleaning up | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |

  +----------------------+----------+----------+------------+--------------+---------------+

  15 rows in set, 1 warning (0.00 sec)

  当关闭ICP时 查询在sending data环节时间消耗是 0.000735s

   代码如下:

  mysql> show profile cpu,block io for query 2;

  +----------------------+----------+----------+------------+--------------+---------------+

  | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |

  +----------------------+----------+----------+------------+--------------+---------------+

  | starting | 0.000045 | 0.000000 | 0.000000 | 0 | 0 |

  | checking permissions | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |

  | Opening tables | 0.000015 | 0.000000 | 0.000000 | 0 | 0 |

  | init | 0.000024 | 0.000000 | 0.000000 | 0 | 0 |

  | System lock | 0.000009 | 0.000000 | 0.000000 | 0 | 0 |

  | optimizing | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |

  | statistics | 0.000049 | 0.000000 | 0.000000 | 0 | 0 |

  | preparing | 0.000016 | 0.000000 | 0.000000 | 0 | 0 |

  | executing | 0.000005 | 0.000000 | 0.000000 | 0 | 0 |

  | Sending data | 0.000735 | 0.001000 | 0.000000 | 0 | 0 |

  | end | 0.000008 | 0.000000 | 0.000000 | 0 | 0 |

  | query end | 0.000008 | 0.000000 | 0.000000 | 0 | 0 |

  | closing tables | 0.000009 | 0.000000 | 0.000000 | 0 | 0 |

  | freeing items | 0.000023 | 0.000000 | 0.000000 | 0 | 0 |

  | cleaning up | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |

  +----------------------+----------+----------+------------+--------------+---------------+

  15 rows in set, 1 warning (0.00 sec)

  从上面的profile 可以看出ICP 开启时整个sql 执行时间是未开启的2/3,sending data 环节的时间消耗前者仅是后者的1/4。

  ICP 开启时的执行计划 含有 Using index condition 标示 ,表示优化器使用了ICP对数据访问进行优化。

   代码如下:

  mysql> explain select * from employees where first_name='Anneke' and last_name like '%nta' ;

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+

  | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+

  | 1 | SIMPLE | employees | ref | idx_emp_fnln | idx_emp_fnln | 44 | const | 224 | Using index condition |

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+

  1 row in set (0.00 sec)

  ICP 关闭时的执行计划显示use where.

   代码如下:

  mysql> explain select * from employees where first_name='Anneke' and last_name like '%nta' ;

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+

  | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+

  | 1 | SIMPLE | employees | ref | idx_emp_fnln | idx_emp_fnln | 44 | const | 224 | Using where |

  +----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+

  1 row in set (0.00 sec)

  案例分析

  以上面的查询为例关闭ICP 时,存储引擎通前缀index first_name 访问表中225条first_name 为Anneke的数据,并在MySQL server层根据last_name like '%sig' 进行过滤

分享:MySQL slave_net_timeout参数解决的一个集群问题案例
这篇文章主要介绍了MySQL slave_net_timeout参数解决的一个集群问题案例,问题日志请见正文,本文使用slave_net_timeout参数解决了这个问题,需要的朋友可以参考下 【背景】 对一套数据库集群进行5.5升级到5.6之后,alter.log 报warning异常。 复制代码 代码如下: 2015-02-

共2页上一页12下一页
来源:模板无忧//所属分类:MySQL教程/更新时间:2015-05-30
相关MySQL教程