提交 d70793f2 authored 作者: noelgrandin's avatar noelgrandin

- only expose the top 100 results in the query statistics table

- fix an occasional error in the test-case
上级 51f65d81
...@@ -12,6 +12,7 @@ import java.util.Comparator; ...@@ -12,6 +12,7 @@ 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.Map; import java.util.Map;
/** /**
...@@ -22,6 +23,8 @@ public class QueryStatisticsData { ...@@ -22,6 +23,8 @@ public class QueryStatisticsData {
private static final int MAX_QUERY_ENTRIES = 100; private static final int MAX_QUERY_ENTRIES = 100;
public static final class QueryEntry { public static final class QueryEntry {
public String sqlStatement;
public long lastUpdateTime; public long lastUpdateTime;
public int count; public int count;
public long executionTimeMin; public long executionTimeMin;
...@@ -64,6 +67,7 @@ public class QueryStatisticsData { ...@@ -64,6 +67,7 @@ public class QueryStatisticsData {
QueryEntry entry = map.get(sqlStatement); QueryEntry entry = map.get(sqlStatement);
if (entry == null) { if (entry == null) {
entry = new QueryEntry(); entry = new QueryEntry();
entry.sqlStatement = sqlStatement;
entry.count = 1; entry.count = 1;
entry.executionTimeMin = executionTime; entry.executionTimeMin = executionTime;
entry.executionTimeMax = executionTime; entry.executionTimeMax = executionTime;
...@@ -113,9 +117,13 @@ public class QueryStatisticsData { ...@@ -113,9 +117,13 @@ public class QueryStatisticsData {
} }
} }
public synchronized HashMap<String, QueryEntry> getQueryMap() { 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 worry about external synchronization
return new HashMap<String, QueryEntry>(map); ArrayList<QueryEntry> list = new ArrayList<QueryEntry>();
list.addAll(map.values());
// only return the newest 100 entries
Collections.sort(list, QUERY_ENTRY_COMPARATOR);
return list.subList(0, Math.min(list.size(), MAX_QUERY_ENTRIES));
} }
private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR = new Comparator<QueryEntry>() { private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR = new Comparator<QueryEntry>() {
......
...@@ -17,7 +17,6 @@ import java.util.ArrayList; ...@@ -17,7 +17,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import org.h2.command.Command; import org.h2.command.Command;
import org.h2.constraint.Constraint; import org.h2.constraint.Constraint;
import org.h2.constraint.ConstraintCheck; import org.h2.constraint.ConstraintCheck;
...@@ -1642,12 +1641,10 @@ public class MetaTable extends Table { ...@@ -1642,12 +1641,10 @@ public class MetaTable extends Table {
case QUERY_STATISTICS: { case QUERY_STATISTICS: {
QueryStatisticsData control = database.getQueryStatisticsData(); QueryStatisticsData control = database.getQueryStatisticsData();
if (control != null) { if (control != null) {
HashMap<String, QueryStatisticsData.QueryEntry> map = control.getQueryMap(); for (QueryStatisticsData.QueryEntry entry : control.getQueries()) {
for (Map.Entry<String, QueryStatisticsData.QueryEntry> mapEntry : map.entrySet()) {
QueryStatisticsData.QueryEntry entry = mapEntry.getValue();
add(rows, add(rows,
// SQL_STATEMENT // SQL_STATEMENT
mapEntry.getKey(), entry.sqlStatement,
// EXECUTION_COUNT // EXECUTION_COUNT
"" + entry.count, "" + entry.count,
// MIN_EXECUTION_TIME // MIN_EXECUTION_TIME
......
...@@ -1010,7 +1010,8 @@ public class TestMetaData extends TestBase { ...@@ -1010,7 +1010,8 @@ public class TestMetaData extends TestBase {
stat.execute("SET QUERY_STATISTICS TRUE"); stat.execute("SET QUERY_STATISTICS TRUE");
stat.execute("select * from test limit 10"); stat.execute("select * from test limit 10");
stat.execute("select * from test limit 10"); stat.execute("select * from test limit 10");
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.QUERY_STATISTICS"); // The "order by" makes the resultset more stable on windows, where the timer resolution is not that great
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.QUERY_STATISTICS ORDER BY EXECUTION_COUNT");
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals("select * from test limit 10", rs.getString("SQL_STATEMENT")); assertEquals("select * from test limit 10", rs.getString("SQL_STATEMENT"));
assertEquals(2, rs.getInt("EXECUTION_COUNT")); assertEquals(2, rs.getInt("EXECUTION_COUNT"));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论