Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comb multi in predicate #160

Merged
merged 2 commits into from
Sep 6, 2021
Merged

comb multi in predicate #160

merged 2 commits into from
Sep 6, 2021

Conversation

wy1433
Copy link
Contributor

@wy1433 wy1433 commented Aug 27, 2021

允许多个in组合条件命中索引
例如索引: key idx_abcd(a, b, c, d), 条件:a in ("a1", "a2") and (b , c) in (("b1", "c1"), ("b2","c2")), 会匹配索引a,b,c三个字段
条件a全匹配被裁剪,条件(b, c)全部字段匹配索引索引被裁剪,裁剪后的条件会被组合展开为索引的ranges:

a1, b1, c1
a2, b1, c1
a1, b2, c2
a2, b2, c2

若一个字段对应多个in表达式,则只会使用一个,例如:(a , b) in (("a1", "b1"), ("a2","b2")) and (b , c) in (("b1", "c1"), ("b2","c2"), ("b3","c3")), b字段同时对应了2个in条件,则b字段只会使用in的值较少的一个,本例为会使用(a,b)
(a,b)全部字段匹配索引将会被裁剪掉,(b, c)只会索引匹配字段c,条件会被保留,但记录("b3", "c3")将会被裁剪掉。索引的ranges将被展开为:

a1, b1, c1
a2, b2, c1
a1, b1, c2
a2, b2, c2

相关测试用例,可以用explain format='plan' select ... 进行分析:

CREATE TABLE `testin` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `a` varchar(1024) NOT NULL ,
  `b` varchar(1024) NOT NULL ,
  `c` varchar(1024) NOT NULL ,
  `d` varchar(1024) NOT NULL ,
  `e` varchar(1024) NOT NULL ,
  `f` varchar(1024) NOT NULL ,
  PRIMARY KEY (`id`),
  KEY idx_abcdef(`a`,`b`,`c`,`d`,`e`,`f`)
);
system sleep 30;

INSERT INTO testin VALUES(NULL, "a1", "b1", "c1", "d1", "e1", "f1");
INSERT INTO testin VALUES(NULL, "a2", "b2", "c2", "d2", "e2", "f2");
INSERT INTO testin VALUES(NULL, "a3", "b3", "c3", "d3", "e3", "f3");
INSERT INTO testin VALUES(NULL, "a4", "b4", "c4", "d4", "e4", "f4");
INSERT INTO testin VALUES(NULL, "a5", "b5", "c5", "d5", "e5", "f5");
INSERT INTO testin VALUES(NULL, "a6", "b6", "c6", "d6", "e6", "f6");

# 单in组合
select id from testin where a IN ("a1" , "a2") and b IN ("b1", "b2", "b3");
select id from testin where a IN ("a1" , "a2") and b IN ("b1", "b2", "b3")  and c IN ("c1", "c2") ;

# 单in与row_expr组合
select id from testin where  a in ("a1", "a2") and (b , c) in (("b1", "c1"), ("b2","c2"));

# row_expr组合
select id from testin where  (a , b) in (("a1", "b1"), ("a2","b2")) and (c , d) in (("c1", "d1"), ("c3","d3"));

# 字段有重叠
select id from testin where  a in ("a1", "a2") and (a , b) in (("a1", "b1"), ("a3","b3"));
select id from testin where  (a, b) IN (("a1", "b1")) and (b, c) IN (("b1","c1"));
select id from testin where  (a, b) IN (("a1", "b1")) and (b, c) IN (("b1","c2"));
select id from testin where  (a , b) in (("a1", "b1"), ("a2","b2")) and (b , c) in (("b1", "c1"), ("b2","c2"));
select id from testin where  (a , b) in (("a1", "b1"), ("a2","b2")) and (b , c) in (("b2", "c1"), ("b1","c2"));
select id from testin where  (a , b) in (("a1", "b1"), ("a2","b2")) and (b , c) in (("b1", "c1"), ("b2","c2"), ("b3","c3"));

select id from testin where (a , b) in (("a1", "b1"), ("a2","b1")) and (b , c) in (("b1", "c1"), ("b2","c2"), ("b3","c3"));
select id from testin where  (a, b) in ( ("a1","b1"),("a2","b1"),("a3","b2"),("a4","b2") ) and (b, c) in ( ("b1","c1"),("b1","c2"),("b2","c3"),("b2","c4"));
select id from testin where  a in ("a1", "a2", "a3") and (a , b) in (("a1", "b1"), ("a2","b2")) and (a, c) in (("a1", "c1"), ("a3","c3"))

# 多in组合与range组合
select id from testin where (a , b) in (("a1", "b1"), ("a2","b2")) and (b , c) in (("b1", "c1"), ("b2","c2"), ("b3","c3")) and d in ("d1","d2","d3","d4") and e > "e1" and e < "e4";

# Row Expr EQ
select id from testin where (a , b) = ("a1", "b1") and (b, a) = ("b1", "a2");
select id from testin where (a,b) = ("a1","b1") and (b,a) = ("b1","a1");
select id from testin where a = "a1" and b="b1" and (a , b) = ("a1", "b1");
select id from testin where a = "a1"  and (a , b) = ("a1", "b1");

# EQ IN Range组合
select id from testin where   a ="a1" and b in ("b1", "b2") and (b,c) in (("b1","c1"),("b2","c2")) and a < "a5" and (d, e) >= ("d1","e1");

Copy link
Collaborator

@lgqss lgqss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑:(a , b) in (("a1", "b1"), ("a2","b1")) and (b , c) in (("b1", "c1"), ("b2","c2"), ("b3","c3"))

a,b 是 a1,b1, a2,b1, a3,b2, a4,b2;b,c 是b1,c1, b1,c2, b2,c3, b2,c4

src/exec/access_path.cpp Outdated Show resolved Hide resolved
src/physical_plan/index_selector.cpp Show resolved Hide resolved
src/exec/filter_node.cpp Outdated Show resolved Hide resolved
src/exec/filter_node.cpp Outdated Show resolved Hide resolved
src/exec/filter_node.cpp Outdated Show resolved Hide resolved
src/exec/filter_node.cpp Outdated Show resolved Hide resolved
src/exec/filter_node.cpp Show resolved Hide resolved
src/exec/filter_node.cpp Show resolved Hide resolved
src/exec/access_path.cpp Show resolved Hide resolved
src/exec/access_path.cpp Outdated Show resolved Hide resolved
src/exec/access_path.cpp Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants