Skip to content

Commit

Permalink
Merge pull request #1 from zanmato1984/tpch-tune-2
Browse files Browse the repository at this point in the history
Tpch tune 2
  • Loading branch information
zanmato1984 authored Aug 19, 2024
2 parents 3c4129c + fb50da6 commit 8c16898
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
33 changes: 26 additions & 7 deletions cmd/go-tpc/tpch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ import (

var tpchConfig tpch.Config

var queryTuningVars = []struct {
name string
value string
}{
// For optimal join order, esp. for q9.
{"tidb_default_string_match_selectivity", "0.1"},
// For optimal join order for all queries.
{"tidb_opt_join_reorder_threshold", "60"},
// For optimal join type between broadcast and hash partition join.
{"tidb_prefer_broadcast_join_by_exchange_data_size", "ON"},
}

func appendQueryTuningVarsToConnParams() {
for _, v := range queryTuningVars {
if !strings.Contains(connParams, v.name) {
connParams = fmt.Sprintf("%s&%s=%s", connParams, v.name, v.value)
}
}
}

func executeTpch(action string) {
if action == "run" && driver == "mysql" && tpchConfig.EnableQueryTuning {
appendQueryTuningVarsToConnParams()
}

openDB()
defer closeDB()

Expand All @@ -34,7 +58,6 @@ func executeTpch(action string) {
tpchConfig.DBName = dbName
tpchConfig.PrepareThreads = threads
tpchConfig.QueryNames = strings.Split(tpchConfig.RawQueries, ",")
tpchConfig.QueryTuningConfig.Vars = strings.Split(tpchConfig.QueryTuningConfig.VarsRaw, ";")
w := tpch.NewWorkloader(globalDB, &tpchConfig)
timeoutCtx, cancel := context.WithTimeout(globalCtx, totalTime)
defer cancel()
Expand Down Expand Up @@ -131,14 +154,10 @@ func registerTpch(root *cobra.Command) {
"",
"Name of plan Replayer file dumps")

cmdRun.PersistentFlags().BoolVar(&tpchConfig.QueryTuningConfig.Enable,
cmdRun.PersistentFlags().BoolVar(&tpchConfig.EnableQueryTuning,
"enable-query-tuning",
true,
"Enable query tuning by setting specified session variables")
cmdRun.PersistentFlags().StringVar(&tpchConfig.QueryTuningConfig.VarsRaw,
"query-tuning-vars",
"tidb_default_string_match_selectivity=0.1;tidb_opt_join_reorder_threshold=60;tidb_prefer_broadcast_join_by_exchange_data_size=ON",
"Specify a sequence of session variables to set before executing each query, in the form of 'name=value', separated by semicolon. Defaulted to some variables known effective for tpch queries.")
"Tune queries by setting some session variables known effective for tpch")

var cmdCleanup = &cobra.Command{
Use: "cleanup",
Expand Down
24 changes: 1 addition & 23 deletions tpch/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ type analyzeConfig struct {
IndexSerialScanConcurrency int
}

type queryTuningConfig struct {
Enable bool
VarsRaw string
Vars []string
}

// Config is the configuration for tpch workload
type Config struct {
Driver string
Expand All @@ -51,7 +45,7 @@ type Config struct {
PlanReplayerConfig replayer.PlanReplayerConfig
EnablePlanReplayer bool

QueryTuningConfig queryTuningConfig
EnableQueryTuning bool

// for prepare command only
OutputType string
Expand Down Expand Up @@ -205,18 +199,6 @@ func (w *Workloader) CheckPrepare(ctx context.Context, threadID int) error {
return nil
}

func (w *Workloader) setQueryTuningVars(ctx context.Context) error {
if w.cfg.QueryTuningConfig.Enable && w.cfg.Driver == "mysql" {
conn := w.getState(ctx).Conn
for _, v := range w.cfg.QueryTuningConfig.Vars {
if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET @@session.%s", v)); err != nil {
return err
}
}
}
return nil
}

// Run runs workload
func (w *Workloader) Run(ctx context.Context, threadID int) error {
s := w.getState(ctx)
Expand All @@ -227,10 +209,6 @@ func (w *Workloader) Run(ctx context.Context, threadID int) error {
}
}

if err := w.setQueryTuningVars(ctx); err != nil {
return fmt.Errorf("set query tuning variables failed %v", err)
}

queryName := w.cfg.QueryNames[s.queryIdx%len(w.cfg.QueryNames)]
query := query(w.cfg.Driver, queryName)
// only for driver == mysql and EnablePlanReplayer == true
Expand Down

0 comments on commit 8c16898

Please sign in to comment.