Skip to content
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

planner: out of range for sampling | tidb-test=pr/2381 #55512

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
10 changes: 7 additions & 3 deletions pkg/planner/cardinality/row_count_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,19 @@ func getIndexRowCountForStatsV2(sctx planctx.PlanContext, idx *statistics.Index,
// Exclude the TopN in Stats Version 2
if idx.StatsVer == statistics.Version2 {
c := coll.GetCol(idx.Histogram.ID)
// If this is single column of a multi-column index - use the column's NDV rather than index NDV
// If this is single column predicate - use the column's information rather than index.
// Index histograms are converted to string. Column uses original type - which can be more accurate for out of range estimation.
isSingleColRange := len(indexRange.LowVal) == len(indexRange.HighVal) && len(indexRange.LowVal) == 1
if isSingleColRange && !isSingleColIdx && c != nil && c.Histogram.NDV > 0 {
if isSingleColRange && c != nil && c.Histogram.NDV > 0 {
histNDV = c.Histogram.NDV - int64(c.TopN.Num())
count += c.Histogram.OutOfRangeRowCount(sctx, &indexRange.LowVal[0], &indexRange.HighVal[0], modifyCount, histNDV, increaseFactor)
} else {
histNDV -= int64(idx.TopN.Num())
count += idx.Histogram.OutOfRangeRowCount(sctx, &l, &r, modifyCount, histNDV, increaseFactor)
}
} else {
count += idx.Histogram.OutOfRangeRowCount(sctx, &l, &r, modifyCount, histNDV, increaseFactor)
}
count += idx.Histogram.OutOfRangeRowCount(sctx, &l, &r, modifyCount, histNDV, increaseFactor)
}

if debugTrace {
Expand Down
6 changes: 3 additions & 3 deletions pkg/planner/cardinality/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ func TestEstimationForUnknownValues(t *testing.T) {

count, err = cardinality.GetRowCountByColumnRanges(sctx, &statsTbl.HistColl, colID, getRange(9, 30))
require.NoError(t, err)
require.Equal(t, 7.2, count)
require.Equal(t, 7.7, count)

count, err = cardinality.GetRowCountByColumnRanges(sctx, &statsTbl.HistColl, colID, getRange(9, math.MaxInt64))
require.NoError(t, err)
require.Equal(t, 7.2, count)
require.Equal(t, 7.7, count)

idxID := table.Meta().Indices[0].ID
count, err = cardinality.GetRowCountByIndexRanges(sctx, &statsTbl.HistColl, idxID, getRange(30, 30))
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestSelectivity(t *testing.T) {
},
{
exprs: "a >= 1 and b > 1 and a < 2",
selectivity: 0.017832647462277088,
selectivity: 0.018175582990397805,
selectivityAfterIncrease: 0.018518518518518517,
},
{
Expand Down
Loading