Skip to content

Commit

Permalink
Extract table/index metrics queries, add them to Engine test mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Rafer Hazen <rafer@ralua.com>
  • Loading branch information
rafer committed Jan 21, 2025
1 parent 64b606d commit bfb0eef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
12 changes: 12 additions & 0 deletions go/vt/vttablet/tabletserver/schema/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ SELECT f.name, i.UDF_RETURN_TYPE, f.type FROM mysql.func f left join performance
`
// fetchAggregateUdfs queries fetches all the aggregate user defined functions.
fetchAggregateUdfs = `select function_name, function_return_type, function_type from %s.udfs`

// fetch a list of all partitions
fetchPartitions = `select table_name, partition_name from information_schema.partitions where table_schema = database() and partition_name is not null`

// fetch the estimated number of rows and the clustered index byte size for all tables
fetchTableRowCountClusteredIndex = `select table_name, n_rows, clustered_index_size * @@innodb_page_size from mysql.innodb_table_stats where database_name = database()`

// fetch the byte size of all indexes
fetchIndexSizes = `select table_name, index_name, stat_value * @@innodb_page_size from mysql.innodb_index_stats where database_name = database() and stat_name = 'size'`

// fetch the cardinality of all indexes
fetchIndexCardinalities = `select table_name, index_name, max(cardinality) from information_schema.statistics s where table_schema = database() group by s.table_name, s.index_name`
)

// reloadTablesDataInDB reloads teh tables information we have stored in our database we use for schema-tracking.
Expand Down
28 changes: 4 additions & 24 deletions go/vt/vttablet/tabletserver/schema/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
partition string
}

partitionsQuery := `
select table_name, partition_name
from information_schema.partitions
where table_schema = database() and partition_name is not null
`
partitionsResults, err := conn.Exec(ctx, partitionsQuery, 8192*maxTableCount, false)
partitionsResults, err := conn.Exec(ctx, fetchPartitions, 8192*maxTableCount, false)
if err != nil {
return err
}
Expand All @@ -738,11 +733,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
rowBytes int64
}
tables := make(map[string]table)
tableStatsQuery := `
select table_name, n_rows, clustered_index_size * @@innodb_page_size
from mysql.innodb_table_stats where database_name = database()
`
tableStatsResults, err := conn.Exec(ctx, tableStatsQuery, maxTableCount*maxPartitionsPerTable, false)
tableStatsResults, err := conn.Exec(ctx, fetchTableRowCountClusteredIndex, maxTableCount*maxPartitionsPerTable, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -775,12 +766,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
indexes := make(map[[2]string]index)

// Load the byte sizes of all indexes. Results contain one row for every index/partition combination.
bytesQuery := `
select table_name, index_name, stat_value * @@innodb_page_size
from mysql.innodb_index_stats
where database_name = database() and stat_name = 'size';
`
bytesResults, err := conn.Exec(ctx, bytesQuery, maxTableCount*maxIndexesPerTable, false)
bytesResults, err := conn.Exec(ctx, fetchIndexSizes, maxTableCount*maxIndexesPerTable, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -808,13 +794,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
}

// Load index cardinalities. Results contain one row for every index (pre-aggregated across partitions).
cardinalityQuery := `
select table_name, index_name, max(cardinality)
from information_schema.statistics s
where table_schema = database()
group by s.table_name, s.index_name
`
cardinalityResults, err := conn.Exec(ctx, cardinalityQuery, maxTableCount*maxPartitionsPerTable, false)
cardinalityResults, err := conn.Exec(ctx, fetchIndexCardinalities, maxTableCount*maxPartitionsPerTable, false)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go/vt/vttablet/tabletserver/schema/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,10 @@ func TestGetTableForPos(t *testing.T) {
se.historian.enabled = false

addExpectedReloadQueries := func(db *fakesqldb.DB) {
db.AddQuery(fetchPartitions, &sqltypes.Result{})
db.AddQuery(fetchTableRowCountClusteredIndex, &sqltypes.Result{})
db.AddQuery(fetchIndexSizes, &sqltypes.Result{})
db.AddQuery(fetchIndexCardinalities, &sqltypes.Result{})
db.AddQuery("SELECT UNIX_TIMESTAMP()", sqltypes.MakeTestResult(sqltypes.MakeTestFields(
"UNIX_TIMESTAMP()",
"int64"),
Expand Down

0 comments on commit bfb0eef

Please sign in to comment.