Skip to content

Commit 058dfd3

Browse files
authored
[opt](schema) Optimize tables schema scan status (apache#64933)
Optimize information_schema.tables status scan for large OlapTables. Before this PR, FE always filled the full table status payload for information_schema.tables, so even queries that only projected lightweight columns such as UPDATE_TIME still paid the cost of tablet-derived status fields. This PR includes the following optimizations: | Optimization | Description | | --- | --- | | Column pruning for table status RPC | BE passes the required information_schema.tables columns to FE, and FE only fills requested optional status fields. Lightweight queries can skip TABLE_ROWS, DATA_LENGTH, AVG_ROW_LENGTH, and INDEX_LENGTH. | | Single tablet-status traversal | TABLE_ROWS, DATA_LENGTH, AVG_ROW_LENGTH, and INDEX_LENGTH are computed together when any of them is requested, avoiding repeated OlapTable tablet/replica traversal. | This PR does not add a table-level cache for tablet-derived status fields. The benchmark still shows the possible room for that future optimization: the full status query remains slower than the UPDATE_TIME-only query because it still needs to read tablet-derived status, while the lightweight query can skip it completely. Benchmark on a table with 3900 partitions and 21 buckets per partition, about 81,900 tablets, workers=20, targetQps=100: | Query | Before avgQps | After avgQps | Improvement | Notes | | --- | ---: | ---: | ---: | --- | | SHOW TABLE STATUS FROM regression_test_stress_load_usercase LIKE ... | 26.3 | 49.1 | +86.7% | Full status query. It still needs tablet-derived status fields, but benefits from the single traversal optimization. | | SELECT TABLE_NAME, UPDATE_TIME FROM information_schema.tables WHERE TABLE_SCHEMA = ... | 29.3 | 92.2 | +214.7% | Lightweight projection. It benefits from column pruning and skips tablet-derived status fields. |
1 parent d1d8b29 commit 058dfd3

12 files changed

Lines changed: 470 additions & 39 deletions

File tree

‎be/src/exec/operator/schema_scan_operator.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "core/data_type/data_type_factory.hpp"
2626
#include "exec/operator/operator.h"
2727
#include "runtime/runtime_profile.h"
28+
#include "util/string_util.h"
2829

2930
namespace doris {
3031
class RuntimeState;
@@ -186,6 +187,8 @@ Status SchemaScanOperatorX::prepare(RuntimeState* state) {
186187
for (; j < columns_desc.size(); ++j) {
187188
if (boost::iequals(_dest_tuple_desc->slots()[i]->col_name(), columns_desc[j].name)) {
188189
_slot_offsets[i] = j;
190+
_common_scanner_param->required_columns.insert(
191+
to_upper(_dest_tuple_desc->slots()[i]->col_name()));
189192
break;
190193
}
191194
}

‎be/src/information_schema/schema_scanner.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cstddef>
2525
#include <cstdint>
2626
#include <memory>
27+
#include <set>
2728
#include <string>
2829
#include <vector>
2930

@@ -69,6 +70,7 @@ struct SchemaScannerCommonParam {
6970
int64_t thread_id;
7071
const std::string* catalog = nullptr;
7172
std::set<TNetworkAddress> fe_addr_list;
73+
std::set<std::string> required_columns;
7274
};
7375

7476
// scanner parameter from frontend

‎be/src/information_schema/schema_tables_scanner.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Status SchemaTablesScanner::_get_new_table() {
114114
if (nullptr != _param->common_param->table) {
115115
table_params.__set_table(*(_param->common_param->table));
116116
}
117+
table_params.__set_required_columns(_param->common_param->required_columns);
117118
if (nullptr != _param->common_param->current_user_ident) {
118119
table_params.__set_current_user_ident(*(_param->common_param->current_user_ident));
119120
} else {

‎fe/fe-core/src/main/java/org/apache/doris/catalog/LocalTablet.java‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.Collections;
3535
import java.util.Comparator;
3636
import java.util.List;
37-
import java.util.stream.LongStream;
3837

3938
public class LocalTablet extends Tablet {
4039
private static final Logger LOG = LogManager.getLogger(LocalTablet.class);
@@ -133,17 +132,34 @@ public Replica getReplicaById(long replicaId) {
133132
// due to dataSize not write to image
134133
@Override
135134
public long getDataSize(boolean singleReplica, boolean filterSizeZero) {
136-
LongStream s = getReplicas().stream().filter(r -> r.getState() == ReplicaState.NORMAL)
137-
.filter(r -> !filterSizeZero || r.getDataSize() > 0)
138-
.mapToLong(Replica::getDataSize);
139-
return singleReplica ? Double.valueOf(s.average().orElse(0)).longValue() : s.sum();
135+
long dataSize = 0;
136+
int replicaNum = 0;
137+
for (Replica replica : getReplicas()) {
138+
if (replica.getState() != ReplicaState.NORMAL) {
139+
continue;
140+
}
141+
long replicaDataSize = replica.getDataSize();
142+
if (filterSizeZero && replicaDataSize <= 0) {
143+
continue;
144+
}
145+
dataSize += replicaDataSize;
146+
replicaNum++;
147+
}
148+
return singleReplica && replicaNum > 0 ? dataSize / replicaNum : dataSize;
140149
}
141150

142151
@Override
143152
public long getRowCount(boolean singleReplica) {
144-
LongStream s = getReplicas().stream().filter(r -> r.getState() == ReplicaState.NORMAL)
145-
.mapToLong(Replica::getRowCount);
146-
return singleReplica ? Double.valueOf(s.average().orElse(0)).longValue() : s.sum();
153+
long rowCount = 0;
154+
int replicaNum = 0;
155+
for (Replica replica : getReplicas()) {
156+
if (replica.getState() != ReplicaState.NORMAL) {
157+
continue;
158+
}
159+
rowCount += replica.getRowCount();
160+
replicaNum++;
161+
}
162+
return singleReplica && replicaNum > 0 ? rowCount / replicaNum : rowCount;
147163
}
148164

149165
// Get the least row count among all valid replicas.

‎fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTable.java‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,37 +1911,43 @@ public long getRowCountForPartitionIndex(long partitionId, long indexId, boolean
19111911

19121912
@Override
19131913
public long getAvgRowLength() {
1914-
long rowCount = 0;
1915-
long dataSize = 0;
1916-
for (Map.Entry<Long, Partition> entry : idToPartition.entrySet()) {
1917-
rowCount += entry.getValue().getBaseIndex().getRowCount();
1918-
dataSize += entry.getValue().getBaseIndex().getDataSize(false, false);
1919-
}
1920-
if (rowCount > 0) {
1921-
return dataSize / rowCount;
1922-
} else {
1923-
return 0;
1924-
}
1914+
return getTableStatusStats().getAvgRowLength();
19251915
}
19261916

19271917
@Override
19281918
public long getDataLength() {
1929-
long dataSize = 0;
1930-
for (Map.Entry<Long, Partition> entry : idToPartition.entrySet()) {
1931-
dataSize += entry.getValue().getBaseIndex().getLocalSegmentSize();
1932-
dataSize += entry.getValue().getBaseIndex().getRemoteSegmentSize();
1933-
}
1934-
return dataSize;
1919+
return getTableStatusStats().getDataLength();
19351920
}
19361921

19371922
@Override
19381923
public long getIndexLength() {
1939-
long indexSize = 0;
1924+
return getTableStatusStats().getIndexLength();
1925+
}
1926+
1927+
@Override
1928+
public TableStatusStats getTableStatusStats() {
1929+
long rowCount = 0;
1930+
long rowCountForAvg = 0;
1931+
long dataSizeForAvg = 0;
1932+
long dataLength = 0;
1933+
long indexLength = 0;
19401934
for (Map.Entry<Long, Partition> entry : idToPartition.entrySet()) {
1941-
indexSize += entry.getValue().getBaseIndex().getLocalIndexSize();
1942-
indexSize += entry.getValue().getBaseIndex().getRemoteIndexSize();
1935+
MaterializedIndex baseIndex = entry.getValue().getBaseIndex();
1936+
long baseIndexRowCount = baseIndex.getRowCount();
1937+
rowCount += baseIndexRowCount == UNKNOWN_ROW_COUNT ? 0 : baseIndexRowCount;
1938+
rowCountForAvg += baseIndexRowCount;
1939+
for (Tablet tablet : baseIndex.getTablets()) {
1940+
for (Replica replica : tablet.getReplicas()) {
1941+
if (replica.getState() == Replica.ReplicaState.NORMAL) {
1942+
dataSizeForAvg += replica.getDataSize();
1943+
}
1944+
dataLength += replica.getLocalSegmentSize() + replica.getRemoteSegmentSize();
1945+
indexLength += replica.getLocalInvertedIndexSize() + replica.getRemoteInvertedIndexSize();
1946+
}
1947+
}
19431948
}
1944-
return indexSize;
1949+
long avgRowLength = rowCountForAvg > 0 ? dataSizeForAvg / rowCountForAvg : 0;
1950+
return new TableStatusStats(rowCount, dataLength, avgRowLength, indexLength);
19451951
}
19461952

19471953
// Get the signature string of this table with specified partitions.

‎fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@
4646
public interface TableIf {
4747
Logger LOG = LogManager.getLogger(TableIf.class);
4848

49+
class TableStatusStats {
50+
private final long rows;
51+
private final long dataLength;
52+
private final long avgRowLength;
53+
private final long indexLength;
54+
55+
public TableStatusStats(long rows, long dataLength, long avgRowLength, long indexLength) {
56+
this.rows = rows;
57+
this.dataLength = dataLength;
58+
this.avgRowLength = avgRowLength;
59+
this.indexLength = indexLength;
60+
}
61+
62+
public long getRows() {
63+
return rows;
64+
}
65+
66+
public long getDataLength() {
67+
return dataLength;
68+
}
69+
70+
public long getAvgRowLength() {
71+
return avgRowLength;
72+
}
73+
74+
public long getIndexLength() {
75+
return indexLength;
76+
}
77+
}
78+
4979
long UNKNOWN_ROW_COUNT = -1;
5080

5181
default void readLock() {
@@ -176,6 +206,10 @@ default int getBaseColumnIdxByName(String colName) {
176206

177207
long getIndexLength();
178208

209+
default TableStatusStats getTableStatusStats() {
210+
return new TableStatusStats(getCachedRowCount(), getDataLength(), getAvgRowLength(), getIndexLength());
211+
}
212+
179213
long getLastCheckTime();
180214

181215
String getComment(boolean escapeQuota);

‎fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java‎

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
import java.util.HashMap;
351351
import java.util.HashSet;
352352
import java.util.List;
353+
import java.util.Locale;
353354
import java.util.Map;
354355
import java.util.Optional;
355356
import java.util.Random;
@@ -660,6 +661,11 @@ public TListTableStatusResult listTableStatus(TGetTablesParams params) throws TE
660661
TListTableStatusResult result = new TListTableStatusResult();
661662
List<TTableStatus> tablesResult = Lists.newArrayList();
662663
result.setTables(tablesResult);
664+
Set<String> requiredColumns = params.isSetRequiredColumns()
665+
? params.getRequiredColumns().stream()
666+
.map(column -> column.toUpperCase(Locale.ROOT))
667+
.collect(Collectors.toSet())
668+
: null;
663669
PatternMatcher matcher = null;
664670
String specifiedTable = null;
665671
if (params.isSetPattern()) {
@@ -739,17 +745,40 @@ public TListTableStatusResult listTableStatus(TGetTablesParams params) throws TE
739745
TTableStatus status = new TTableStatus();
740746
status.setName(table.getName());
741747
status.setType(table.getMysqlType());
742-
status.setEngine(table.getEngine());
743748
status.setComment(table.getComment());
744-
status.setCreateTime(table.getCreateTime());
745-
status.setLastCheckTime(lastCheckTime / 1000);
746-
status.setUpdateTime(table.getUpdateTime() / 1000);
747-
status.setCheckTime(lastCheckTime / 1000);
748-
status.setCollation("utf-8");
749-
status.setRows(table.getCachedRowCount());
750-
status.setDataLength(table.getDataLength());
751-
status.setAvgRowLength(table.getAvgRowLength());
752-
status.setIndexLength(table.getIndexLength());
749+
if (needTableStatusColumn(requiredColumns, "ENGINE")) {
750+
status.setEngine(table.getEngine());
751+
}
752+
if (needTableStatusColumn(requiredColumns, "CREATE_TIME")) {
753+
status.setCreateTime(table.getCreateTime());
754+
}
755+
if (needTableStatusColumn(requiredColumns, "LAST_CHECK_TIME")
756+
|| needTableStatusColumn(requiredColumns, "CHECK_TIME")) {
757+
status.setLastCheckTime(lastCheckTime / 1000);
758+
}
759+
if (needTableStatusColumn(requiredColumns, "UPDATE_TIME")) {
760+
status.setUpdateTime(table.getUpdateTime() / 1000);
761+
}
762+
if (needTableStatusColumn(requiredColumns, "CHECK_TIME")) {
763+
status.setCheckTime(lastCheckTime / 1000);
764+
}
765+
if (needTableStatusColumn(requiredColumns, "TABLE_COLLATION")) {
766+
status.setCollation("utf-8");
767+
}
768+
TableIf.TableStatusStats tableStatusStats =
769+
needTableStatusStats(requiredColumns) ? table.getTableStatusStats() : null;
770+
if (needTableStatusColumn(requiredColumns, "TABLE_ROWS")) {
771+
status.setRows(tableStatusStats.getRows());
772+
}
773+
if (needTableStatusColumn(requiredColumns, "DATA_LENGTH")) {
774+
status.setDataLength(tableStatusStats.getDataLength());
775+
}
776+
if (needTableStatusColumn(requiredColumns, "AVG_ROW_LENGTH")) {
777+
status.setAvgRowLength(tableStatusStats.getAvgRowLength());
778+
}
779+
if (needTableStatusColumn(requiredColumns, "INDEX_LENGTH")) {
780+
status.setIndexLength(tableStatusStats.getIndexLength());
781+
}
753782
if (table instanceof View) {
754783
status.setDdlSql(((View) table).getInlineViewDef());
755784
}
@@ -766,6 +795,17 @@ public TListTableStatusResult listTableStatus(TGetTablesParams params) throws TE
766795
return result;
767796
}
768797

798+
private static boolean needTableStatusColumn(Set<String> requiredColumns, String columnName) {
799+
return requiredColumns == null || requiredColumns.contains(columnName);
800+
}
801+
802+
private static boolean needTableStatusStats(Set<String> requiredColumns) {
803+
return needTableStatusColumn(requiredColumns, "TABLE_ROWS")
804+
|| needTableStatusColumn(requiredColumns, "DATA_LENGTH")
805+
|| needTableStatusColumn(requiredColumns, "AVG_ROW_LENGTH")
806+
|| needTableStatusColumn(requiredColumns, "INDEX_LENGTH");
807+
}
808+
769809
public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams params) throws TException {
770810
if (LOG.isDebugEnabled()) {
771811
LOG.debug("get list simple table request: {}", params);

‎fe/fe-core/src/test/java/org/apache/doris/catalog/OlapTableTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@
5252

5353
public class OlapTableTest {
5454

55+
@Test
56+
public void testGetTableStatusStatsUsesSinglePassSemantics() {
57+
OlapTable olapTable = new OlapTable();
58+
MaterializedIndex index = new MaterializedIndex(10, MaterializedIndex.IndexState.NORMAL);
59+
index.setRowCount(20);
60+
61+
List<Replica> replicas = Lists.newArrayList(
62+
mockReplica(Replica.ReplicaState.NORMAL, 10, 100, 1, 20, 2),
63+
mockReplica(Replica.ReplicaState.DECOMMISSION, 30, 300, 3, 40, 4),
64+
mockReplica(Replica.ReplicaState.NORMAL, 50, 500, 5, 60, 6));
65+
Tablet tablet = Mockito.mock(Tablet.class);
66+
Mockito.when(tablet.getReplicas()).thenReturn(replicas);
67+
index.appendTablets(Lists.newArrayList(tablet));
68+
69+
Partition partition = new Partition(11, "p1", index, null);
70+
olapTable.addPartition(partition);
71+
72+
TableIf.TableStatusStats stats = olapTable.getTableStatusStats();
73+
Assert.assertEquals(20L, stats.getRows());
74+
Assert.assertEquals(909L, stats.getDataLength());
75+
Assert.assertEquals(3L, stats.getAvgRowLength());
76+
Assert.assertEquals(132L, stats.getIndexLength());
77+
}
78+
79+
private Replica mockReplica(Replica.ReplicaState state, long dataSize, long localSegmentSize,
80+
long remoteSegmentSize, long localIndexSize, long remoteIndexSize) {
81+
Replica replica = Mockito.mock(Replica.class);
82+
Mockito.when(replica.getState()).thenReturn(state);
83+
Mockito.when(replica.getDataSize()).thenReturn(dataSize);
84+
Mockito.when(replica.getLocalSegmentSize()).thenReturn(localSegmentSize);
85+
Mockito.when(replica.getRemoteSegmentSize()).thenReturn(remoteSegmentSize);
86+
Mockito.when(replica.getLocalInvertedIndexSize()).thenReturn(localIndexSize);
87+
Mockito.when(replica.getRemoteInvertedIndexSize()).thenReturn(remoteIndexSize);
88+
return replica;
89+
}
90+
5591
@Test
5692
public void test() throws IOException {
5793

‎fe/fe-core/src/test/java/org/apache/doris/catalog/TabletTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ public void getMethodTest() {
105105
Assert.assertEquals("tabletId=" + newTabletId, tablet.toString());
106106
}
107107

108+
@Test
109+
public void testGetReplicaStatsOnlyUsesNormalReplicas() {
110+
Tablet statsTablet = new LocalTablet(2);
111+
invertedIndex.addTablet(2, new TabletMeta(10, 20, 30, 40, 1, TStorageMedium.HDD));
112+
statsTablet.addReplica(new LocalReplica(11L, 1L, 100L, 0, 10L, 0L, 100L, ReplicaState.NORMAL, 0L, 100L));
113+
statsTablet.addReplica(new LocalReplica(12L, 2L, 100L, 0, 20L, 0L, 200L, ReplicaState.NORMAL, 0L, 100L));
114+
statsTablet.addReplica(new LocalReplica(13L, 3L, 100L, 0, 0L, 0L, 300L, ReplicaState.NORMAL, 0L, 100L));
115+
statsTablet.addReplica(new LocalReplica(14L, 4L, 100L, 0, 40L, 0L, 400L, ReplicaState.DECOMMISSION,
116+
0L, 100L));
117+
118+
Assert.assertEquals(30L, statsTablet.getDataSize(false, false));
119+
Assert.assertEquals(10L, statsTablet.getDataSize(true, false));
120+
Assert.assertEquals(30L, statsTablet.getDataSize(false, true));
121+
Assert.assertEquals(600L, statsTablet.getRowCount(false));
122+
Assert.assertEquals(200L, statsTablet.getRowCount(true));
123+
}
124+
108125
@Test
109126
public void deleteReplicaTest() {
110127
// delete replica1

0 commit comments

Comments
 (0)