Skip to content

Enhancement CBO (#2563) #2566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,68 +157,72 @@ public TiDAGRequest buildTiDAGReq(
TiTimestamp ts,
TiDAGRequest dagRequest) {

TiKVScanPlan minPlan = null;
TiKVScanPlan minCostPlan = null;
if (canUseTiKV) {
minPlan = buildTableScan(conditions, table, tableStatistics);
}
if (canUseTiFlash) {
// it is possible that only TiFlash plan exists due to isolation read.
TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
if (minPlan == null || plan.getCost() < minPlan.getCost()) {
minPlan = plan;
}
} else if (canUseTiKV && allowIndexScan) {
minPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
if (table.isPartitionEnabled()) {
// disable index scan
} else {
TiKVScanPlan minIndexPlan = null;
double minIndexCost = Double.MAX_VALUE;
for (TiIndexInfo index : table.getIndices()) {
if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
continue;
}
// tikv table scan
minCostPlan = buildTableScan(conditions, table, tableStatistics);
// tikv index scan
if (allowIndexScan) {
minCostPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
if (table.isPartitionEnabled()) {
// disable index scan
} else {
TiKVScanPlan minCostIndexPlan = null;
double minIndexCost = Double.MAX_VALUE;
for (TiIndexInfo index : table.getIndices()) {
if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
continue;
}

if (supportIndexScan(index, table)) {
TiKVScanPlan plan =
buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
if (plan.getCost() < minIndexCost) {
minIndexPlan = plan;
minIndexCost = plan.getCost();
if (supportIndexScan(index, table)) {
TiKVScanPlan plan =
buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
if (plan.getCost() < minIndexCost) {
minCostIndexPlan = plan;
minIndexCost = plan.getCost();
}
}
}
if (minCostIndexPlan != null
&& (minIndexCost < minCostPlan.getCost() || useIndexScanFirst)) {
minCostPlan = minCostIndexPlan;
}
}
if (minIndexPlan != null && (minIndexCost < minPlan.getCost() || useIndexScanFirst)) {
minPlan = minIndexPlan;
}
}
}
if (minPlan == null) {
if (canUseTiFlash) {
// it is possible that only TiFlash plan exists due to isolation read.
TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
if (minCostPlan == null || plan.getCost() < minCostPlan.getCost()) {
minCostPlan = plan;
}
}
if (minCostPlan == null) {
throw new TiClientInternalException(
"No valid plan found for table '" + table.getName() + "'");
}

TiStoreType minPlanStoreType = minPlan.getStoreType();
TiStoreType minCostPlanStoreType = minCostPlan.getStoreType();
// TiKV should not use CHBlock as Encode Type.
if (minPlanStoreType == TiStoreType.TiKV
if (minCostPlanStoreType == TiStoreType.TiKV
&& dagRequest.getEncodeType() == EncodeType.TypeCHBlock) {
dagRequest.setEncodeType(EncodeType.TypeChunk);
}
// Set DAG Request's store type as minPlan's store type.
dagRequest.setStoreType(minPlanStoreType);

dagRequest.addRanges(minPlan.getKeyRanges());
dagRequest.setPrunedParts(minPlan.getPrunedParts());
dagRequest.addFilters(new ArrayList<>(minPlan.getFilters()));
if (minPlan.isIndexScan()) {
dagRequest.setIndexInfo(minPlan.getIndex());
// Set DAG Request's store type as minCostPlan's store type.
dagRequest.setStoreType(minCostPlanStoreType);

dagRequest.addRanges(minCostPlan.getKeyRanges());
dagRequest.setPrunedParts(minCostPlan.getPrunedParts());
dagRequest.addFilters(new ArrayList<>(minCostPlan.getFilters()));
if (minCostPlan.isIndexScan()) {
dagRequest.setIndexInfo(minCostPlan.getIndex());
// need to set isDoubleRead to true for dagRequest in case of double read
dagRequest.setIsDoubleRead(minPlan.isDoubleRead());
dagRequest.setIsDoubleRead(minCostPlan.isDoubleRead());
}

dagRequest.setTableInfo(table);
dagRequest.setStartTs(ts);
dagRequest.setEstimatedCount(minPlan.getEstimatedRowCount());
dagRequest.setEstimatedCount(minCostPlan.getEstimatedRowCount());
return dagRequest;
}

Expand Down