Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
aab00fa3
提交
aab00fa3
authored
9月 09, 2013
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Formatting / javadocs
上级
ccf8ee20
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
189 行增加
和
141 行删除
+189
-141
SetTypes.java
h2/src/main/org/h2/command/dml/SetTypes.java
+1
-2
Database.java
h2/src/main/org/h2/engine/Database.java
+2
-2
QueryStatisticsData.java
h2/src/main/org/h2/engine/QueryStatisticsData.java
+186
-137
没有找到文件。
h2/src/main/org/h2/command/dml/SetTypes.java
浏览文件 @
aab00fa3
...
@@ -218,8 +218,7 @@ public class SetTypes {
...
@@ -218,8 +218,7 @@ public class SetTypes {
* The type of a SET QUERY_STATISTICS_ACTIVE statement.
* The type of a SET QUERY_STATISTICS_ACTIVE statement.
*/
*/
public
static
final
int
QUERY_STATISTICS
=
41
;
public
static
final
int
QUERY_STATISTICS
=
41
;
private
static
final
ArrayList
<
String
>
TYPES
=
New
.
arrayList
();
private
static
final
ArrayList
<
String
>
TYPES
=
New
.
arrayList
();
private
SetTypes
()
{
private
SetTypes
()
{
...
...
h2/src/main/org/h2/engine/Database.java
浏览文件 @
aab00fa3
...
@@ -2094,7 +2094,7 @@ public class Database implements DataHandler {
...
@@ -2094,7 +2094,7 @@ public class Database implements DataHandler {
public
boolean
getQueryStatistics
()
{
public
boolean
getQueryStatistics
()
{
return
queryStatistics
;
return
queryStatistics
;
}
}
public
QueryStatisticsData
getQueryStatisticsData
()
{
public
QueryStatisticsData
getQueryStatisticsData
()
{
if
(!
queryStatistics
)
{
if
(!
queryStatistics
)
{
return
null
;
return
null
;
...
@@ -2108,7 +2108,7 @@ public class Database implements DataHandler {
...
@@ -2108,7 +2108,7 @@ public class Database implements DataHandler {
}
}
return
queryStatisticsData
;
return
queryStatisticsData
;
}
}
/**
/**
* Check if the database is currently opening. This is true until all stored
* Check if the database is currently opening. This is true until all stored
* SQL statements have been executed.
* SQL statements have been executed.
...
...
h2/src/main/org/h2/engine/QueryStatisticsData.java
浏览文件 @
aab00fa3
/*
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Initial Developer: H2 Group
*/
*/
package
org
.
h2
.
engine
;
package
org
.
h2
.
engine
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
/**
/**
* Maintains query statistics.
* Maintains query statistics.
*/
*/
public
class
QueryStatisticsData
{
public
class
QueryStatisticsData
{
private
static
final
int
MAX_QUERY_ENTRIES
=
100
;
private
static
final
int
MAX_QUERY_ENTRIES
=
100
;
private
static
final
Comparator
<
QueryEntry
>
QUERY_ENTRY_COMPARATOR
=
new
Comparator
<
QueryEntry
>()
{
private
static
final
Comparator
<
QueryEntry
>
QUERY_ENTRY_COMPARATOR
=
new
Comparator
<
QueryEntry
>()
{
@Override
@Override
public
int
compare
(
QueryEntry
o1
,
QueryEntry
o2
)
{
public
int
compare
(
QueryEntry
o1
,
QueryEntry
o2
)
{
return
(
int
)
Math
.
signum
(
o1
.
lastUpdateTime
-
o2
.
lastUpdateTime
);
return
(
int
)
Math
.
signum
(
o1
.
lastUpdateTime
-
o2
.
lastUpdateTime
);
}
}
};
};
private
final
HashMap
<
String
,
QueryEntry
>
map
=
new
HashMap
<
String
,
QueryEntry
>();
private
final
HashMap
<
String
,
QueryEntry
>
map
=
new
HashMap
<
String
,
QueryEntry
>();
public
synchronized
List
<
QueryEntry
>
getQueries
()
{
public
synchronized
List
<
QueryEntry
>
getQueries
()
{
// return a copy of the map so we don't have to worry about external synchronization
// return a copy of the map so we don't have to
ArrayList
<
QueryEntry
>
list
=
new
ArrayList
<
QueryEntry
>();
// worry about external synchronization
list
.
addAll
(
map
.
values
());
ArrayList
<
QueryEntry
>
list
=
new
ArrayList
<
QueryEntry
>();
// only return the newest 100 entries
list
.
addAll
(
map
.
values
());
Collections
.
sort
(
list
,
QUERY_ENTRY_COMPARATOR
);
// only return the newest 100 entries
return
list
.
subList
(
0
,
Math
.
min
(
list
.
size
(),
MAX_QUERY_ENTRIES
));
Collections
.
sort
(
list
,
QUERY_ENTRY_COMPARATOR
);
}
return
list
.
subList
(
0
,
Math
.
min
(
list
.
size
(),
MAX_QUERY_ENTRIES
));
}
/**
* Update query statistics.
/**
*
* Update query statistics.
* @param sqlStatement the statement being executed
*
* @param executionTime the time in milliseconds the query/update took to execute
* @param sqlStatement the statement being executed
* @param rowCount the query or update row count
* @param executionTime the time in milliseconds the query/update took to execute
*/
* @param rowCount the query or update row count
public
synchronized
void
update
(
String
sqlStatement
,
long
executionTime
,
int
rowCount
)
{
*/
QueryEntry
entry
=
map
.
get
(
sqlStatement
);
public
synchronized
void
update
(
String
sqlStatement
,
long
executionTime
,
int
rowCount
)
{
if
(
entry
==
null
)
{
QueryEntry
entry
=
map
.
get
(
sqlStatement
);
entry
=
new
QueryEntry
();
if
(
entry
==
null
)
{
entry
.
sqlStatement
=
sqlStatement
;
entry
=
new
QueryEntry
();
entry
.
executionTimeMin
=
executionTime
;
map
.
put
(
sqlStatement
,
entry
);
entry
.
executionTimeMax
=
executionTime
;
}
entry
.
rowCountMin
=
rowCount
;
entry
.
update
(
executionTime
,
rowCount
);
entry
.
rowCountMax
=
rowCount
;
entry
.
executionTimeMean
=
executionTime
;
// Age-out the oldest entries if the map gets too big.
entry
.
rowCountMean
=
rowCount
;
// Test against 1.5 x max-size so we don't do this too often
map
.
put
(
sqlStatement
,
entry
);
if
(
map
.
size
()
>
MAX_QUERY_ENTRIES
*
1.5f
)
{
}
else
{
// Sort the entries by age
entry
.
count
++;
ArrayList
<
QueryEntry
>
list
=
new
ArrayList
<
QueryEntry
>();
entry
.
executionTimeMin
=
Math
.
min
(
executionTime
,
entry
.
executionTimeMin
);
list
.
addAll
(
map
.
values
());
entry
.
executionTimeMax
=
Math
.
max
(
executionTime
,
entry
.
executionTimeMax
);
Collections
.
sort
(
list
,
QUERY_ENTRY_COMPARATOR
);
entry
.
rowCountMin
=
Math
.
min
(
rowCount
,
entry
.
rowCountMin
);
// Create a set of the oldest 1/3 of the entries
entry
.
rowCountMax
=
Math
.
max
(
rowCount
,
entry
.
rowCountMax
);
HashSet
<
QueryEntry
>
oldestSet
=
new
HashSet
<
QueryEntry
>(
list
.
subList
(
0
,
list
.
size
()
/
3
));
// Loop over the map using the set and remove
double
delta
=
rowCount
-
entry
.
rowCountMean
;
// the oldest 1/3 of the entries.
entry
.
rowCountMean
+=
delta
/
entry
.
count
;
for
(
Iterator
<
Entry
<
String
,
QueryEntry
>>
it
=
map
.
entrySet
().
iterator
();
it
.
hasNext
();)
{
entry
.
rowCountM2
+=
delta
*
(
rowCount
-
entry
.
rowCountMean
);
Entry
<
String
,
QueryEntry
>
mapEntry
=
it
.
next
();
if
(
oldestSet
.
contains
(
mapEntry
.
getValue
()))
{
delta
=
executionTime
-
entry
.
executionTimeMean
;
it
.
remove
();
entry
.
executionTimeMean
+=
delta
/
entry
.
count
;
}
entry
.
executionTimeM2
+=
delta
*
(
executionTime
-
entry
.
executionTimeMean
);
}
}
}
entry
.
executionTimeCumulative
+=
executionTime
;
}
entry
.
rowCountCumulative
+=
rowCount
;
entry
.
lastUpdateTime
=
System
.
currentTimeMillis
();
/**
* The collected statistics for one query.
// Age-out the oldest entries if the map gets too big.
*/
// Test against 1.5 x max-size so we don't do this too often
public
static
final
class
QueryEntry
{
if
(
map
.
size
()
>
MAX_QUERY_ENTRIES
*
1.5f
)
{
// Sort the entries by age
/**
ArrayList
<
QueryEntry
>
list
=
new
ArrayList
<
QueryEntry
>();
* The SQL statement.
list
.
addAll
(
map
.
values
());
*/
Collections
.
sort
(
list
,
QUERY_ENTRY_COMPARATOR
);
public
String
sqlStatement
;
// Create a set of the oldest 1/3 of the entries
HashSet
<
QueryEntry
>
oldestSet
=
new
HashSet
<
QueryEntry
>(
list
.
subList
(
0
,
list
.
size
()
/
3
));
/**
// Loop over the map using the set and remove the oldest 1/3 of the
* The number of times the statement was executed.
// entries.
*/
for
(
Iterator
<
Map
.
Entry
<
String
,
QueryEntry
>>
iter
=
map
.
entrySet
().
iterator
();
iter
.
hasNext
();)
{
public
int
count
;
Map
.
Entry
<
String
,
QueryEntry
>
mapEntry
=
iter
.
next
();
if
(
oldestSet
.
contains
(
mapEntry
.
getValue
()))
{
/**
iter
.
remove
();
* The last time the statistics for this entry were updated,
}
* in milliseconds since 1970.
}
*/
}
public
long
lastUpdateTime
;
}
/**
/**
* The minimum execution time, in milliseconds.
* The collected statistics for one query.
*/
*/
public
long
executionTimeMin
;
public
static
final
class
QueryEntry
{
/**
public
String
sqlStatement
;
* The maximum execution time, in milliseconds.
*/
public
int
count
=
1
;
public
long
executionTimeMax
;
public
long
lastUpdateTime
;
public
long
executionTimeMin
;
/**
public
long
executionTimeMax
;
* The total execution time.
public
long
executionTimeCumulative
;
*/
public
int
rowCountMin
;
public
long
executionTimeCumulative
;
public
int
rowCountMax
;
public
long
rowCountCumulative
;
/**
* The minimum number of rows.
// Using Welford's method, see also
*/
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
public
int
rowCountMin
;
// http://www.johndcook.com/standard_deviation.html
public
double
executionTimeMean
;
/**
public
double
executionTimeM2
;
* The maximum number of rows.
public
double
rowCountMean
;
*/
public
double
rowCountM2
;
public
int
rowCountMax
;
public
double
getExecutionTimeStandardDeviation
()
{
/**
// population standard deviation
* The total number of rows.
return
Math
.
sqrt
(
executionTimeM2
/
count
);
*/
}
public
long
rowCountCumulative
;
public
double
getRowCountStandardDeviation
()
{
/**
// population standard deviation
* The mean execution time.
return
Math
.
sqrt
(
rowCountM2
/
count
);
*/
}
public
double
executionTimeMean
;
}
/**
* The mean number of rows.
}
*/
public
double
rowCountMean
;
// Using Welford's method, see also
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
// http://www.johndcook.com/standard_deviation.html
private
double
executionTimeM2
;
private
double
rowCountM2
;
/**
* Update the statistics entry.
*
* @param time the execution time
* @param rows the number of rows
*/
void
update
(
long
time
,
int
rows
)
{
count
++;
executionTimeMin
=
Math
.
min
(
time
,
executionTimeMin
);
executionTimeMax
=
Math
.
max
(
time
,
executionTimeMax
);
rowCountMin
=
Math
.
min
(
rows
,
rowCountMin
);
rowCountMax
=
Math
.
max
(
rows
,
rowCountMax
);
double
delta
=
rows
-
rowCountMean
;
rowCountMean
+=
delta
/
count
;
rowCountM2
+=
delta
*
(
rows
-
rowCountMean
);
delta
=
time
-
executionTimeMean
;
executionTimeMean
+=
delta
/
count
;
executionTimeM2
+=
delta
*
(
time
-
executionTimeMean
);
executionTimeCumulative
+=
time
;
rowCountCumulative
+=
rows
;
lastUpdateTime
=
System
.
currentTimeMillis
();
}
public
double
getExecutionTimeStandardDeviation
()
{
// population standard deviation
return
Math
.
sqrt
(
executionTimeM2
/
count
);
}
public
double
getRowCountStandardDeviation
()
{
// population standard deviation
return
Math
.
sqrt
(
rowCountM2
/
count
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论