Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
14664665
提交
14664665
authored
1月 30, 2009
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimizer: the expected runtime calculation was incorrect.
上级
c77c145a
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
57 行增加
和
49 行删除
+57
-49
changelog.html
h2/src/docsrc/html/changelog.html
+3
-1
Optimizer.java
h2/src/main/org/h2/command/dml/Optimizer.java
+16
-13
MathUtils.java
h2/src/main/org/h2/util/MathUtils.java
+0
-26
TestMathUtils.java
h2/src/test/org/h2/test/unit/TestMathUtils.java
+38
-9
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
14664665
...
@@ -18,7 +18,9 @@ Change Log
...
@@ -18,7 +18,9 @@ Change Log
<h1>
Change Log
</h1>
<h1>
Change Log
</h1>
<h2>
Next Version (unreleased)
</h2>
<h2>
Next Version (unreleased)
</h2>
<ul><li>
Improved exception message when connecting to a just started server fails.
<ul><li>
Optimizer: the expected runtime calculation was incorrect. The fixed calculation
should give slightly better query plans when using many joins.
</li><li>
Improved exception message when connecting to a just started server fails.
</li><li>
Connection.isValid is a bit faster.
</li><li>
Connection.isValid is a bit faster.
</li><li>
H2 Console: The autocomplete feature has been improved a bit. It can now better
</li><li>
H2 Console: The autocomplete feature has been improved a bit. It can now better
parse conditions.
parse conditions.
...
...
h2/src/main/org/h2/command/dml/Optimizer.java
浏览文件 @
14664665
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
*/
*/
package
org
.
h2
.
command
.
dml
;
package
org
.
h2
.
command
.
dml
;
import
java.math.BigInteger
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.BitSet
;
import
java.util.BitSet
;
import
java.util.Random
;
import
java.util.Random
;
...
@@ -16,7 +15,6 @@ import org.h2.expression.Expression;
...
@@ -16,7 +15,6 @@ import org.h2.expression.Expression;
import
org.h2.table.Plan
;
import
org.h2.table.Plan
;
import
org.h2.table.PlanItem
;
import
org.h2.table.PlanItem
;
import
org.h2.table.TableFilter
;
import
org.h2.table.TableFilter
;
import
org.h2.util.MathUtils
;
import
org.h2.util.ObjectUtils
;
import
org.h2.util.ObjectUtils
;
import
org.h2.util.Permutations
;
import
org.h2.util.Permutations
;
...
@@ -27,12 +25,12 @@ import org.h2.util.Permutations;
...
@@ -27,12 +25,12 @@ import org.h2.util.Permutations;
public
class
Optimizer
{
public
class
Optimizer
{
private
static
final
int
MAX_BRUTE_FORCE_FILTERS
=
7
;
private
static
final
int
MAX_BRUTE_FORCE_FILTERS
=
7
;
private
static
final
BigInteger
MAX_BRUTE_FORCE
=
new
BigInteger
(
""
+
2000
)
;
private
static
final
int
MAX_BRUTE_FORCE
=
2000
;
private
static
final
int
MAX_GENETIC
=
500
;
private
static
final
int
MAX_GENETIC
=
500
;
private
long
start
;
private
long
start
;
private
BitSet
switched
;
private
BitSet
switched
;
// possible plans for filters:
// possible plans for filters
, if using brute force
:
// 1 filter 1 plan
// 1 filter 1 plan
// 2 filters 2 plans
// 2 filters 2 plans
// 3 filters 6 plans
// 3 filters 6 plans
...
@@ -43,10 +41,6 @@ public class Optimizer {
...
@@ -43,10 +41,6 @@ public class Optimizer {
// 8 filters 40320 plan
// 8 filters 40320 plan
// 9 filters 362880 plans
// 9 filters 362880 plans
// 10 filters 3628800 filters
// 10 filters 3628800 filters
// 1 of 1, 2, 3, 4, 5, 6 filters: 1, 2, 3, 4, 5, 6
// 2 of 2, 3, 4, 5, 6 filters: 2, 6, 12, 20, 30
// 3 of 3, 4, 5, 6 filters: 6, 24, 75, 120
// 4 of 4, 5, 6 filters: 24, 120, 260
private
TableFilter
[]
filters
;
private
TableFilter
[]
filters
;
private
Expression
condition
;
private
Expression
condition
;
...
@@ -63,12 +57,21 @@ public class Optimizer {
...
@@ -63,12 +57,21 @@ public class Optimizer {
this
.
session
=
session
;
this
.
session
=
session
;
}
}
private
int
getMaxBruteForceFilters
(
int
filterCount
)
{
/**
int
i
=
0
,
j
=
filterCount
;
* How many filter to calculate using brute force. The remaining filters are
BigInteger
total
=
new
BigInteger
(
""
+
filterCount
);
* selected using a greedy algorithm which has a runtime of (1 + 2 + ... +
while
(
j
>
0
&&
total
.
compareTo
(
MAX_BRUTE_FORCE
)
<
0
)
{
* n) = (n * (n-1) / 2) for n filters. The brute force algorithm has a
* runtime of n * (n-1) * ... * (n-m) when calculating m brute force of n
* total. The combined runtime is (brute force) * (greedy).
*
* @param filterCount the number of filters total
* @return the number of filters to calculate using brute force
*/
private
static
int
getMaxBruteForceFilters
(
int
filterCount
)
{
int
i
=
0
,
j
=
filterCount
,
total
=
filterCount
;
while
(
j
>
0
&&
total
*
(
j
*
(
j
-
1
)
/
2
)
<
MAX_BRUTE_FORCE
)
{
j
--;
j
--;
total
=
total
.
multiply
(
MathUtils
.
factorial
(
j
))
;
total
*=
j
;
i
++;
i
++;
}
}
return
i
;
return
i
;
...
...
h2/src/main/org/h2/util/MathUtils.java
浏览文件 @
14664665
...
@@ -7,7 +7,6 @@
...
@@ -7,7 +7,6 @@
package
org
.
h2
.
util
;
package
org
.
h2
.
util
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
org.h2.engine.Constants
;
import
org.h2.engine.Constants
;
...
@@ -179,29 +178,4 @@ public class MathUtils {
...
@@ -179,29 +178,4 @@ public class MathUtils {
}
}
}
}
/**
* Calculate the factorial (n!) of a number.
* This implementation uses a naive multiplication loop, and
* is very slow for large n.
* For n = 1000, it takes about 10 ms.
* For n = 8000, it takes about 800 ms.
*
* @param n the number
* @return the factorial of n
*/
public
static
BigInteger
factorial
(
int
n
)
{
if
(
n
<
0
)
{
throw
new
IllegalArgumentException
(
n
+
"<0"
);
}
else
if
(
n
<
2
)
{
return
BigInteger
.
ONE
;
}
BigInteger
x
=
new
BigInteger
(
""
+
n
);
BigInteger
result
=
x
;
for
(
int
i
=
n
-
1
;
i
>=
2
;
i
--)
{
x
=
x
.
subtract
(
BigInteger
.
ONE
);
result
=
result
.
multiply
(
x
);
}
return
result
;
}
}
}
h2/src/test/org/h2/test/unit/TestMathUtils.java
浏览文件 @
14664665
...
@@ -6,10 +6,9 @@
...
@@ -6,10 +6,9 @@
*/
*/
package
org
.
h2
.
test
.
unit
;
package
org
.
h2
.
test
.
unit
;
import
java.math.BigInteger
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
org.h2.test.TestBase
;
import
org.h2.test.TestBase
;
import
org.h2.util.MathUtils
;
/**
/**
* Tests math utility methods.
* Tests math utility methods.
...
@@ -26,17 +25,47 @@ public class TestMathUtils extends TestBase {
...
@@ -26,17 +25,47 @@ public class TestMathUtils extends TestBase {
}
}
public
void
test
()
throws
SQLException
{
public
void
test
()
throws
SQLException
{
testFactorial
();
}
private
void
testFactorial
()
{
try
{
try
{
MathUtils
.
factorial
(-
1
);
factorial
(-
1
);
fail
();
fail
();
}
catch
(
IllegalArgumentException
e
)
{
}
catch
(
IllegalArgumentException
e
)
{
// ignore
// ignore
}
}
assertEquals
(
"1"
,
MathUtils
.
factorial
(
0
).
toString
());
assertEquals
(
"1"
,
factorial
(
0
).
toString
());
assertEquals
(
"1"
,
MathUtils
.
factorial
(
1
).
toString
());
assertEquals
(
"1"
,
factorial
(
1
).
toString
());
assertEquals
(
"2"
,
MathUtils
.
factorial
(
2
).
toString
());
assertEquals
(
"2"
,
factorial
(
2
).
toString
());
assertEquals
(
"6"
,
MathUtils
.
factorial
(
3
).
toString
());
assertEquals
(
"6"
,
factorial
(
3
).
toString
());
assertEquals
(
"3628800"
,
MathUtils
.
factorial
(
10
).
toString
());
assertEquals
(
"3628800"
,
factorial
(
10
).
toString
());
assertEquals
(
"2432902008176640000"
,
MathUtils
.
factorial
(
20
).
toString
());
assertEquals
(
"2432902008176640000"
,
factorial
(
20
).
toString
());
}
}
/**
* Calculate the factorial (n!) of a number.
* This implementation uses a naive multiplication loop, and
* is very slow for large n.
* For n = 1000, it takes about 10 ms.
* For n = 8000, it takes about 800 ms.
*
* @param n the number
* @return the factorial of n
*/
public
static
BigInteger
factorial
(
int
n
)
{
if
(
n
<
0
)
{
throw
new
IllegalArgumentException
(
n
+
"<0"
);
}
else
if
(
n
<
2
)
{
return
BigInteger
.
ONE
;
}
BigInteger
x
=
new
BigInteger
(
""
+
n
);
BigInteger
result
=
x
;
for
(
int
i
=
n
-
1
;
i
>=
2
;
i
--)
{
x
=
x
.
subtract
(
BigInteger
.
ONE
);
result
=
result
.
multiply
(
x
);
}
return
result
;
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论