Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
e5e2eea1
提交
e5e2eea1
authored
11月 20, 2012
作者:
noelgrandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimize IN(...) queries where the values are constant and of the same type
上级
ea56c852
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
160 行增加
和
1 行删除
+160
-1
ConditionIn.java
h2/src/main/org/h2/expression/ConditionIn.java
+11
-1
ConditionInConstantSet.java
h2/src/main/org/h2/expression/ConditionInConstantSet.java
+133
-0
TestOptimizations.java
h2/src/test/org/h2/test/db/TestOptimizations.java
+16
-0
没有找到文件。
h2/src/main/org/h2/expression/ConditionIn.java
浏览文件 @
e5e2eea1
...
...
@@ -80,7 +80,9 @@ public class ConditionIn extends Condition {
return
left
;
}
boolean
allValuesConstant
=
true
;
boolean
allValuesSameType
=
true
;
int
size
=
valueList
.
size
();
Expression
lastExpr
=
null
;
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Expression
e
=
valueList
.
get
(
i
);
e
=
e
.
optimize
(
session
);
...
...
@@ -88,6 +90,10 @@ public class ConditionIn extends Condition {
allValuesConstant
=
false
;
}
valueList
.
set
(
i
,
e
);
if
(
lastExpr
!=
null
&&
lastExpr
.
getType
()
!=
e
.
getType
())
{
allValuesSameType
=
false
;
}
lastExpr
=
e
;
}
if
(
constant
&&
allValuesConstant
)
{
return
ValueExpression
.
get
(
getValue
(
session
));
...
...
@@ -98,6 +104,11 @@ public class ConditionIn extends Condition {
expr
=
expr
.
optimize
(
session
);
return
expr
;
}
if
(
allValuesConstant
&&
allValuesSameType
)
{
Expression
expr
=
new
ConditionInConstantSet
(
session
,
left
,
valueList
);
expr
=
expr
.
optimize
(
session
);
return
expr
;
}
return
this
;
}
...
...
@@ -185,5 +196,4 @@ public class ConditionIn extends Condition {
}
return
null
;
}
}
h2/src/main/org/h2/expression/ConditionInConstantSet.java
0 → 100644
浏览文件 @
e5e2eea1
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
expression
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
org.h2.engine.Session
;
import
org.h2.index.IndexCondition
;
import
org.h2.message.DbException
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.TableFilter
;
import
org.h2.util.StatementBuilder
;
import
org.h2.value.Value
;
import
org.h2.value.ValueBoolean
;
import
org.h2.value.ValueNull
;
/**
* Used for optimised IN(...) queries where the contents of the IN list are all constant and of the same type.
* <p>
* Checking using a HashSet is has time complexity O(1), instead of O(n) for checking using
* an array.
*/
public
class
ConditionInConstantSet
extends
Condition
{
private
Expression
left
;
private
int
queryLevel
;
private
final
ArrayList
<
Expression
>
valueList
;
private
final
HashSet
<
Value
>
valueSet
;
/**
* Create a new IN(..) condition.
*
* @param left the expression before IN
* @param valueList the value list (at least two elements)
*/
public
ConditionInConstantSet
(
Session
session
,
Expression
left
,
ArrayList
<
Expression
>
valueList
)
{
this
.
left
=
left
;
this
.
valueList
=
valueList
;
this
.
valueSet
=
new
HashSet
<
Value
>(
valueList
.
size
());
for
(
Expression
expression
:
valueList
)
{
this
.
valueSet
.
add
(
expression
.
getValue
(
session
));
}
}
public
Value
getValue
(
Session
session
)
{
Value
leftVal
=
left
.
getValue
(
session
);
if
(
leftVal
==
ValueNull
.
INSTANCE
)
{
return
leftVal
;
}
boolean
setHasNull
=
valueSet
.
contains
(
ValueNull
.
INSTANCE
);
Value
firstRightVal
=
valueSet
.
iterator
().
next
();
leftVal
=
leftVal
.
convertTo
(
firstRightVal
.
getType
());
boolean
result
=
valueSet
.
contains
(
leftVal
);
if
(!
result
&&
setHasNull
)
{
return
ValueNull
.
INSTANCE
;
}
return
ValueBoolean
.
get
(
result
);
}
public
void
mapColumns
(
ColumnResolver
resolver
,
int
level
)
{
left
.
mapColumns
(
resolver
,
level
);
this
.
queryLevel
=
Math
.
max
(
level
,
this
.
queryLevel
);
}
public
Expression
optimize
(
Session
session
)
{
left
=
left
.
optimize
(
session
);
return
this
;
}
public
void
createIndexConditions
(
Session
session
,
TableFilter
filter
)
{
if
(!(
left
instanceof
ExpressionColumn
))
{
return
;
}
ExpressionColumn
l
=
(
ExpressionColumn
)
left
;
if
(
filter
!=
l
.
getTableFilter
())
{
return
;
}
if
(
session
.
getDatabase
().
getSettings
().
optimizeInList
)
{
filter
.
addIndexCondition
(
IndexCondition
.
getInList
(
l
,
valueList
));
return
;
}
}
public
void
setEvaluatable
(
TableFilter
tableFilter
,
boolean
b
)
{
left
.
setEvaluatable
(
tableFilter
,
b
);
}
public
String
getSQL
()
{
StatementBuilder
buff
=
new
StatementBuilder
(
"("
);
buff
.
append
(
left
.
getSQL
()).
append
(
" IN("
);
for
(
Expression
e
:
valueList
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
e
.
getSQL
());
}
return
buff
.
append
(
"))"
).
toString
();
}
public
void
updateAggregate
(
Session
session
)
{
// nothing to do
}
public
boolean
isEverything
(
ExpressionVisitor
visitor
)
{
if
(!
left
.
isEverything
(
visitor
))
{
return
false
;
}
switch
(
visitor
.
getType
())
{
case
ExpressionVisitor
.
OPTIMIZABLE_MIN_MAX_COUNT_ALL
:
case
ExpressionVisitor
.
DETERMINISTIC
:
case
ExpressionVisitor
.
READONLY
:
case
ExpressionVisitor
.
INDEPENDENT
:
case
ExpressionVisitor
.
EVALUATABLE
:
case
ExpressionVisitor
.
SET_MAX_DATA_MODIFICATION_ID
:
case
ExpressionVisitor
.
NOT_FROM_RESOLVER
:
case
ExpressionVisitor
.
GET_DEPENDENCIES
:
case
ExpressionVisitor
.
QUERY_COMPARABLE
:
case
ExpressionVisitor
.
GET_COLUMNS
:
return
true
;
default
:
throw
DbException
.
throwInternalError
(
"type="
+
visitor
.
getType
());
}
}
public
int
getCost
()
{
int
cost
=
left
.
getCost
();
return
cost
;
}
}
h2/src/test/org/h2/test/db/TestOptimizations.java
浏览文件 @
e5e2eea1
...
...
@@ -49,6 +49,7 @@ public class TestOptimizations extends TestBase {
testAutoAnalyze
();
testInAndBetween
();
testNestedIn
();
testConstantIn
();
testNestedInSelectAndLike
();
testNestedInSelect
();
testInSelectJoin
();
...
...
@@ -306,6 +307,21 @@ public class TestOptimizations extends TestBase {
conn
.
close
();
}
private
void
testConstantIn
()
throws
SQLException
{
deleteDb
(
"optimizations"
);
Connection
conn
=
getConnection
(
"optimizations"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"create table test(id int primary key, name varchar(255))"
);
stat
.
execute
(
"insert into test values(1, 'Hello'), (2, 'World')"
);
assertSingleValue
(
stat
,
"select count(*) from test where name in ('Hello', 'World', 1)"
,
2
);
assertSingleValue
(
stat
,
"select count(*) from test where name in ('Hello', 'World')"
,
2
);
assertSingleValue
(
stat
,
"select count(*) from test where name in ('Hello', 'Not')"
,
1
);
stat
.
execute
(
"drop table test"
);
conn
.
close
();
}
private
void
testNestedInSelect
()
throws
SQLException
{
deleteDb
(
"optimizations"
);
Connection
conn
=
getConnection
(
"optimizations"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论