{"id":6867,"date":"2017-07-21T15:28:52","date_gmt":"2017-07-21T07:28:52","guid":{"rendered":"http:\/\/rmohan.com\/?p=6867"},"modified":"2017-07-21T15:28:52","modified_gmt":"2017-07-21T07:28:52","slug":"mysql-index-conditions-push-a-simple-test","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6867","title":{"rendered":"MySQL index conditions push a simple test"},"content":{"rendered":"<p>Since MySQL 5.6, there have been some improvements in the index, such as the index condition pushdown (ICP), strictly speaking, the optimizer level improvement.<\/p>\n<p>If it is simple to understand, that is, the optimizer will try to index the treatment from the Server layer to the storage engine layer.\u00a0As an example, there is a table containing the combined index idx_cols containing (c1, c2, &#8230;, cn) n columns, if there is a range scan on the where1 condition, then the remaining c2, &#8230;, cn this n-1 On the index can not be used to extract and filter data, and ICP is to optimize this thing.<\/p>\n<p>We are in the MySQL 5.6 environment to a simple test.<\/p>\n<p>We create the table emp, which contains a primary key, a combined index to illustrate.<\/p>\n<p>Create Table EMP (<br \/>\nEMPNO smallint The (. 5) unsigned Not null AUTO_INCREMENT,<br \/>\nename VARCHAR (30) Not null,<br \/>\nDEPTNO smallint The (. 5) unsigned Not null,<br \/>\nJob VARCHAR (30) Not null,<br \/>\nPrimary Key (EMPNO),<br \/>\nKey idx_emp_info (DEPTNO, Ename)<br \/>\n) engine = InnoDB charset = utf8;<\/p>\n<p>Of course, I also randomly inserted a few data, meaning that<\/p>\n<p>Insert the emp values ??(1, &#8216;zhangsan&#8217;, 1, &#8216;CEO&#8217;), (2, &#8216;lisi&#8217;, 2, &#8216;CFO&#8217;), (3, &#8216;wangwu&#8217;, 3, &#8216;CTO&#8217;), (4, Jeanron100 &#8216;, 3,&#8217; Enginer &#8216;);<\/p>\n<p>ICP control in the database parameters have an optimizer parameter optimizer_switch to unified management, I think this is the MySQL optimizer from our closest time.\u00a0You can use the following way to view.<\/p>\n<p>Show variables like &#8216;optimizer_switch&#8217;;<\/p>\n<p>Of course, in the previous version of 5.6, you can not see the index condition pushdown this kind of words.\u00a0The results in the 5.6 version are as follows:<\/p>\n<p>In the same way as in the case of the following event: index_merge_switch = on, mrr = on, mrr_cost_based = on, block_nested_loop = on, batched_key_access = off, materialization = On, semijoin = on, loosescan = on, firstmatch = on, subquery_materialization_cost_based_ed, use_index_extensions = on Here we use two statements to compare the description, through the implementation of the plan to contrast.<\/p>\n<p>Set optimizer_switch = &#8220;index_condition_pushdown = off&#8221;<\/p>\n<p>&gt; Explain select * from emp where deptno between 1 and 100 and ename = &#8216;jeanron100&#8217;;<br \/>\n+ &#8212;- + &#8212;&#8212;&#8212;&#8212;- + &#8212;&#8212;- + &#8212; &#8212; + &#8212;&#8212;&#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; +<br \/>\n| __-\u00a0+\u00a0| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |<br \/>\n+ &#8212;- + &#8212;&#8212;&#8211; &#8212;&#8211; + &#8212;&#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; &#8212;<br \/>\n|\u00a0+ + + + +\u00a0_&#8212;\u00a0+\u00a0| 1 | SIMPLE | emp | ALL | idx_emp_info | NULL | NULL | NULL | 4 |\u00a0Using where\u00a0|<br \/>\n+ &#8212;- + &#8212;&#8212;&#8212;&#8212;- + &#8212;&#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8211; &#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8212;- +<\/p>\n<p>And if it is turned on, see if ICP is enabled.<br \/>\nSet optimizer_switch = &#8220;index_condition_pushdown = on&#8221;;&gt; explain select * from emp where deptno between 10 and 3000 and ename = &#8216;jeanron100&#8217;;<\/p>\n<p>&gt; Explain select * from emp where deptno between 1 and 300 and ename = &#8216;jeanron100&#8217;;<br \/>\n+ &#8212;- + &#8212;&#8212;&#8212;&#8212;- + &#8212;&#8212;- + &#8212; &#8212; + &#8212;&#8212;&#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; +<br \/>\n| __-\u00a0+\u00a0| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |<br \/>\n+ &#8212;- + &#8212;&#8212;&#8211; &#8212;&#8211; + &#8212;&#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; &#8212;<br \/>\n|\u00a0+ + + + +\u00a0_&#8212;\u00a0+\u00a0| 1 | SIMPLE | emp | ALL | idx_emp_info | NULL | NULL | NULL | 4 |\u00a0Using where\u00a0|<br \/>\n+ &#8212;- + &#8212;&#8212;&#8212;&#8212;- + &#8212;&#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8211; &#8212;- + &#8212;&#8212; + &#8212;&#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212; + &#8212;&#8212;&#8212;&#8212;- +<br \/>\n1 row in set (0.00 sec) This place is worth considering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since MySQL 5.6, there have been some improvements in the index, such as the index condition pushdown (ICP), strictly speaking, the optimizer level improvement.<\/p>\n<p>If it is simple to understand, that is, the optimizer will try to index the treatment from the Server layer to the storage engine layer. As an example, there is a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6867"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6867"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6867\/revisions"}],"predecessor-version":[{"id":6868,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6867\/revisions\/6868"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}