Skip to content

Commit f20aff5

Browse files
authored
[improvement](statistics)Skip auto analyze empty table. (#43865) (#44045)
backport: #43865
1 parent c2732d4 commit f20aff5

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public void createAnalysisJob(AnalyzeTblStmt stmt, boolean proxy) throws DdlExce
219219
}
220220
List<AnalysisInfo> jobs = new ArrayList<>();
221221
autoCollector.createAnalyzeJobForTbl(stmt.getDb(), jobs, stmt.getTable());
222+
if (jobs.isEmpty()) {
223+
return;
224+
}
222225
AnalysisInfo job = autoCollector.getReAnalyzeRequiredPart(jobs.get(0));
223226
if (job != null) {
224227
Env.getCurrentEnv().getStatisticsAutoCollector().createSystemAnalysisJob(job);

fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsAutoCollector.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ protected List<AnalysisInfo> constructAnalysisInfo(DatabaseIf<? extends TableIf>
144144
} catch (Throwable t) {
145145
LOG.warn("Failed to analyze table {}.{}.{}",
146146
db.getCatalog().getName(), db.getFullName(), table.getName(), t);
147-
continue;
148147
}
149148
}
150149
return analysisInfos;
@@ -186,7 +185,19 @@ protected void createAnalyzeJobForTbl(DatabaseIf<? extends TableIf> db,
186185
return;
187186
}
188187
}
189-
long rowCount = StatisticsUtil.isEmptyTable(table, analysisMethod) ? 0 : table.getRowCount();
188+
// We don't auto analyze empty table to avoid all 0 stats.
189+
// Because all 0 is more dangerous than unknown stats when row count report is delayed.
190+
AnalysisManager manager = Env.getServingEnv().getAnalysisManager();
191+
TableStatsMeta tableStatsStatus = manager.findTableStatsStatus(table.getId());
192+
long rowCount = table.getRowCount();
193+
if (rowCount <= 0) {
194+
LOG.info("Table {} is empty, remove its old stats and skip auto analyze it.", table.getName());
195+
// Remove the table's old stats if exists.
196+
if (tableStatsStatus != null && !tableStatsStatus.isColumnsStatsEmpty()) {
197+
manager.dropStats(table);
198+
}
199+
return;
200+
}
190201
AnalysisInfo jobInfo = new AnalysisInfoBuilder()
191202
.setJobId(Env.getCurrentEnv().getNextId())
192203
.setCatalogId(db.getCatalog().getId())

fe/fe-core/src/main/java/org/apache/doris/statistics/TableStatsMeta.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,8 @@ protected void clearStaleIndexRowCount(OlapTable table) {
242242
protected void addIndexRowForTest(long indexId, long rowCount) {
243243
indexesRowCount.put(indexId, rowCount);
244244
}
245+
246+
public boolean isColumnsStatsEmpty() {
247+
return colNameToColStatsMeta == null || colNameToColStatsMeta.isEmpty();
248+
}
245249
}

fe/fe-core/src/test/java/org/apache/doris/statistics/StatisticsAutoCollectorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public List<Column> getSchemaAllIndexes(boolean full) {
142142
columns.add(new Column("c2", PrimitiveType.HLL));
143143
return columns;
144144
}
145+
146+
@Mock
147+
public long getRowCount() {
148+
return 1;
149+
}
145150
};
146151
StatisticsAutoCollector saa = new StatisticsAutoCollector();
147152
List<AnalysisInfo> analysisInfoList = saa.constructAnalysisInfo(new Database(1, "anydb"));
@@ -397,6 +402,11 @@ public List<Long> getMvColumnIndexIds(String columnName) {
397402
objects.add(-1L);
398403
return objects;
399404
}
405+
406+
@Mock
407+
public long getRowCount() {
408+
return 1;
409+
}
400410
};
401411

402412
new MockUp<StatisticsUtil>() {
@@ -469,6 +479,11 @@ public List<Long> getMvColumnIndexIds(String columnName) {
469479
objects.add(-1L);
470480
return objects;
471481
}
482+
483+
@Mock
484+
public long getRowCount() {
485+
return 1;
486+
}
472487
};
473488

474489
new MockUp<StatisticsUtil>() {

regression-test/suites/statistics/test_analyze_mv.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,18 @@ suite("test_analyze_mv") {
689689
assertEquals("0", result_row[0][3])
690690
assertEquals("-1", result_row[0][4])
691691

692+
// ** Embedded test for skip auto analyze when table is empty
693+
sql """analyze table mvTestDup properties ("use.auto.analyzer" = "true")"""
694+
def empty_test = sql """show auto analyze mvTestDup"""
695+
assertEquals(0, empty_test.size())
696+
empty_test = sql """show column stats mvTestDup"""
697+
assertEquals(0, empty_test.size())
698+
// ** End of embedded test
699+
700+
sql """analyze table mvTestDup with sync"""
701+
empty_test = sql """show column stats mvTestDup"""
702+
assertEquals(12, empty_test.size())
703+
692704
for (int i = 0; i < 120; i++) {
693705
result_row = sql """show index stats mvTestDup mv3"""
694706
logger.info("mv3 stats: " + result_row)
@@ -703,6 +715,27 @@ suite("test_analyze_mv") {
703715
assertEquals("mv3", result_row[0][1])
704716
assertEquals("0", result_row[0][3])
705717
assertEquals("0", result_row[0][4])
718+
719+
// ** Embedded test for skip auto analyze when table is empty again
720+
sql """analyze table mvTestDup properties ("use.auto.analyzer" = "true")"""
721+
empty_test = sql """show auto analyze mvTestDup"""
722+
assertEquals(0, empty_test.size())
723+
empty_test = sql """show column stats mvTestDup"""
724+
for (int i = 0; i < 100; i++) {
725+
empty_test = sql """show column stats mvTestDup"""
726+
if (empty_test.size() != 0) {
727+
logger.info("async delete is not finished yet.")
728+
Thread.sleep(1000)
729+
}
730+
break
731+
}
732+
assertEquals(0, empty_test.size())
733+
// ** End of embedded test
734+
735+
sql """analyze table mvTestDup with sync"""
736+
empty_test = sql """show column stats mvTestDup"""
737+
assertEquals(12, empty_test.size())
738+
706739
sql """insert into mvTestDup values (1, 2, 3, 4, 5), (1, 2, 3, 4, 5), (10, 20, 30, 40, 50), (10, 20, 30, 40, 50), (100, 200, 300, 400, 500), (1001, 2001, 3001, 4001, 5001);"""
707740
result_row = sql """show index stats mvTestDup mv3"""
708741
assertEquals(1, result_row.size())

0 commit comments

Comments
 (0)