@@ -157,68 +157,72 @@ public TiDAGRequest buildTiDAGReq(
157
157
TiTimestamp ts ,
158
158
TiDAGRequest dagRequest ) {
159
159
160
- TiKVScanPlan minPlan = null ;
160
+ TiKVScanPlan minCostPlan = null ;
161
161
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
+ }
181
176
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
+ }
188
184
}
189
185
}
186
+ if (minCostIndexPlan != null
187
+ && (minIndexCost < minCostPlan .getCost () || useIndexScanFirst )) {
188
+ minCostPlan = minCostIndexPlan ;
189
+ }
190
190
}
191
- if (minIndexPlan != null && (minIndexCost < minPlan .getCost () || useIndexScanFirst )) {
192
- minPlan = minIndexPlan ;
193
- }
194
191
}
195
192
}
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 ) {
197
201
throw new TiClientInternalException (
198
202
"No valid plan found for table '" + table .getName () + "'" );
199
203
}
200
204
201
- TiStoreType minPlanStoreType = minPlan .getStoreType ();
205
+ TiStoreType minCostPlanStoreType = minCostPlan .getStoreType ();
202
206
// TiKV should not use CHBlock as Encode Type.
203
- if (minPlanStoreType == TiStoreType .TiKV
207
+ if (minCostPlanStoreType == TiStoreType .TiKV
204
208
&& dagRequest .getEncodeType () == EncodeType .TypeCHBlock ) {
205
209
dagRequest .setEncodeType (EncodeType .TypeChunk );
206
210
}
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 ());
215
219
// need to set isDoubleRead to true for dagRequest in case of double read
216
- dagRequest .setIsDoubleRead (minPlan .isDoubleRead ());
220
+ dagRequest .setIsDoubleRead (minCostPlan .isDoubleRead ());
217
221
}
218
222
219
223
dagRequest .setTableInfo (table );
220
224
dagRequest .setStartTs (ts );
221
- dagRequest .setEstimatedCount (minPlan .getEstimatedRowCount ());
225
+ dagRequest .setEstimatedCount (minCostPlan .getEstimatedRowCount ());
222
226
return dagRequest ;
223
227
}
224
228
0 commit comments