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: Use realtimeRowCount when all topN collected #56848

Merged
merged 7 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 11 additions & 1 deletion pkg/planner/cardinality/row_count_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cardinality
terry1purcell marked this conversation as resolved.
Show resolved Hide resolved

import (
"math"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/planner/planctx"
"github.com/pingcap/tidb/pkg/planner/util/debugtrace"
Expand Down Expand Up @@ -177,7 +179,15 @@ func equalRowCountOnColumn(sctx planctx.PlanContext, c *statistics.Column, val t
if modifyCount == 0 {
return 0, nil
}
return 1, nil
// Reset to the original NDV, or if no NDV - derive an NDV using sqrt
if c.Histogram.NDV > 0 {
histNDV = float64(c.Histogram.NDV)
} else {
histNDV = math.Sqrt(max(c.Histogram.NotNullCount(), float64(modifyCount)))
}
// Return the min of the original notNullCount or the modifyCount/NDV. This is to reduce the
// risk of too large or too small an estimate if modifyCount is large or NotNullCount is small
return max(1, min(c.Histogram.NotNullCount(), float64(modifyCount)/histNDV)), nil
terry1purcell marked this conversation as resolved.
Show resolved Hide resolved
}
return c.Histogram.NotNullCount() / histNDV, nil
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/planner/cardinality/row_count_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,15 @@ func equalRowCountOnIndex(sctx planctx.PlanContext, idx *statistics.Index, b []b
if modifyCount == 0 {
return 0
}
return 1
// Reset to the original NDV, or if no NDV - derive an NDV using sqrt
if idx.Histogram.NDV > 0 {
histNDV = float64(idx.Histogram.NDV)
} else {
histNDV = math.Sqrt(max(idx.Histogram.NotNullCount(), float64(modifyCount)))
}
// Return the min of the original notNullCount or the modifyCount/NDV. This is to reduce the
// risk of too large or too small an estimate if modifyCount is large or NotNullCount is small
return max(1, min(idx.Histogram.NotNullCount(), float64(modifyCount)/histNDV))
}
return idx.Histogram.NotNullCount() / histNDV
}
Expand Down