1
0
mirror of https://github.com/MariaDB/server.git synced 2025-04-18 21:44:20 +03:00

MDEV-34413 Index Condition Pushdown for reverse-ordered scans

Adds tests which show that ICP was not enabled for reverse-ordered scans
prior to this mdev.  A later commit for this same mdev records again
these same tests, showing that ICP for reverse-ordered scans is
enabled and working.
This commit is contained in:
Dave Gosselin 2025-02-07 11:20:23 -05:00 committed by Dave Gosselin
parent c3f21762e9
commit 261d5520a2
5 changed files with 555 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[myisam]
default-storage-engine=myisam
[innodb]
innodb
default-storage-engine=innodb

View File

@ -0,0 +1,278 @@
create table ten(a int);
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table one_k(a int);
insert into one_k select A.a + B.a* 10 + C.a * 100 from ten A, ten B, ten C;
create table t10 (a int, b int, c int, key(a,b));
insert into t10 select a,a,a from one_k;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
a b c
20 20 20
19 19 19
18 18 18
17 17 17
16 16 16
15 15 15
14 14 14
13 13 13
12 12 12
11 11 11
10 10 10
explain select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 range a a 5 NULL 11 Using where
flush status;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
a b c
20 20 20
19 19 19
18 18 18
17 17 17
16 16 16
15 15 15
14 14 14
13 13 13
12 12 12
11 11 11
10 10 10
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
a b c
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15
16 16 16
17 17 17
18 18 18
19 19 19
20 20 20
explain select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 range a a 5 NULL 11 Using index condition
flush status;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
a b c
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15
16 16 16
17 17 17
18 18 18
19 19 19
20 20 20
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 11
HANDLER_ICP_MATCH 11
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
a b c
10 10 10
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 ref a a 5 const 1 Using where
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
a b c
10 10 10
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
a b c
10 10 10
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 ref a a 5 const 1 Using index condition; Using where
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
a b c
10 10 10
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 1
HANDLER_ICP_MATCH 1
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
a b c
10 10 10
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 ref a a 5 const 1 Using where
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
a b c
10 10 10
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
a b c
10 10 10
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t10 ref a a 5 const 1 Using index condition; Using where
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
a b c
10 10 10
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 1
HANDLER_ICP_MATCH 1
create table t1 (a int, b int, c int, key(a,b));
insert into t1 (a, b, c) values (1,10,100),(2,20,200),(3,30,300),(4,40,400),(5,50,500),(6,60,600),(7,70,700),(8,80,800),(9,90,900),(10,100,1000);
select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
a b c
3 30 300
explain select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 1 Using where
flush status;
select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
a b c
3 30 300
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
a b c
3 30 300
explain select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 1 Using index condition
flush status;
select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
a b c
3 30 300
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 1
HANDLER_ICP_MATCH 1
drop table t1;
create table t1 (a int, b int, c int, key(a,b));
insert into t1 (a, b, c) values (1,10,100),(2,20,200),(3,30,300);
select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
a b c
2 20 200
explain select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 1 Using where
flush status;
select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
a b c
2 20 200
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
a b c
2 20 200
explain select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 1 Using index condition
flush status;
select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
a b c
2 20 200
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 1
HANDLER_ICP_MATCH 1
drop table ten, one_k, t10, t1;
create table t1 (
a int not null,
b int not null,
c int not null,
key (a,b)
) partition by range ((a)) (
partition p0 values less than (5),
partition p1 values less than (10),
partition p2 values less than (15),
partition p3 values less than (20)
);
insert into t1 (a,b,c) values (1,1,1),(2,2,2),(3,3,3),
(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9),(10,10,10),
(11,11,11),(12,12,12),(13,13,13),(14,14,14),(15,15,15),
(16,16,16),(17,17,17),(18,18,18),(19,19,19);
select * from t1 where a >= 3 and a <= 7 order by a desc;
a b c
7 7 7
6 6 6
5 5 5
4 4 4
3 3 3
explain select * from t1 where a >= 3 and a <= 7 order by a desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 4 NULL 5 Using where
flush status;
select * from t1 where a >= 3 and a <= 7 order by a desc;
a b c
7 7 7
6 6 6
5 5 5
4 4 4
3 3 3
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
a b c
7 7 7
6 6 6
5 5 5
4 4 4
3 3 3
explain select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 4 NULL 5 Using where
flush status;
select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
a b c
7 7 7
6 6 6
5 5 5
4 4 4
3 3 3
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
drop table t1;
create table t1 (
pk int primary key,
kp1 int, kp2 int,
col1 int,
index (kp1,kp2)
) partition by hash (pk) partitions 10;
insert into t1 select seq, seq, seq, seq from seq_1_to_1000;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 asc, kp2 asc;
pk kp1 kp2 col1
flush status;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 asc, kp2 asc;
pk kp1 kp2 col1
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 11
HANDLER_ICP_MATCH 0
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 desc, kp2 desc;
pk kp1 kp2 col1
flush status;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 desc, kp2 desc;
pk kp1 kp2 col1
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
VARIABLE_NAME VARIABLE_VALUE
HANDLER_ICP_ATTEMPTS 0
HANDLER_ICP_MATCH 0
drop table t1;

View File

@ -0,0 +1,162 @@
--source include/have_innodb.inc
--source include/have_partition.inc
--source include/have_sequence.inc
create table ten(a int);
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table one_k(a int);
insert into one_k select A.a + B.a* 10 + C.a * 100 from ten A, ten B, ten C;
create table t10 (a int, b int, c int, key(a,b));
insert into t10 select a,a,a from one_k;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
--disable_ps_protocol
explain select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
flush status;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a desc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
--disable_ps_protocol
explain select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
flush status;
select * from t10 force index(a) where a between 10 and 20 and b+1 <3333 order by a asc, b asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
--disable_ps_protocol
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
--disable_ps_protocol
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
--disable_ps_protocol
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a asc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
--disable_ps_protocol
explain select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
flush status;
select * from t10 force index(a) where a=10 and b+1 <3333 order by a desc, b asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
create table t1 (a int, b int, c int, key(a,b));
insert into t1 (a, b, c) values (1,10,100),(2,20,200),(3,30,300),(4,40,400),(5,50,500),(6,60,600),(7,70,700),(8,80,800),(9,90,900),(10,100,1000);
select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
--disable_ps_protocol
explain select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
flush status;
select * from t1 where a >= 3 and a <= 3 order by a desc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
--disable_ps_protocol
explain select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
flush status;
select * from t1 where a >= 3 and a <= 3 order by a asc, b asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
drop table t1;
create table t1 (a int, b int, c int, key(a,b));
insert into t1 (a, b, c) values (1,10,100),(2,20,200),(3,30,300);
select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
--disable_ps_protocol
explain select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
flush status;
select * from t1 where a >= 2 and a <= 2 order by a desc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
--disable_ps_protocol
explain select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
flush status;
select * from t1 where a >= 2 and a <= 2 order by a asc, b asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
drop table ten, one_k, t10, t1;
create table t1 (
a int not null,
b int not null,
c int not null,
key (a,b)
) partition by range ((a)) (
partition p0 values less than (5),
partition p1 values less than (10),
partition p2 values less than (15),
partition p3 values less than (20)
);
insert into t1 (a,b,c) values (1,1,1),(2,2,2),(3,3,3),
(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9),(10,10,10),
(11,11,11),(12,12,12),(13,13,13),(14,14,14),(15,15,15),
(16,16,16),(17,17,17),(18,18,18),(19,19,19);
select * from t1 where a >= 3 and a <= 7 order by a desc;
--disable_ps_protocol
explain select * from t1 where a >= 3 and a <= 7 order by a desc;
flush status;
select * from t1 where a >= 3 and a <= 7 order by a desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
--disable_ps_protocol
explain select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
flush status;
select * from t1 where a >= 3 and a <= 7 order by a desc, b desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
drop table t1;
create table t1 (
pk int primary key,
kp1 int, kp2 int,
col1 int,
index (kp1,kp2)
) partition by hash (pk) partitions 10;
insert into t1 select seq, seq, seq, seq from seq_1_to_1000;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 asc, kp2 asc;
--disable_ps_protocol
flush status;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 asc, kp2 asc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 desc, kp2 desc;
--disable_ps_protocol
flush status;
select * from t1 where kp1 between 950 and 960 and kp2+1 >33333 order by kp1 desc, kp2 desc;
SELECT * FROM information_schema.SESSION_STATUS WHERE VARIABLE_NAME LIKE '%icp%';
--enable_ps_protocol
drop table t1;

View File

@ -3795,6 +3795,96 @@ ANALYZE
] ]
} }
} }
set @save_optimizer_switch= @@optimizer_switch;
set optimizer_switch='rowid_filter=off';
analyze format=json select * from t1
where
a =30100 and b in (30100,30101,30102)
and
(a+pk)>30100+8439
order by pk desc;
ANALYZE
{
"query_optimization": {
"r_total_time_ms": "REPLACED"
},
"query_block": {
"select_id": 1,
"cost": "REPLACED",
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ref",
"possible_keys": ["a", "b"],
"key": "a",
"key_length": "5",
"used_key_parts": ["a"],
"ref": ["const"],
"loops": 1,
"r_loops": 1,
"rows": 250,
"r_rows": 250,
"cost": "REPLACED",
"r_table_time_ms": "REPLACED",
"r_other_time_ms": "REPLACED",
"r_engine_stats": REPLACED,
"filtered": 4.799086094,
"r_total_filtered": 0.8,
"attached_condition": "t1.a <=> 30100 and t1.b in (30100,30101,30102) and 30100 + t1.pk > 38539",
"r_filtered": 0.8
}
}
]
}
}
set optimizer_switch='rowid_filter=on';
analyze format=json select * from t1
where
a =30100 and b in (30100,30101,30102)
and
(a+pk)>30100+8439
order by pk desc;
ANALYZE
{
"query_optimization": {
"r_total_time_ms": "REPLACED"
},
"query_block": {
"select_id": 1,
"cost": "REPLACED",
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ref",
"possible_keys": ["a", "b"],
"key": "a",
"key_length": "5",
"used_key_parts": ["a"],
"ref": ["const"],
"loops": 1,
"r_loops": 1,
"rows": 250,
"r_rows": 250,
"cost": "REPLACED",
"r_table_time_ms": "REPLACED",
"r_other_time_ms": "REPLACED",
"r_engine_stats": REPLACED,
"filtered": 4.799086094,
"r_total_filtered": 0.8,
"attached_condition": "t1.a <=> 30100 and t1.b in (30100,30101,30102) and 30100 + t1.pk > 38539",
"r_filtered": 0.8
}
}
]
}
}
set optimizer_switch=@save_optimizer_switch;
drop table t1; drop table t1;
# End of 10.6 tests # End of 10.6 tests
set global innodb_stats_persistent= @stats.save; set global innodb_stats_persistent= @stats.save;

View File

@ -818,6 +818,25 @@ where
order by order by
pk desc; pk desc;
set @save_optimizer_switch= @@optimizer_switch;
set optimizer_switch='rowid_filter=off';
--source include/analyze-format.inc
analyze format=json select * from t1
where
a =30100 and b in (30100,30101,30102)
and
(a+pk)>30100+8439
order by pk desc;
set optimizer_switch='rowid_filter=on';
--source include/analyze-format.inc
analyze format=json select * from t1
where
a =30100 and b in (30100,30101,30102)
and
(a+pk)>30100+8439
order by pk desc;
set optimizer_switch=@save_optimizer_switch;
drop table t1; drop table t1;
--echo # End of 10.6 tests --echo # End of 10.6 tests