提交 c90da788 authored 作者: Thomas Mueller's avatar Thomas Mueller

LIKE didn't use an index unless the right hand side was a literal or parameter.…

LIKE didn't use an index unless the right hand side was a literal or parameter. Now an index is used for LIKE CONCAT(?, '%').
上级 9f13c572
...@@ -157,14 +157,14 @@ public class CompareLike extends Condition { ...@@ -157,14 +157,14 @@ public class CompareLike extends Condition {
return; return;
} }
// parameters are always evaluatable, but // parameters are always evaluatable, but
// we need to check the actual value now // we need to check if the value is set
// (at prepare time) // (at prepare time)
// otherwise we would need to prepare at execute time, // otherwise we would need to prepare at execute time,
// which is maybe slower (but maybe not in this case!) // which may be slower (possibly not in this case)
if (!right.isValueSet()) { if (!right.isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
return; return;
} }
if (escape != null && !escape.isValueSet()) { if (escape != null && !escape.isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
return; return;
} }
String p = right.getValue(session).getString(); String p = right.getValue(session).getString();
......
...@@ -38,6 +38,7 @@ public class TestOptimizations extends TestBase { ...@@ -38,6 +38,7 @@ public class TestOptimizations extends TestBase {
public void test() throws Exception { public void test() throws Exception {
deleteDb("optimizations"); deleteDb("optimizations");
testLike();
testExistsSubquery(); testExistsSubquery();
testQueryCacheConcurrentUse(); testQueryCacheConcurrentUse();
testQueryCacheResetParams(); testQueryCacheResetParams();
...@@ -67,6 +68,18 @@ public class TestOptimizations extends TestBase { ...@@ -67,6 +68,18 @@ public class TestOptimizations extends TestBase {
deleteDb("optimizations"); deleteDb("optimizations");
} }
private void testLike() throws Exception {
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("create table test(name varchar primary key) as select x from system_range(1, 10)");
ResultSet rs = stat.executeQuery("explain select * from test where name like ? || '%' {1: 'Hello'}");
rs.next();
// ensure the ID = 10 part is evaluated first
assertContains(rs.getString(1), "PRIMARY_KEY_");
stat.execute("drop table test");
conn.close();
}
private void testExistsSubquery() throws Exception { private void testExistsSubquery() throws Exception {
Connection conn = getConnection("optimizations"); Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论