连续聚合
适用于 ❌ 开源版 ✅ Express 版 ✅ 专业版 ✅ 企业版
处理此事件的 SPI 方法是 consecutiveAggregation()
多个连续的聚合查询(相似的 FROM 子句),但每个查询使用不同的聚合函数和不同的WHERE 子句,可能会被统一成一个查询,一次性聚合所有内容。
为什么这不好?
与其运行 N 个查询,不如只运行 1 个查询。
这里给出了一个例子
// A custom DiagnosticsListener SPI implementation
class ConsecutiveAggregation implements DiagnosticsListener {
@Override
public void consecutiveAggregation(DiagnosticsContext ctx) {
// The statement that is being executed and which has similar aggregate queries.
System.out.println("Actual statement: " + ctx.actualStatement());
// A normalised version of the actual statement, which is shared by all duplicates
// This statement has its SELECT and WHERE clause removed.
System.out.println("Normalised statement: " + ctx.normalisedStatement());
// All the duplicate actual statements that have produced the same normalised
// statement in the recent past.
System.out.println("Repeated statements: " + ctx.repeatedStatements());
}
}
然后
// Configuration is configured with the target DataSource, SQLDialect, etc. for instance Oracle.
try (
Connection c = DSL.using(configuration.derive(new ConsecutiveAggregation()))
.diagnosticsConnection();
Statement s = c.createStatement()
) {
try (ResultSet a = s.executeQuery("SELECT count(*) FROM author")) {
while (a.next())
println(a.getInt(1));
}
// This query could be merged into the previous one e.g.:
// SELECT count(*), count(*) FILTER (WHERE last_name LIKE 'A%') FROM author
try (ResultSet a = s.executeQuery("SELECT count(*) FROM author WHERE last_name LIKE 'A%'")) {
while (a.next())
println(a.getInt(1));
}
}
与检测重复语句不同,重复语句统计信息仅在本地执行,针对单个 JDBC 连接,或者如果可能,针对一个事务。不同事务中的重复语句通常不是问题的迹象。
反馈
您对此页面有任何反馈吗? 我们很乐意听取您的意见!