Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b694ef8f
提交
b694ef8f
authored
10月 11, 2015
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #183 from svladykin/optimizerHints
Optimizer hints
上级
89537665
6c267c03
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
157 行增加
和
1 行删除
+157
-1
Optimizer.java
h2/src/main/org/h2/command/dml/Optimizer.java
+9
-1
OptimizerHints.java
h2/src/main/org/h2/command/dml/OptimizerHints.java
+51
-0
TestOptimizerHints.java
h2/src/test/org/h2/test/db/TestOptimizerHints.java
+97
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Optimizer.java
浏览文件 @
b694ef8f
...
@@ -53,6 +53,14 @@ class Optimizer {
...
@@ -53,6 +53,14 @@ class Optimizer {
this
.
session
=
session
;
this
.
session
=
session
;
}
}
/**
* @return {@code true} If join reordering is enabled (it can be disabled by hint).
*/
private
static
boolean
isJoinReorderingEnabled
()
{
OptimizerHints
hints
=
OptimizerHints
.
get
();
return
hints
==
null
||
hints
.
joinReorderEnabled
;
}
/**
/**
* How many filter to calculate using brute force. The remaining filters are
* How many filter to calculate using brute force. The remaining filters are
* selected using a greedy algorithm which has a runtime of (1 + 2 + ... +
* selected using a greedy algorithm which has a runtime of (1 + 2 + ... +
...
@@ -76,7 +84,7 @@ class Optimizer {
...
@@ -76,7 +84,7 @@ class Optimizer {
private
void
calculateBestPlan
()
{
private
void
calculateBestPlan
()
{
start
=
System
.
currentTimeMillis
();
start
=
System
.
currentTimeMillis
();
cost
=
-
1
;
cost
=
-
1
;
if
(
filters
.
length
==
1
)
{
if
(
filters
.
length
==
1
||
!
isJoinReorderingEnabled
()
)
{
testPlan
(
filters
);
testPlan
(
filters
);
}
else
if
(
filters
.
length
<=
MAX_BRUTE_FORCE_FILTERS
)
{
}
else
if
(
filters
.
length
<=
MAX_BRUTE_FORCE_FILTERS
)
{
calculateBruteForceAll
();
calculateBruteForceAll
();
...
...
h2/src/main/org/h2/command/dml/OptimizerHints.java
0 → 100644
浏览文件 @
b694ef8f
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
command
.
dml
;
/**
* Thread local hints for H2 query optimizer. All the ongoing queries in the current thread
* will run with respect to these hints, so if they are needed only for a single
* operation it is preferable to setup and drop them in try-finally block.
*
* Currently works only in embedded mode.
*
* @author Sergi Vladykin
*/
public
class
OptimizerHints
{
private
static
final
ThreadLocal
<
OptimizerHints
>
HINTS
=
new
ThreadLocal
<
OptimizerHints
>();
boolean
joinReorderEnabled
=
true
;
/**
* Set thread local hints or {@code null} to drop any existing hints.
*
* @param hints
*/
public
static
void
set
(
OptimizerHints
hints
)
{
if
(
hints
!=
null
)
{
HINTS
.
set
(
hints
);
}
else
{
HINTS
.
remove
();
}
}
/**
* @return Current thread local hints or {@code null} if none.
*/
public
static
OptimizerHints
get
()
{
return
HINTS
.
get
();
}
/**
* Set whether reordering of tables (or anything else in the {@code FROM} clause) is enabled.
* By default is {@code true}.
*
* @param joinReorderEnabled Flag value.
*/
public
void
setJoinReorderEnabled
(
boolean
joinReorderEnabled
)
{
this
.
joinReorderEnabled
=
joinReorderEnabled
;
}
}
h2/src/test/org/h2/test/db/TestOptimizerHints.java
0 → 100644
浏览文件 @
b694ef8f
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
db
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
org.h2.command.dml.OptimizerHints
;
import
org.h2.test.TestBase
;
/**
* Test for optimizer hints.
*
* @author Sergi Vladykin
*/
public
class
TestOptimizerHints
extends
TestBase
{
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
[]
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
@Override
public
void
test
()
throws
Exception
{
if
(
config
.
networked
)
{
return
;
}
deleteDb
(
"testOptimizerHints"
);
Connection
conn
=
getConnection
(
"testOptimizerHints"
);
Statement
s
=
conn
.
createStatement
();
s
.
execute
(
"create table t1(id int)"
);
s
.
execute
(
"create table t2(id int, ref_id int)"
);
s
.
execute
(
"insert into t1 values(1),(2),(3)"
);
s
.
execute
(
"insert into t2 values(1,2),(2,3),(3,4),(4,6),(5,1),(6,4)"
);
s
.
execute
(
"create unique index idx1_id on t1(id)"
);
s
.
execute
(
"create index idx2_id on t2(id)"
);
s
.
execute
(
"create index idx2_ref_id on t2(ref_id)"
);
enableJoinReordering
(
false
);
try
{
String
plan
;
plan
=
plan
(
s
,
"select * from t1, t2 where t1.id = t2.ref_id"
);
assertTrue
(
plan
,
plan
.
contains
(
"INNER JOIN PUBLIC.T2"
));
plan
=
plan
(
s
,
"select * from t2, t1 where t1.id = t2.ref_id"
);
assertTrue
(
plan
,
plan
.
contains
(
"INNER JOIN PUBLIC.T1"
));
plan
=
plan
(
s
,
"select * from t2, t1 where t1.id = 1"
);
assertTrue
(
plan
,
plan
.
contains
(
"INNER JOIN PUBLIC.T1"
));
plan
=
plan
(
s
,
"select * from t2, t1 where t1.id = t2.ref_id and t2.id = 1"
);
assertTrue
(
plan
,
plan
.
contains
(
"INNER JOIN PUBLIC.T1"
));
plan
=
plan
(
s
,
"select * from t1, t2 where t1.id = t2.ref_id and t2.id = 1"
);
assertTrue
(
plan
,
plan
.
contains
(
"INNER JOIN PUBLIC.T2"
));
}
finally
{
enableJoinReordering
(
true
);
}
deleteDb
(
"testOptimizerHints"
);
}
/**
* @param enable Enabled.
*/
private
void
enableJoinReordering
(
boolean
enable
)
{
OptimizerHints
hints
=
new
OptimizerHints
();
hints
.
setJoinReorderEnabled
(
enable
);
OptimizerHints
.
set
(
hints
);
}
/**
* @param s Statement.
* @param query Query.
* @return Plan.
* @throws SQLException If failed.
*/
private
String
plan
(
Statement
s
,
String
query
)
throws
SQLException
{
ResultSet
rs
=
s
.
executeQuery
(
"explain "
+
query
);
assertTrue
(
rs
.
next
());
String
plan
=
rs
.
getString
(
1
);
rs
.
close
();
return
plan
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论