Skip to content

Commit e6fd049

Browse files
authored
Enhancement CBO (#2563) (#2566)
1 parent 5564a07 commit e6fd049

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

tikv-client/src/main/java/com/pingcap/tikv/predicates/TiKVScanAnalyzer.java

+46-42
Original file line numberDiff line numberDiff line change
@@ -157,68 +157,72 @@ public TiDAGRequest buildTiDAGReq(
157157
TiTimestamp ts,
158158
TiDAGRequest dagRequest) {
159159

160-
TiKVScanPlan minPlan = null;
160+
TiKVScanPlan minCostPlan = null;
161161
if (canUseTiKV) {
162-
minPlan = buildTableScan(conditions, table, tableStatistics);
163-
}
164-
if (canUseTiFlash) {
165-
// it is possible that only TiFlash plan exists due to isolation read.
166-
TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
167-
if (minPlan == null || plan.getCost() < minPlan.getCost()) {
168-
minPlan = plan;
169-
}
170-
} else if (canUseTiKV && allowIndexScan) {
171-
minPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
172-
if (table.isPartitionEnabled()) {
173-
// disable index scan
174-
} else {
175-
TiKVScanPlan minIndexPlan = null;
176-
double minIndexCost = Double.MAX_VALUE;
177-
for (TiIndexInfo index : table.getIndices()) {
178-
if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
179-
continue;
180-
}
162+
// tikv table scan
163+
minCostPlan = buildTableScan(conditions, table, tableStatistics);
164+
// tikv index scan
165+
if (allowIndexScan) {
166+
minCostPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
167+
if (table.isPartitionEnabled()) {
168+
// disable index scan
169+
} else {
170+
TiKVScanPlan minCostIndexPlan = null;
171+
double minIndexCost = Double.MAX_VALUE;
172+
for (TiIndexInfo index : table.getIndices()) {
173+
if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
174+
continue;
175+
}
181176

182-
if (supportIndexScan(index, table)) {
183-
TiKVScanPlan plan =
184-
buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
185-
if (plan.getCost() < minIndexCost) {
186-
minIndexPlan = plan;
187-
minIndexCost = plan.getCost();
177+
if (supportIndexScan(index, table)) {
178+
TiKVScanPlan plan =
179+
buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
180+
if (plan.getCost() < minIndexCost) {
181+
minCostIndexPlan = plan;
182+
minIndexCost = plan.getCost();
183+
}
188184
}
189185
}
186+
if (minCostIndexPlan != null
187+
&& (minIndexCost < minCostPlan.getCost() || useIndexScanFirst)) {
188+
minCostPlan = minCostIndexPlan;
189+
}
190190
}
191-
if (minIndexPlan != null && (minIndexCost < minPlan.getCost() || useIndexScanFirst)) {
192-
minPlan = minIndexPlan;
193-
}
194191
}
195192
}
196-
if (minPlan == null) {
193+
if (canUseTiFlash) {
194+
// it is possible that only TiFlash plan exists due to isolation read.
195+
TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
196+
if (minCostPlan == null || plan.getCost() < minCostPlan.getCost()) {
197+
minCostPlan = plan;
198+
}
199+
}
200+
if (minCostPlan == null) {
197201
throw new TiClientInternalException(
198202
"No valid plan found for table '" + table.getName() + "'");
199203
}
200204

201-
TiStoreType minPlanStoreType = minPlan.getStoreType();
205+
TiStoreType minCostPlanStoreType = minCostPlan.getStoreType();
202206
// TiKV should not use CHBlock as Encode Type.
203-
if (minPlanStoreType == TiStoreType.TiKV
207+
if (minCostPlanStoreType == TiStoreType.TiKV
204208
&& dagRequest.getEncodeType() == EncodeType.TypeCHBlock) {
205209
dagRequest.setEncodeType(EncodeType.TypeChunk);
206210
}
207-
// Set DAG Request's store type as minPlan's store type.
208-
dagRequest.setStoreType(minPlanStoreType);
209-
210-
dagRequest.addRanges(minPlan.getKeyRanges());
211-
dagRequest.setPrunedParts(minPlan.getPrunedParts());
212-
dagRequest.addFilters(new ArrayList<>(minPlan.getFilters()));
213-
if (minPlan.isIndexScan()) {
214-
dagRequest.setIndexInfo(minPlan.getIndex());
211+
// Set DAG Request's store type as minCostPlan's store type.
212+
dagRequest.setStoreType(minCostPlanStoreType);
213+
214+
dagRequest.addRanges(minCostPlan.getKeyRanges());
215+
dagRequest.setPrunedParts(minCostPlan.getPrunedParts());
216+
dagRequest.addFilters(new ArrayList<>(minCostPlan.getFilters()));
217+
if (minCostPlan.isIndexScan()) {
218+
dagRequest.setIndexInfo(minCostPlan.getIndex());
215219
// need to set isDoubleRead to true for dagRequest in case of double read
216-
dagRequest.setIsDoubleRead(minPlan.isDoubleRead());
220+
dagRequest.setIsDoubleRead(minCostPlan.isDoubleRead());
217221
}
218222

219223
dagRequest.setTableInfo(table);
220224
dagRequest.setStartTs(ts);
221-
dagRequest.setEstimatedCount(minPlan.getEstimatedRowCount());
225+
dagRequest.setEstimatedCount(minCostPlan.getEstimatedRowCount());
222226
return dagRequest;
223227
}
224228

0 commit comments

Comments
 (0)