Debian Codegraph @

src/main/org/h2/engine/QueryStatisticsData.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import java.util.ArrayList;
9 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map.Entry;
15
16 /**
17 * Maintains query statistics.
18 */
19 public class QueryStatisticsData {
20
21 private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR =
22 Comparator.comparingLong(q -> q.lastUpdateTime);
23
24 private final HashMap<String, QueryEntry> map = new HashMap<>();
25
26 private int maxQueryEntries;
27
28 public QueryStatisticsData(int maxQueryEntries) {
29 this.maxQueryEntries = maxQueryEntries;
30 }
31
32 public synchronized void setMaxQueryEntries(int maxQueryEntries) {
33 this.maxQueryEntries = maxQueryEntries;
34 }
35
36 public synchronized List<QueryEntry> getQueries() {
37 // return a copy of the map so we don't have to
38 // worry about external synchronization
39 ArrayList<QueryEntry> list = new ArrayList<>(map.values());
40 // only return the newest 100 entries
41 list.sort(QUERY_ENTRY_COMPARATOR);
42 return list.subList(0, Math.min(list.size(), maxQueryEntries));
43 }
44
45 /**
46 * Update query statistics.
47 *
48 * @param sqlStatement the statement being executed
49 * @param executionTimeNanos the time in nanoseconds the query/update took
50 * to execute
51 * @param rowCount the query or update row count
52 */
53 public synchronized void update(String sqlStatement, long executionTimeNanos, long rowCount) {
54 QueryEntry entry = map.get(sqlStatement);
55 if (entry == null) {
56 entry = new QueryEntry(sqlStatement);
57 map.put(sqlStatement, entry);
58 }
59 entry.update(executionTimeNanos, rowCount);
60
61 // Age-out the oldest entries if the map gets too big.
62 // Test against 1.5 x max-size so we don't do this too often
63 if (map.size() > maxQueryEntries * 1.5f) {
64 // Sort the entries by age
65 ArrayList<QueryEntry> list = new ArrayList<>(map.values());
66 list.sort(QUERY_ENTRY_COMPARATOR);
67 // Create a set of the oldest 1/3 of the entries
68 HashSet<QueryEntry> oldestSet =
69 new HashSet<>(list.subList(0, list.size() / 3));
70 // Loop over the map using the set and remove
71 // the oldest 1/3 of the entries.
72 for (Iterator<Entry<String, QueryEntry>> it =
73 map.entrySet().iterator(); it.hasNext();) {
74 Entry<String, QueryEntry> mapEntry = it.next();
75 if (oldestSet.contains(mapEntry.getValue())) {
76 it.remove();
77 }
78 }
79 }
80 }
81
82 /**
83 * The collected statistics for one query.
84 */
85 public static final class QueryEntry {
86
87 /**
88 * The SQL statement.
89 */
90 public final String sqlStatement;
91
92 /**
93 * The number of times the statement was executed.
94 */
95 public int count;
96
97 /**
98 * The last time the statistics for this entry were updated,
99 * in milliseconds since 1970.
100 */
101 public long lastUpdateTime;
102
103 /**
104 * The minimum execution time, in nanoseconds.
105 */
106 public long executionTimeMinNanos;
107
108 /**
109 * The maximum execution time, in nanoseconds.
110 */
111 public long executionTimeMaxNanos;
112
113 /**
114 * The total execution time.
115 */
116 public long executionTimeCumulativeNanos;
117
118 /**
119 * The minimum number of rows.
120 */
121 public long rowCountMin;
122
123 /**
124 * The maximum number of rows.
125 */
126 public long rowCountMax;
127
128 /**
129 * The total number of rows.
130 */
131 public long rowCountCumulative;
132
133 /**
134 * The mean execution time.
135 */
136 public double executionTimeMeanNanos;
137
138 /**
139 * The mean number of rows.
140 */
141 public double rowCountMean;
142
143 // Using Welford's method, see also
144 // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
145 // https://www.johndcook.com/blog/standard_deviation/
146
147 private double executionTimeM2Nanos;
148 private double rowCountM2;
149
150 public QueryEntry(String sql) {
151 this.sqlStatement = sql;
152 }
153
154 /**
155 * Update the statistics entry.
156 *
157 * @param timeNanos the execution time in nanos
158 * @param rows the number of rows
159 */
160 void update(long timeNanos, long rows) {
161 count++;
162 executionTimeMinNanos = Math.min(timeNanos, executionTimeMinNanos);
163 executionTimeMaxNanos = Math.max(timeNanos, executionTimeMaxNanos);
164 rowCountMin = Math.min(rows, rowCountMin);
165 rowCountMax = Math.max(rows, rowCountMax);
166
167 double rowDelta = rows - rowCountMean;
168 rowCountMean += rowDelta / count;
169 rowCountM2 += rowDelta * (rows - rowCountMean);
170
171 double timeDelta = timeNanos - executionTimeMeanNanos;
172 executionTimeMeanNanos += timeDelta / count;
173 executionTimeM2Nanos += timeDelta * (timeNanos - executionTimeMeanNanos);
174
175 executionTimeCumulativeNanos += timeNanos;
176 rowCountCumulative += rows;
177 lastUpdateTime = System.currentTimeMillis();
178 }
179
180 public double getExecutionTimeStandardDeviation() {
181 // population standard deviation
182 return Math.sqrt(executionTimeM2Nanos / count);
183 }
184
185 public double getRowCountStandardDeviation() {
186 // population standard deviation
187 return Math.sqrt(rowCountM2 / count);
188 }
189
190 }
191
192 }