From bfb0eef45327fc9d2fa87cc30eab1f5aaed7ce22 Mon Sep 17 00:00:00 2001 From: Rafer Hazen Date: Tue, 21 Jan 2025 15:13:25 -0700 Subject: [PATCH] Extract table/index metrics queries, add them to Engine test mocks Signed-off-by: Rafer Hazen --- go/vt/vttablet/tabletserver/schema/db.go | 12 ++++++++ go/vt/vttablet/tabletserver/schema/engine.go | 28 +++---------------- .../tabletserver/schema/engine_test.go | 4 +++ 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/go/vt/vttablet/tabletserver/schema/db.go b/go/vt/vttablet/tabletserver/schema/db.go index 7b328c518d2..4731ae3f3ed 100644 --- a/go/vt/vttablet/tabletserver/schema/db.go +++ b/go/vt/vttablet/tabletserver/schema/db.go @@ -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. diff --git a/go/vt/vttablet/tabletserver/schema/engine.go b/go/vt/vttablet/tabletserver/schema/engine.go index 54803af9589..216c0ae9da9 100644 --- a/go/vt/vttablet/tabletserver/schema/engine.go +++ b/go/vt/vttablet/tabletserver/schema/engine.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/go/vt/vttablet/tabletserver/schema/engine_test.go b/go/vt/vttablet/tabletserver/schema/engine_test.go index 3faa7a10554..db7c56d7f1b 100644 --- a/go/vt/vttablet/tabletserver/schema/engine_test.go +++ b/go/vt/vttablet/tabletserver/schema/engine_test.go @@ -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"),