Skip to content

Commit bfb0eef

Browse files
committed
Extract table/index metrics queries, add them to Engine test mocks
Signed-off-by: Rafer Hazen <[email protected]>
1 parent 64b606d commit bfb0eef

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

go/vt/vttablet/tabletserver/schema/db.go

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ SELECT f.name, i.UDF_RETURN_TYPE, f.type FROM mysql.func f left join performance
114114
`
115115
// fetchAggregateUdfs queries fetches all the aggregate user defined functions.
116116
fetchAggregateUdfs = `select function_name, function_return_type, function_type from %s.udfs`
117+
118+
// fetch a list of all partitions
119+
fetchPartitions = `select table_name, partition_name from information_schema.partitions where table_schema = database() and partition_name is not null`
120+
121+
// fetch the estimated number of rows and the clustered index byte size for all tables
122+
fetchTableRowCountClusteredIndex = `select table_name, n_rows, clustered_index_size * @@innodb_page_size from mysql.innodb_table_stats where database_name = database()`
123+
124+
// fetch the byte size of all indexes
125+
fetchIndexSizes = `select table_name, index_name, stat_value * @@innodb_page_size from mysql.innodb_index_stats where database_name = database() and stat_name = 'size'`
126+
127+
// fetch the cardinality of all indexes
128+
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`
117129
)
118130

119131
// reloadTablesDataInDB reloads teh tables information we have stored in our database we use for schema-tracking.

go/vt/vttablet/tabletserver/schema/engine.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
712712
partition string
713713
}
714714

715-
partitionsQuery := `
716-
select table_name, partition_name
717-
from information_schema.partitions
718-
where table_schema = database() and partition_name is not null
719-
`
720-
partitionsResults, err := conn.Exec(ctx, partitionsQuery, 8192*maxTableCount, false)
715+
partitionsResults, err := conn.Exec(ctx, fetchPartitions, 8192*maxTableCount, false)
721716
if err != nil {
722717
return err
723718
}
@@ -738,11 +733,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
738733
rowBytes int64
739734
}
740735
tables := make(map[string]table)
741-
tableStatsQuery := `
742-
select table_name, n_rows, clustered_index_size * @@innodb_page_size
743-
from mysql.innodb_table_stats where database_name = database()
744-
`
745-
tableStatsResults, err := conn.Exec(ctx, tableStatsQuery, maxTableCount*maxPartitionsPerTable, false)
736+
tableStatsResults, err := conn.Exec(ctx, fetchTableRowCountClusteredIndex, maxTableCount*maxPartitionsPerTable, false)
746737
if err != nil {
747738
return err
748739
}
@@ -775,12 +766,7 @@ func (se *Engine) updateTableIndexMetrics(ctx context.Context, conn *connpool.Co
775766
indexes := make(map[[2]string]index)
776767

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

810796
// Load index cardinalities. Results contain one row for every index (pre-aggregated across partitions).
811-
cardinalityQuery := `
812-
select table_name, index_name, max(cardinality)
813-
from information_schema.statistics s
814-
where table_schema = database()
815-
group by s.table_name, s.index_name
816-
`
817-
cardinalityResults, err := conn.Exec(ctx, cardinalityQuery, maxTableCount*maxPartitionsPerTable, false)
797+
cardinalityResults, err := conn.Exec(ctx, fetchIndexCardinalities, maxTableCount*maxPartitionsPerTable, false)
818798
if err != nil {
819799
return err
820800
}

go/vt/vttablet/tabletserver/schema/engine_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,10 @@ func TestGetTableForPos(t *testing.T) {
19001900
se.historian.enabled = false
19011901

19021902
addExpectedReloadQueries := func(db *fakesqldb.DB) {
1903+
db.AddQuery(fetchPartitions, &sqltypes.Result{})
1904+
db.AddQuery(fetchTableRowCountClusteredIndex, &sqltypes.Result{})
1905+
db.AddQuery(fetchIndexSizes, &sqltypes.Result{})
1906+
db.AddQuery(fetchIndexCardinalities, &sqltypes.Result{})
19031907
db.AddQuery("SELECT UNIX_TIMESTAMP()", sqltypes.MakeTestResult(sqltypes.MakeTestFields(
19041908
"UNIX_TIMESTAMP()",
19051909
"int64"),

0 commit comments

Comments
 (0)