From b7a70010d4ebe561f65344f626ec5424281c8d44 Mon Sep 17 00:00:00 2001 From: Ruoxi Sun Date: Wed, 14 Aug 2024 20:49:17 +0800 Subject: [PATCH 1/3] WIP --- cmd/go-tpc/tpch.go | 30 +++++++++++++++++++++++------- tpch/workload.go | 8 +------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/go-tpc/tpch.go b/cmd/go-tpc/tpch.go index 256e2a4..8905a1c 100644 --- a/cmd/go-tpc/tpch.go +++ b/cmd/go-tpc/tpch.go @@ -14,7 +14,24 @@ import ( var tpchConfig tpch.Config +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 + func executeTpch(action string) { + if tpchConfig.EnableQueryTuning { + } + openDB() defer closeDB() @@ -34,7 +51,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() @@ -131,14 +147,14 @@ 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") + // 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.") var cmdCleanup = &cobra.Command{ Use: "cleanup", diff --git a/tpch/workload.go b/tpch/workload.go index 48d7cb3..48d28b9 100644 --- a/tpch/workload.go +++ b/tpch/workload.go @@ -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 @@ -51,7 +45,7 @@ type Config struct { PlanReplayerConfig replayer.PlanReplayerConfig EnablePlanReplayer bool - QueryTuningConfig queryTuningConfig + EnableQueryTuning bool // for prepare command only OutputType string From 57ea31cee4084610269fe1133af3d2f3671f270f Mon Sep 17 00:00:00 2001 From: Ruoxi Sun Date: Mon, 19 Aug 2024 17:13:28 +0800 Subject: [PATCH 2/3] Address comments --- cmd/go-tpc/tpch.go | 22 ++++++++++++++-------- tpch/workload.go | 16 ---------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/cmd/go-tpc/tpch.go b/cmd/go-tpc/tpch.go index 8905a1c..fb2c6a4 100644 --- a/cmd/go-tpc/tpch.go +++ b/cmd/go-tpc/tpch.go @@ -14,8 +14,8 @@ import ( var tpchConfig tpch.Config -queryTuningVars := []struct { - name string +var queryTuningVars = []struct { + name string value string }{ // For optimal join order, esp. for q9. @@ -26,12 +26,22 @@ queryTuningVars := []struct { {"tidb_prefer_broadcast_join_by_exchange_data_size", "ON"}, } -func +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 tpchConfig.EnableQueryTuning { + if action == "run" && driver == "mysql" && tpchConfig.EnableQueryTuning { + appendQueryTuningVarsToConnParams() } + fmt.Println("Conn params: ") + fmt.Println(connParams) + openDB() defer closeDB() @@ -151,10 +161,6 @@ func registerTpch(root *cobra.Command) { "enable-query-tuning", true, "Tune queries by setting some session variables known effective for tpch") - // 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.") var cmdCleanup = &cobra.Command{ Use: "cleanup", diff --git a/tpch/workload.go b/tpch/workload.go index 48d28b9..6dd0926 100644 --- a/tpch/workload.go +++ b/tpch/workload.go @@ -199,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) @@ -221,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 From fb50da67b49c9940f62422c1b342399744ab2545 Mon Sep 17 00:00:00 2001 From: Ruoxi Sun Date: Mon, 19 Aug 2024 17:16:18 +0800 Subject: [PATCH 3/3] Remove debug code --- cmd/go-tpc/tpch.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/go-tpc/tpch.go b/cmd/go-tpc/tpch.go index fb2c6a4..6547d98 100644 --- a/cmd/go-tpc/tpch.go +++ b/cmd/go-tpc/tpch.go @@ -39,9 +39,6 @@ func executeTpch(action string) { appendQueryTuningVarsToConnParams() } - fmt.Println("Conn params: ") - fmt.Println(connParams) - openDB() defer closeDB()