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, workload-based learning: Add common analyze table cost steps in producer part #58930

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion pkg/workloadbasedlearning/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "workloadbasedlearning",
srcs = ["handle.go"],
srcs = [
"handle.go",
"metrics.go",
],
importpath = "github.com/pingcap/tidb/pkg/workloadbasedlearning",
visibility = ["//visibility:public"],
)
57 changes: 56 additions & 1 deletion pkg/workloadbasedlearning/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,61 @@ func NewWorkloadBasedLearningHandle() *Handle {
}

// HandleReadTableCost Start a new round of analysis of all historical read queries.
func (Handle *Handle) HandleReadTableCost() {
// According to abstracted table cost metrics, calculate the percentage of read scan time and memory usage for each table.
// The result will be saved to the table "mysql.workload_values".
// Dataflow
// 1. Abstract middle table cost metrics(scan time, memory usage, read frequency)
// from every record in statement_summary/statement_stats
//
// 2,3. Group by tablename, get the total scan time, total memory usage, and every table scan time, memory usage,
//
// read frequency
//
// 4. Calculate table cost for each table, table cost = table scan time / total scan time + table mem usage / total mem usage
// 5. Save all table cost metrics[per table](scan time, table cost, etc) to table "mysql.workload_values"
func (handle *Handle) HandleReadTableCost() {
// step1: abstract middle table cost metrics from every record in statement_summary
middleMetrics := handle.analyzeBasedOnStatementSummary()
if middleMetrics == nil || len(middleMetrics) == 0 {
return
}
// step2: group by tablename, sum(table-scan-time), sum(table-mem-usage), sum(read-frequency)
// step3: calculate the total scan time and total memory usage
tableNameToMetrics := make(map[string]*ReadTableCostMetrics)
totalScanTime := 0.0
totalMemUsage := 0.0
for _, middleMetric := range middleMetrics {
metric, ok := tableNameToMetrics[middleMetric.tableName]
if !ok {
tableNameToMetrics[middleMetric.tableName] = middleMetric
} else {
metric.tableScanTime += middleMetric.tableScanTime * float64(middleMetric.readFrequency)
metric.tableMemUsage += middleMetric.tableMemUsage * float64(middleMetric.readFrequency)
metric.readFrequency += middleMetric.readFrequency
}
totalScanTime += middleMetric.tableScanTime
totalMemUsage += middleMetric.tableMemUsage
}
if totalScanTime == 0 || totalMemUsage == 0 {
return
}
// step4: calculate the percentage of scan time and memory usage for each table
for _, metric := range tableNameToMetrics {
metric.tableCost = metric.tableScanTime/totalScanTime + metric.tableMemUsage/totalMemUsage
}
// TODO step5: save the table cost metrics to table "mysql.workload_values"

}

func (handle *Handle) analyzeBasedOnStatementSummary() []*ReadTableCostMetrics {
// step1: get all record from statement_summary
// step2: abstract table cost metrics from each record
return nil
}

// TODO
func (handle *Handle) analyzeBasedOnStatementStats() []*ReadTableCostMetrics {
// step1: get all record from statement_stats
// step2: abstract table cost metrics from each record
return nil
}
28 changes: 28 additions & 0 deletions pkg/workloadbasedlearning/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package workloadbasedlearning

type ReadTableCostMetrics struct {
tableName string
// tableScanTime[t] = sum(scan-time * readFrequency) of all records in statement_summary where table-name = t
tableScanTime float64
// tableMemUsage[t] = sum(mem-usage * readFrequency) of all records in statement_summary where table-name = t
tableMemUsage float64
// readFrequency[t] = sum(read-frequency) of all records in statement_summary where table-name = t
readFrequency int64
// tableCost[t] = tableScanTime[t] / totalScanTime + tableMemUsage[t] / totalMemUsage
// range between 0 ~ 2
tableCost float64
}