@@ -309,22 +309,24 @@ func getCostedIndexScan(ctx *sql.Context, statsProv sql.StatsProvider, rt sql.Ta
309309 retFilters = b .leftover
310310 }
311311
312- bestStat , err := c .bestStat .WithHistogram (c .bestHist )
313- if err != nil {
314- return nil , nil , nil , err
315- }
316- if c .bestStat .FuncDeps ().HasMax1Row () && lookup .Ranges .Len () == 1 {
317- bestStat = bestStat .WithRowCount (1 ).WithDistinctCount (1 )
318- if ! qFlags .JoinIsSet () && ! qFlags .SubqueryIsSet () {
319- // Strict index lookup without a join or subquery scope will return
320- // at most one row. We could also use some sort of scope counting
321- // to check for single scope.
322- qFlags .Set (sql .QFlagMax1Row )
323- }
312+ var bestStat sql.Statistic
313+ if c .bestStat .FuncDeps ().HasMax1Row () {
314+ bestStat = c .bestStat .WithRowCount (1 ).WithDistinctCount (1 )
324315 } else {
316+ bestStat , err = c .bestStat .WithHistogram (c .bestHist )
317+ if err != nil {
318+ return nil , nil , nil , err
319+ }
325320 bestStat = stats .UpdateCounts (bestStat )
326321 }
327322
323+ if bestStat .FuncDeps ().HasMax1Row () && ! qFlags .JoinIsSet () && ! qFlags .SubqueryIsSet () && lookup .Ranges .Len () == 1 {
324+ // Strict index lookup without a join or subquery scope will return
325+ // at most one row. We could also use some sort of scope counting
326+ // to check for single scope.
327+ qFlags .Set (sql .QFlagMax1Row )
328+ }
329+
328330 return ret , bestStat , retFilters , nil
329331}
330332
0 commit comments