Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
8aaaec64
提交
8aaaec64
authored
8月 18, 2009
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimization for OR: (X=1 OR X=2) is converted to (X IN(1, 2)).
上级
d7856843
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
125 行增加
和
23 行删除
+125
-23
changelog.html
h2/src/docsrc/html/changelog.html
+3
-0
SysProperties.java
h2/src/main/org/h2/constant/SysProperties.java
+12
-6
Comparison.java
h2/src/main/org/h2/expression/Comparison.java
+48
-11
ConditionAndOr.java
h2/src/main/org/h2/expression/ConditionAndOr.java
+24
-3
ConditionIn.java
h2/src/main/org/h2/expression/ConditionIn.java
+17
-0
ObjectArray.java
h2/src/main/org/h2/util/ObjectArray.java
+14
-1
TestAll.java
h2/src/test/org/h2/test/TestAll.java
+7
-2
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
8aaaec64
...
...
@@ -27,6 +27,9 @@ Change Log
start a transaction, insert many rows, delete many rows, rollback. The number of rows depends
on the cache size.
</li><li>
The stack trace of very common exceptions is no longer written to the .trace.db file by default.
</li><li>
An optimization for OR is implemented, but disabled by default.
Expressions of the type X=1 OR X=2 are converted to X IN(1, 2).
To enable, set the system property h2.optimizeInList to true before loading the H2 JDBC driver.
</li><li>
An optimization for IN(..) and IN(SELECT...) is implemented, but disabled by default.
To enable, set the system property h2.optimizeInList to true before loading the H2 JDBC driver.
If enabled, this overrides h2.optimizeIn and h2.optimizeInJoin. Unlike now, this optimization
...
...
h2/src/main/org/h2/constant/SysProperties.java
浏览文件 @
8aaaec64
...
...
@@ -462,18 +462,24 @@ public class SysProperties {
*/
public
static
final
boolean
OPTIMIZE_MIN_MAX
=
getBooleanSetting
(
"h2.optimizeMinMax"
,
true
);
/**
* System property <code>h2.optimizeSubqueryCache</code> (default: true).<br />
* Cache subquery results.
*/
public
static
final
boolean
OPTIMIZE_SUBQUERY_CACHE
=
getBooleanSetting
(
"h2.optimizeSubqueryCache"
,
true
);
/**
* System property <code>h2.optimizeNot</code> (default: true).<br />
* Optimize NOT conditions by removing the NOT and inverting the condition.
*/
public
static
final
boolean
OPTIMIZE_NOT
=
getBooleanSetting
(
"h2.optimizeNot"
,
true
);
/**
* System property <code>h2.optimizeOr</code> (default: false).<br />
* Convert (C=? OR C=?) to (C IN(?, ?)).
*/
public
static
final
boolean
OPTIMIZE_OR
=
getBooleanSetting
(
"h2.optimizeOr"
,
false
);
/**
* System property <code>h2.optimizeSubqueryCache</code> (default: true).<br />
* Cache subquery results.
*/
public
static
final
boolean
OPTIMIZE_SUBQUERY_CACHE
=
getBooleanSetting
(
"h2.optimizeSubqueryCache"
,
true
);
/**
* System property <code>h2.optimizeTwoEquals</code> (default: true).<br />
* Optimize expressions of the form A=B AND B=1. In this case, AND A=1 is
...
...
h2/src/main/org/h2/expression/Comparison.java
浏览文件 @
8aaaec64
...
...
@@ -16,6 +16,7 @@ import org.h2.index.IndexCondition;
import
org.h2.message.Message
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.TableFilter
;
import
org.h2.util.ObjectArray
;
import
org.h2.value.Value
;
import
org.h2.value.ValueBoolean
;
import
org.h2.value.ValueNull
;
...
...
@@ -427,15 +428,36 @@ public class Comparison extends Condition {
return
left
.
getCost
()
+
(
right
==
null
?
0
:
right
.
getCost
())
+
1
;
}
/**
* Get the other expression if this is an equals comparison and the other
* expression matches.
*
* @param match the expression that should match
* @return null if no match, the other expression if there is a match
*/
Expression
getIfEquals
(
Expression
match
)
{
if
(
compareType
==
EQUAL
)
{
String
sql
=
match
.
getSQL
();
if
(
left
.
getSQL
().
equals
(
sql
))
{
return
right
;
}
else
if
(
right
.
getSQL
().
equals
(
sql
))
{
return
left
;
}
}
return
null
;
}
/**
* Get an additional condition if possible. Example: given two conditions
* A=B and B=C, the new condition A=C is returned.
* A=B AND B=C, the new condition A=C is returned. Given the two conditions
* A=1 OR A=2, the new condition A IN(1, 2) is returned.
*
* @param session the session
* @param other the second condition
* @param add true for AND, false for OR
* @return null or the third condition
*/
Comparison
getAdditional
(
Session
session
,
Comparison
other
)
{
Expression
getAdditional
(
Session
session
,
Comparison
other
,
boolean
and
)
{
if
(
compareType
==
other
.
compareType
&&
compareType
==
EQUAL
)
{
boolean
lc
=
left
.
isConstant
(),
rc
=
right
.
isConstant
();
boolean
l2c
=
other
.
left
.
isConstant
(),
r2c
=
other
.
right
.
isConstant
();
...
...
@@ -443,15 +465,30 @@ public class Comparison extends Condition {
String
l2
=
other
.
left
.
getSQL
();
String
r
=
right
.
getSQL
();
String
r2
=
other
.
right
.
getSQL
();
// must not compare constants. example: NOT(B=2 AND B=3)
if
(!(
rc
&&
r2c
)
&&
l
.
equals
(
l2
))
{
return
new
Comparison
(
session
,
EQUAL
,
right
,
other
.
right
);
}
else
if
(!(
rc
&&
l2c
)
&&
l
.
equals
(
r2
))
{
return
new
Comparison
(
session
,
EQUAL
,
right
,
other
.
left
);
}
else
if
(!(
lc
&&
r2c
)
&&
r
.
equals
(
l2
))
{
return
new
Comparison
(
session
,
EQUAL
,
left
,
other
.
right
);
}
else
if
(!(
lc
&&
l2c
)
&&
r
.
equals
(
r2
))
{
return
new
Comparison
(
session
,
EQUAL
,
left
,
other
.
left
);
if
(
and
)
{
// a=b AND a=c
// must not compare constants. example: NOT(B=2 AND B=3)
if
(!(
rc
&&
r2c
)
&&
l
.
equals
(
l2
))
{
return
new
Comparison
(
session
,
EQUAL
,
right
,
other
.
right
);
}
else
if
(!(
rc
&&
l2c
)
&&
l
.
equals
(
r2
))
{
return
new
Comparison
(
session
,
EQUAL
,
right
,
other
.
left
);
}
else
if
(!(
lc
&&
r2c
)
&&
r
.
equals
(
l2
))
{
return
new
Comparison
(
session
,
EQUAL
,
left
,
other
.
right
);
}
else
if
(!(
lc
&&
l2c
)
&&
r
.
equals
(
r2
))
{
return
new
Comparison
(
session
,
EQUAL
,
left
,
other
.
left
);
}
}
else
{
// a=b OR a=c
Database
db
=
session
.
getDatabase
();
if
(
rc
&&
r2c
&&
l
.
equals
(
l2
))
{
return
new
ConditionIn
(
db
,
left
,
ObjectArray
.
newInstance
(
right
,
other
.
right
));
}
else
if
(
rc
&&
l2c
&&
l
.
equals
(
r2
))
{
return
new
ConditionIn
(
db
,
left
,
ObjectArray
.
newInstance
(
right
,
other
.
left
));
}
else
if
(
lc
&&
r2c
&&
r
.
equals
(
l2
))
{
return
new
ConditionIn
(
db
,
right
,
ObjectArray
.
newInstance
(
left
,
other
.
right
));
}
else
if
(
lc
&&
l2c
&&
r
.
equals
(
r2
))
{
return
new
ConditionIn
(
db
,
right
,
ObjectArray
.
newInstance
(
left
,
other
.
left
));
}
}
}
return
null
;
...
...
h2/src/main/org/h2/expression/ConditionAndOr.java
浏览文件 @
8aaaec64
...
...
@@ -126,7 +126,7 @@ public class ConditionAndOr extends Condition {
public
Expression
optimize
(
Session
session
)
throws
SQLException
{
// TODO NULL: see wikipedia,
// http://www-cs-students.stanford.edu/~wlam/compsci/sqlnulls
// TODO test
if all optimizations are
switched off against all on
// TODO test
all optimizations
switched off against all on
// (including performance)
left
=
left
.
optimize
(
session
);
right
=
right
.
optimize
(
session
);
...
...
@@ -142,12 +142,12 @@ public class ConditionAndOr extends Condition {
// INSERT INTO TEST VALUES(1, NULL);
// SELECT * FROM TEST WHERE NOT (B=A AND B=0); // no rows
// SELECT * FROM TEST WHERE NOT (B=A AND B=0 AND A=0); // 1, NULL
if
(
SysProperties
.
OPTIMIZE_
NOT
&&
SysProperties
.
OPTIMIZE_TWO_EQUALS
&&
andOrType
==
AND
)
{
if
(
SysProperties
.
OPTIMIZE_
TWO_EQUALS
&&
SysProperties
.
OPTIMIZE_NOT
&&
andOrType
==
AND
)
{
// try to add conditions (A=B AND B=1: add A=1)
if
(
left
instanceof
Comparison
&&
right
instanceof
Comparison
)
{
Comparison
compLeft
=
(
Comparison
)
left
;
Comparison
compRight
=
(
Comparison
)
right
;
Expression
added
=
compLeft
.
getAdditional
(
session
,
compRight
);
Expression
added
=
compLeft
.
getAdditional
(
session
,
compRight
,
true
);
if
(
added
!=
null
)
{
added
=
added
.
optimize
(
session
);
ConditionAndOr
a
=
new
ConditionAndOr
(
AND
,
this
,
added
);
...
...
@@ -157,6 +157,27 @@ public class ConditionAndOr extends Condition {
}
// TODO optimization: convert ((A=1 AND B=2) OR (A=1 AND B=3)) to
// (A=1 AND (B=2 OR B=3))
if
(
SysProperties
.
OPTIMIZE_OR
&&
andOrType
==
OR
)
{
// try to add conditions (A=B AND B=1: add A=1)
if
(
left
instanceof
Comparison
&&
right
instanceof
Comparison
)
{
Comparison
compLeft
=
(
Comparison
)
left
;
Comparison
compRight
=
(
Comparison
)
right
;
Expression
added
=
compLeft
.
getAdditional
(
session
,
compRight
,
false
);
if
(
added
!=
null
)
{
return
added
.
optimize
(
session
);
}
}
else
if
(
left
instanceof
ConditionIn
&&
right
instanceof
Comparison
)
{
Expression
added
=
((
ConditionIn
)
left
).
getAdditional
(
session
,
(
Comparison
)
right
);
if
(
added
!=
null
)
{
return
added
.
optimize
(
session
);
}
}
else
if
(
right
instanceof
ConditionIn
&&
left
instanceof
Comparison
)
{
Expression
added
=
((
ConditionIn
)
right
).
getAdditional
(
session
,
(
Comparison
)
left
);
if
(
added
!=
null
)
{
return
added
.
optimize
(
session
);
}
}
}
// TODO optimization: convert .. OR .. to UNION if the cost is lower
Value
l
=
left
.
isConstant
()
?
left
.
getValue
(
session
)
:
null
;
Value
r
=
right
.
isConstant
()
?
right
.
getValue
(
session
)
:
null
;
...
...
h2/src/main/org/h2/expression/ConditionIn.java
浏览文件 @
8aaaec64
...
...
@@ -258,4 +258,21 @@ public class ConditionIn extends Condition {
return
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
this
,
on
);
}
/**
* Add an additional element if possible. Example: given two conditions
* A IN(1, 2) OR A=3, the constant 3 is added: A IN(1, 2, 3).
*
* @param session the session
* @param other the second condition
* @return null if the condition was not added, or the new condition
*/
public
Expression
getAdditional
(
Session
session
,
Comparison
other
)
{
Expression
add
=
other
.
getIfEquals
(
left
);
if
(
add
!=
null
)
{
valueList
.
add
(
add
);
return
this
;
}
return
null
;
}
}
h2/src/main/org/h2/util/ObjectArray.java
浏览文件 @
8aaaec64
...
...
@@ -9,7 +9,6 @@ package org.h2.util;
import
java.util.Collection
;
import
java.util.Comparator
;
import
java.util.Iterator
;
import
org.h2.constant.SysProperties
;
/**
...
...
@@ -47,6 +46,20 @@ public class ObjectArray<T> implements Iterable<T> {
return
new
ObjectArray
<
T
>(
CAPACITY_INIT
);
}
/**
* Create a new object with the given values.
*
* @param list the initial elements
* @return the object
*/
public
static
<
T
>
ObjectArray
<
T
>
newInstance
(
T
...
list
)
{
ObjectArray
<
T
>
t
=
new
ObjectArray
<
T
>(
CAPACITY_INIT
);
for
(
T
x
:
list
)
{
t
.
add
(
x
);
}
return
t
;
}
/**
* Create a new object with the default initial capacity.
*
...
...
h2/src/test/org/h2/test/TestAll.java
浏览文件 @
8aaaec64
...
...
@@ -295,9 +295,14 @@ java org.h2.test.TestAll timer
/*
improve LIKE performance
System.setProperty("h2.optimizeInList", "true");
System.setProperty("h2.optimizeOr", "true");
optimization for X IN(..) and OR:
add more test cases, code coverage 100%
document in optimizations.sql
out of memory in tests: analyze heap dump
-------------
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论