#1868 - Change the Metric "sqlHash" into a hash of the SQL + name + location

This commit is contained in:
rob bygrave
2019-11-20 20:15:32 +13:00
parent 88839f7613
commit eb292e017e
19 changed files with 256 additions and 31 deletions
@@ -60,7 +60,7 @@ class CQueryBindCapture {
DQueryPlanOutput queryPlan = planLogger.logQueryPlan(request.getConnection(), cQueryPlan, last);
if (queryPlan != null) {
queryPlan.with(queryTimeMicros, captureCount, cQueryPlan.getSqlHash());
queryPlan.with(queryTimeMicros, captureCount, cQueryPlan.getHash());
request.process(queryPlan);
}
}
@@ -73,7 +73,7 @@ public class CQueryPlan {
private final boolean rowNumberIncluded;
private final String sql;
private final String sqlHash;
private final String hash;
private final String logWhereSql;
@@ -118,7 +118,6 @@ public class CQueryPlan {
this.autoTuned = query.isAutoTuned();
this.asOfTableCount = query.getAsOfTableCount();
this.sql = sqlRes.getSql();
this.sqlHash = md5Hash(sql);
this.rowNumberIncluded = sqlRes.isIncludesRowNumberColumn();
this.sqlTree = sqlTree;
this.rawSql = rawSql;
@@ -127,6 +126,7 @@ public class CQueryPlan {
this.stats = new CQueryPlanStats(this, server.isCollectQueryOrigins());
this.dependentTables = sqlTree.dependentTables();
this.bindCapture = initBindCapture(server.getServerConfig(), query);
this.hash = md5Hash();
}
/**
@@ -146,7 +146,6 @@ public class CQueryPlan {
this.autoTuned = false;
this.asOfTableCount = 0;
this.sql = sql;
this.sqlHash = md5Hash(sql);
this.sqlTree = sqlTree;
this.rawSql = false;
this.rowNumberIncluded = rowNumberIncluded;
@@ -155,6 +154,7 @@ public class CQueryPlan {
this.stats = new CQueryPlanStats(this, server.isCollectQueryOrigins());
this.dependentTables = sqlTree.dependentTables();
this.bindCapture = initBindCapture(server.getServerConfig(), query);
this.hash = md5Hash();
}
private String deriveName(String label, SpiQuery.Type type) {
@@ -260,23 +260,26 @@ public class CQueryPlan {
private String calcAuditQueryKey() {
// rawSql needs to include the MD5 hash of the sql
return rawSql ? planKey.getPartialKey() + "_" + sqlHash : planKey.getPartialKey();
return rawSql ? planKey.getPartialKey() + "_" + hash : planKey.getPartialKey();
}
/**
* Return the MD5 hash of the sql.
*/
private String md5Hash(String sql) {
private String md5Hash() {
StringBuilder sb = new StringBuilder(sql)
.append("|").append(name)
.append("|").append(location);
try {
return Md5.hash(sql);
return Md5.hash(sb.toString());
} catch (Exception e) {
logger.error("Failed to MD5 hash the query", e);
return "error";
}
}
String getSqlHash() {
return sqlHash;
String getHash() {
return hash;
}
public String getSql() {
@@ -233,8 +233,8 @@ public final class CQueryPlanStats {
}
@Override
public String getSqlHash() {
return queryPlan.getSqlHash();
public String getHash() {
return queryPlan.getHash();
}
@Override
@@ -16,7 +16,7 @@ class DQueryPlanOutput implements MetaQueryPlan {
private final String bind;
private final String plan;
private String sqlHash;
private String hash;
private long queryTimeMicros;
private long captureCount;
@@ -30,8 +30,8 @@ class DQueryPlanOutput implements MetaQueryPlan {
}
@Override
public String getSqlHash() {
return sqlHash;
public String getHash() {
return hash;
}
/**
@@ -98,15 +98,15 @@ class DQueryPlanOutput implements MetaQueryPlan {
@Override
public String toString() {
return " BeanType:" + ((beanType == null) ? "" : beanType.getSimpleName()) + " planHash:" + sqlHash + " label:" + label + " queryTimeMicros:" + queryTimeMicros + " captureCount:" + captureCount + "\n SQL:" + sql + "\nBIND:" + bind + "\nPLAN:" + plan;
return " BeanType:" + ((beanType == null) ? "" : beanType.getSimpleName()) + " planHash:" + hash + " label:" + label + " queryTimeMicros:" + queryTimeMicros + " captureCount:" + captureCount + "\n SQL:" + sql + "\nBIND:" + bind + "\nPLAN:" + plan;
}
/**
* Additionally set the query execution time and the number of bind captures.
*/
void with(long queryTimeMicros, long captureCount, String sqlHash) {
void with(long queryTimeMicros, long captureCount, String hash) {
this.queryTimeMicros = queryTimeMicros;
this.captureCount = captureCount;
this.sqlHash = sqlHash;
this.hash = hash;
}
}