create table t1 (a int, b int);
insert into t1 (a, b) values
(1, 1000),
(2, 2000),
(3, 3000),
(4, 4000),
(5, 5000),
(6, 6000),
(7, 7000),
(8, 8000),
(9, 9000),
(10, 10000);
create index t1_a on t1 (a);
create index t1_b on t1 (b);
set @old_where_cost=@@optimizer_where_cost;
set session optimizer_where_cost=10;
explain format=json select * from t1 where t1.a between 3 and 8 and t1.b between 4000 and 4100 order by t1.b desc;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["t1_a", "t1_b"],
          "key": "t1_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "rowid_filter": {
            "range": {
              "key": "t1_a",
              "used_key_parts": ["a"]
            },
            "rows": 6,
            "selectivity_pct": 60
          },
          "loops": 1,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 60,
          "index_condition": "t1.b between 4000 and 4100",
          "attached_condition": "t1.a between 3 and 8"
        }
      }
    ]
  }
}
select * from t1 where t1.a between 3 and 8 and t1.b between 4000 and 4100 order by t1.b desc;
a	b
4	4000
flush status;
select * from t1 where t1.a between 3 and 8 and t1.b between 4000 and 4100 order by t1.b desc;
a	b
4	4000
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME	VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS	1
HANDLER_ICP_MATCH	1
explain format=json select * from t1 where t1.a between 2 and 9 and t1.b between 4000 and 5001 order by t1.b desc;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["t1_a", "t1_b"],
          "key": "t1_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "rowid_filter": {
            "range": {
              "key": "t1_a",
              "used_key_parts": ["a"]
            },
            "rows": 8,
            "selectivity_pct": 80
          },
          "loops": 1,
          "rows": 2,
          "cost": "COST_REPLACED",
          "filtered": 80,
          "index_condition": "t1.b between 4000 and 5001",
          "attached_condition": "t1.a between 2 and 9"
        }
      }
    ]
  }
}
select * from t1 where t1.a between 2 and 9 and t1.b between 4000 and 5001 order by t1.b desc;
a	b
5	5000
4	4000
flush status;
select * from t1 where t1.a between 2 and 9 and t1.b between 4000 and 5001 order by t1.b desc;
a	b
5	5000
4	4000
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME	VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS	2
HANDLER_ICP_MATCH	2
set @old_optimizer_switch=@@optimizer_switch;
set optimizer_switch='index_condition_pushdown=off';
explain format=json select * from t1 where t1.a between 3 and 8 and t1.b between 4000 and 4100 order by t1.b desc;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": "COST_REPLACED",
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["t1_a", "t1_b"],
          "key": "t1_b",
          "key_length": "5",
          "used_key_parts": ["b"],
          "rowid_filter": {
            "range": {
              "key": "t1_a",
              "used_key_parts": ["a"]
            },
            "rows": 6,
            "selectivity_pct": 60
          },
          "loops": 1,
          "rows": 1,
          "cost": "COST_REPLACED",
          "filtered": 60,
          "attached_condition": "t1.a between 3 and 8 and t1.b between 4000 and 4100"
        }
      }
    ]
  }
}
select * from t1 where t1.a between 3 and 8 and t1.b between 4000 and 4100 order by t1.b desc;
a	b
4	4000
flush status;
select * from t1 where t1.a between 2 and 9 and t1.b between 4000 and 5001 order by t1.b desc;
a	b
5	5000
4	4000
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME	VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS	0
HANDLER_ICP_MATCH	0
set optimizer_switch=@old_optimizer_switch;
set session optimizer_where_cost=@old_where_cost;
drop table t1;
