Skip to content

Commit

Permalink
executor: display Cardinality info from stats for SHOW INDEX (#42441)
Browse files Browse the repository at this point in the history
close #42227
  • Loading branch information
winoros authored Apr 10, 2023
1 parent c282c9c commit c233969
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,16 @@ func (e *ShowExec) fetchShowColumns(ctx context.Context) error {
}

func (e *ShowExec) fetchShowIndex() error {
do := domain.GetDomain(e.ctx)
h := do.StatsHandle()

tb, err := e.getTable()
if err != nil {
return errors.Trace(err)
}

statsTbl := h.GetTableStats(tb.Meta())

checker := privilege.GetPrivilegeManager(e.ctx)
activeRoles := e.ctx.GetSessionVars().ActiveRoles
if checker != nil && e.ctx.GetSessionVars().User != nil && !checker.RequestVerification(activeRoles, e.DBName.O, tb.Meta().Name.O, "", mysql.AllPrivMask) {
Expand All @@ -739,14 +744,19 @@ func (e *ShowExec) fetchShowIndex() error {
break
}
}
colStats, ok := statsTbl.Columns[pkCol.ID]
var ndv int64
if ok {
ndv = colStats.NDV
}
e.appendRow([]interface{}{
tb.Meta().Name.O, // Table
0, // Non_unique
"PRIMARY", // Key_name
1, // Seq_in_index
pkCol.Name.O, // Column_name
"A", // Collation
0, // Cardinality
ndv, // Cardinality
nil, // Sub_part
nil, // Packed
"", // Null
Expand Down Expand Up @@ -796,14 +806,20 @@ func (e *ShowExec) fetchShowIndex() error {
expression = tblCol.GeneratedExprString
}

colStats, ok := statsTbl.Columns[tblCol.ID]
var ndv int64
if ok {
ndv = colStats.NDV
}

e.appendRow([]interface{}{
tb.Meta().Name.O, // Table
nonUniq, // Non_unique
idx.Meta().Name.O, // Key_name
i + 1, // Seq_in_index
colName, // Column_name
"A", // Collation
0, // Cardinality
ndv, // Cardinality
subPart, // Sub_part
nil, // Packed
nullVal, // Null
Expand Down

0 comments on commit c233969

Please sign in to comment.