[Fix](statistics)Need to recalculate health value when table row count become 0 (#27673)
Need to recalculate health value when table row count become 0. Otherwise, when user truncate a table, the old statistics will not be updated.
This commit is contained in:
@ -724,6 +724,21 @@ public class AnalysisManager implements Writable {
|
||||
StatisticsRepository.dropStatistics(tblId, cols);
|
||||
}
|
||||
|
||||
public void dropStats(TableIf table) throws DdlException {
|
||||
TableStatsMeta tableStats = findTableStatsStatus(table.getId());
|
||||
if (tableStats == null) {
|
||||
return;
|
||||
}
|
||||
Set<String> cols = table.getBaseSchema().stream().map(Column::getName).collect(Collectors.toSet());
|
||||
for (String col : cols) {
|
||||
tableStats.removeColumn(col);
|
||||
Env.getCurrentEnv().getStatisticsCache().invalidate(table.getId(), -1L, col);
|
||||
}
|
||||
tableStats.updatedTime = 0;
|
||||
logCreateTableStats(tableStats);
|
||||
StatisticsRepository.dropStatistics(table.getId(), cols);
|
||||
}
|
||||
|
||||
public void handleKillAnalyzeStmt(KillAnalysisJobStmt killAnalysisJobStmt) throws DdlException {
|
||||
Map<Long, BaseAnalysisTask> analysisTaskMap = analysisJobIdToTaskMap.remove(killAnalysisJobStmt.jobId);
|
||||
if (analysisTaskMap == null) {
|
||||
|
||||
@ -91,15 +91,21 @@ public class StatisticsAutoCollector extends StatisticsCollector {
|
||||
public void analyzeDb(DatabaseIf<TableIf> databaseIf) throws DdlException {
|
||||
List<AnalysisInfo> analysisInfos = constructAnalysisInfo(databaseIf);
|
||||
for (AnalysisInfo analysisInfo : analysisInfos) {
|
||||
analysisInfo = getReAnalyzeRequiredPart(analysisInfo);
|
||||
if (analysisInfo == null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (needDropStaleStats(analysisInfo)) {
|
||||
Env.getCurrentEnv().getAnalysisManager().dropStats(databaseIf.getTable(analysisInfo.tblId).get());
|
||||
continue;
|
||||
}
|
||||
analysisInfo = getReAnalyzeRequiredPart(analysisInfo);
|
||||
if (analysisInfo == null) {
|
||||
continue;
|
||||
}
|
||||
createSystemAnalysisJob(analysisInfo);
|
||||
} catch (Throwable t) {
|
||||
analysisInfo.message = t.getMessage();
|
||||
throw t;
|
||||
LOG.warn("Failed to auto analyze table {}.{}, reason {}",
|
||||
databaseIf.getFullName(), analysisInfo.tblId, analysisInfo.message, t);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -191,4 +197,29 @@ public class StatisticsAutoCollector extends StatisticsCollector {
|
||||
return new AnalysisInfoBuilder(jobInfo).setColToPartitions(needRunPartitions).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given table should drop stale stats. User may truncate table,
|
||||
* in this case, we need to drop the stale stats.
|
||||
* @param jobInfo
|
||||
* @return True if you need to drop, false otherwise.
|
||||
*/
|
||||
protected boolean needDropStaleStats(AnalysisInfo jobInfo) {
|
||||
TableIf table = StatisticsUtil
|
||||
.findTable(jobInfo.catalogId, jobInfo.dbId, jobInfo.tblId);
|
||||
if (!(table instanceof OlapTable)) {
|
||||
return false;
|
||||
}
|
||||
AnalysisManager analysisManager = Env.getServingEnv().getAnalysisManager();
|
||||
TableStatsMeta tblStats = analysisManager.findTableStatsStatus(table.getId());
|
||||
if (tblStats == null) {
|
||||
return false;
|
||||
}
|
||||
if (tblStats.analyzeColumns().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (table.getRowCount() == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -522,8 +522,7 @@ public class StatisticsUtil {
|
||||
*
|
||||
* @param updatedRows The number of rows updated by the table
|
||||
* @param totalRows The current number of rows in the table
|
||||
* the healthier the statistics of the table
|
||||
* @return Health, the value range is [0, 100], the larger the value,
|
||||
* @return Health, the value range is [0, 100], the larger the value, the healthier the statistics of the table.
|
||||
*/
|
||||
public static int getTableHealth(long totalRows, long updatedRows) {
|
||||
if (updatedRows >= totalRows) {
|
||||
|
||||
Reference in New Issue
Block a user