index condition pushdown

Index condition pushdown(ICP)是直到mysql5.6才引入的特性,主要是为了减少通过二级索引查找主键索引的次数。目前ICP相关的文章也比较多,本文主要从源码角度介绍ICP的实现。讨论之前,我们先再温习下。

以下图片来自mariadb

  • 引入ICP之前 screenshot.png

  • 引入ICP之后 screenshot.png

再来看个例子

  1. CREATE TABLE `t1` (
  2. `a` int(11) DEFAULT NULL,
  3. `b` char(8) DEFAULT NULL,
  4. `c` int(11) DEFAULT '0',
  5. `pk` int(11) NOT NULL AUTO_INCREMENT,
  6. PRIMARY KEY (`pk`),
  7. KEY `idx1` (`a`,`b`)
  8. ) ENGINE=ROCKSDB;
  9. INSERT INTO t1 (a,b) VALUES (1,'a'),(2,'b'),(3,'c');
  10. INSERT INTO t1 (a,b) VALUES (4,'a'),(4,'b'),(4,'c'),(4,'d'),(4,'e'),(4,'f');
  11. set optimizer_switch='index_condition_pushdown=off';
  12. ## 关闭ICP(Using where)
  13. explain select * from t1 where a=4 and b!='e';
  14. +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
  15. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  16. +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
  17. | 1 | SIMPLE | t1 | range | idx1 | idx1 | 14 | NULL | 2 | Using where |
  18. +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
  19. ## 关闭ICP走cover index(Using where; Using index)
  20. explain select a,b from t1 where a=4 and b!='e';
  21. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
  22. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  23. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
  24. | 1 | SIMPLE | t1 | ref | idx1 | idx1 | 5 | const | 4 | Using where; Using index |
  25. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
  26. set optimizer_switch='index_condition_pushdown=on';
  27. ## 开启ICP(Using index conditione)
  28. explain select * from t1 where a=4 and b!='e';
  29. +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
  30. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  31. +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
  32. | 1 | SIMPLE | t1 | range | idx1 | idx1 | 14 | NULL | 2 | Using index condition |
  33. +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
  34. ## 开启ICP仍然是cover index(Using where; Using index)
  35. explain select a,b from t1 where a=4 and b!='e';
  36. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
  37. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  38. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+
  39. | 1 | SIMPLE | t1 | ref | idx1 | idx1 | 5 | const | 4 | Using where; Using index |
  40. +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+

这里总结下ICP的条件

server层主要负责判断是否符合ICP的条件,符合ICP则把需要的condition push到engine层。 engine层通过二级索引查找数据时,用server层push的condition再做一次判断,如果符合条件才会去查找主索引。

目前mysql支持ICP的引擎有MyISAM和InnoDB,MyRocks引入rocksdb后,也支持了ICP。 server层实现是一样的,engine层我们主要介绍innodb和rocksdb的实现。

server层

关键代码片段如下

  1. make_join_readinfo()
  2. switch (tab->type) {
  3. case JT_EQ_REF:
  4. case JT_REF_OR_NULL:
  5. case JT_REF:
  6. if (tab->select)
  7. tab->select->set_quick(NULL);
  8. delete tab->quick;
  9. tab->quick=0;
  10. /* fall through */
  11. case JT_SYSTEM:
  12. case JT_CONST:
  13. /* Only happens with outer joins */
  14. if (setup_join_buffering(tab, join, options, no_jbuf_after,
  15. &icp_other_tables_ok))
  16. DBUG_RETURN(true);
  17. if (tab->use_join_cache != JOIN_CACHE::ALG_NONE)
  18. tab[-1].next_select= sub_select_op;
  19. if (table->covering_keys.is_set(tab->ref.key) &&
  20. !table->no_keyread)
  21. table->set_keyread(TRUE);
  22. else
  23. push_index_cond(tab, tab->ref.key, icp_other_tables_ok,
  24. &trace_refine_table);
  25. break;

从代码中看出只有符合的类型range, ref, eq_ref, and ref_or_null 二级索引才可能会push_index_cond。

而这里通过covering_keys来判断并排除使用了cover index的情况。covering_keys是一个bitmap,保存了所有可能用到的覆盖索引。在解析查询列以及条件列时会设置covering_keys,详细可以参考setup_fields,setup_wild,setup_conds。

engine层

innodb

innodb在扫描二级索引时会根据是否有push condition来检查记录是否符合条件(row_search_idx_cond_check) 逻辑如下:

  1. row_search_for_mysql()
  2. ......
  3. if (prebuilt->idx_cond)
  4. {
  5. row_search_idx_cond_check //检查condition
  6. row_sel_get_clust_rec_for_mysql //检查通过了才会去取主索引数据
  7. }
  8. ....

典型的堆栈如下

  1. handler::compare_key_icp
  2. innobase_index_cond
  3. row_search_idx_cond_check
  4. row_search_for_mysql
  5. ha_innobase::index_read
  6. ha_innobase::index_first
  7. ha_innobase::rnd_next
  8. handler::ha_rnd_next
  9. rr_sequential
  10. join_init_read_record
  11. sub_select
  12. do_select

rocksdb

rocksdb在扫描二级索引时也会根据是否有push condition来检查记录是否符合条件

逻辑如下

  1. read_row_from_secondary_key()
  2. {
  3. find_icp_matching_index_rec//push了condition才会检查condition
  4. get_row_by_rowid//检查通过了才会去取主索引数据
  5. }

典型的堆栈如下

  1. handler::compare_key_icp
  2. myrocks::ha_rocksdb::check_index_cond
  3. myrocks::ha_rocksdb::find_icp_matching_index_rec
  4. myrocks::ha_rocksdb::read_row_from_secondary_key
  5. myrocks::ha_rocksdb::index_read_map_impl
  6. myrocks::ha_rocksdb::read_range_first
  7. handler::multi_range_read_next

other

ICP对cover index作出了严格的限制,而实际上应该可以放开此限制,这样可以减少enging层传第给server层的数据量,至少可以减少server层的内存使用。欢迎指正!