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

*: improve performance of show variables #5363

Merged
merged 5 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (t basicCtxType) String() string {
const (
// QueryString is the key for original query string.
QueryString basicCtxType = 1
// Initing is the key for indicating if the server is running bootstrap or upgrad job.
// Initing is the key for indicating if the server is running bootstrap or upgrade job.
Initing basicCtxType = 2
// LastExecuteDDL is the key for whether the session execute a ddl command last time.
LastExecuteDDL basicCtxType = 3
Expand Down
3 changes: 2 additions & 1 deletion ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ type testForeighKeySuite struct {
}

func (s *testForeighKeySuite) SetUpSuite(c *C) {
testleak.BeforeTest()
s.store = testCreateStore(c, "test_foreign")
}

func (s *testForeighKeySuite) TearDownSuite(c *C) {
err := s.store.Close()
c.Assert(err, IsNil)
testleak.AfterTest(c)()
}

func (s *testForeighKeySuite) testCreateForeignKey(c *C, tblInfo *model.TableInfo, fkName string, keys []string, refTable string, refKeys []string, onDelete ast.ReferOptionType, onUpdate ast.ReferOptionType) *model.Job {
Expand Down Expand Up @@ -110,7 +112,6 @@ func getForeignKey(t table.Table, name string) *model.FKInfo {
}

func (s *testForeighKeySuite) TestForeignKey(c *C) {
defer testleak.AfterTest(c)()
d := testNewDDL(goctx.Background(), nil, s.store, nil, nil, testLease)
defer d.Stop()
s.d = d
Expand Down
41 changes: 34 additions & 7 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,23 +358,50 @@ func (e *ShowExec) fetchShowCharset() error {
return nil
}

func (e *ShowExec) fetchShowVariables() error {
sessionVars := e.ctx.GetSessionVars()
func (e *ShowExec) fetchShowVariables() (err error) {
var (
value string
ok bool
sessionVars = e.ctx.GetSessionVars()
unreachedVars = make([]string, 0, len(variable.SysVars))
)
for _, v := range variable.SysVars {
var err error
var value string
if !e.GlobalScope {
// Try to get Session Scope variable value first.
value, err = varsutil.GetSessionSystemVar(sessionVars, v.Name)
// For a session scope variable,
// 1. try to fetch value from SessionVars.Systems;
// 2. if this variable is session-only, fetch value from SysVars
// otherwise, fetch the value from table `mysql.Global_Variables`.
value, ok, err = varsutil.GetSessionOnlySysVars(sessionVars, v.Name)
} else {
value, err = varsutil.GetGlobalSystemVar(sessionVars, v.Name)
// If the scope of a system variable is ScopeNone,
// it's a read-only variable, so we return the default value of it.
// Otherwise, we have to fetch the values from table `mysql.Global_Variables` for global variable names.
value, ok, err = varsutil.GetScopeNoneSystemVar(v.Name)
}
if err != nil {
return errors.Trace(err)
}
if !ok {
unreachedVars = append(unreachedVars, v.Name)
continue
}
row := types.MakeDatums(v.Name, value)
e.rows = append(e.rows, row)
}
if len(unreachedVars) != 0 {
systemVars, err := sessionVars.GlobalVarsAccessor.GetAllSysVars()
if err != nil {
return errors.Trace(err)
}
for _, varName := range unreachedVars {
varValue, ok := systemVars[varName]
if !ok {
varValue = variable.SysVars[varName].Value
}
row := types.MakeDatums(varName, varValue)
e.rows = append(e.rows, row)
}
}
return nil
}

Expand Down
117 changes: 24 additions & 93 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *testIntegrationSuite) cleanEnv(c *C) {

func (s *testIntegrationSuite) SetUpSuite(c *C) {
var err error
testleak.BeforeTest()
s.store, s.dom, err = newStoreWithBootstrap()
c.Assert(err, IsNil)
s.ctx = mock.NewContext()
Expand All @@ -67,14 +68,12 @@ func (s *testIntegrationSuite) SetUpSuite(c *C) {
func (s *testIntegrationSuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
}

func (s *testIntegrationSuite) TestFuncREPEAT(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk.MustExec("USE test;")
tk.MustExec("DROP TABLE IF EXISTS table_string;")
tk.MustExec("CREATE TABLE table_string(a CHAR(20), b VARCHAR(20), c TINYTEXT, d TEXT(20), e MEDIUMTEXT, f LONGTEXT, g BIGINT);")
Expand Down Expand Up @@ -108,10 +107,7 @@ func (s *testIntegrationSuite) TestFuncREPEAT(c *C) {

func (s *testIntegrationSuite) TestFuncLpadAndRpad(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk.MustExec(`USE test;`)
tk.MustExec(`DROP TABLE IF EXISTS t;`)
tk.MustExec(`CREATE TABLE t(a BINARY(10), b CHAR(10));`)
Expand All @@ -131,10 +127,7 @@ func (s *testIntegrationSuite) TestFuncLpadAndRpad(c *C) {
}

func (s *testIntegrationSuite) TestMiscellaneousBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -213,10 +206,7 @@ func (s *testIntegrationSuite) TestMiscellaneousBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestConvertToBit(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1")
Expand All @@ -242,10 +232,7 @@ func (s *testIntegrationSuite) TestConvertToBit(c *C) {
}

func (s *testIntegrationSuite) TestMathBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -531,10 +518,7 @@ func (s *testIntegrationSuite) TestMathBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -896,10 +880,7 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestEncryptionBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -1022,7 +1003,6 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
defer func() {
s.ctx.GetSessionVars().StrictSQLMode = originSQLMode
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -1706,10 +1686,7 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestOpBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -1749,10 +1726,7 @@ func (s *testIntegrationSuite) TestOpBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2120,10 +2094,7 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestInfoBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2209,10 +2180,7 @@ func (s *testIntegrationSuite) TestInfoBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestControlBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2271,10 +2239,7 @@ func (s *testIntegrationSuite) TestControlBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2444,10 +2409,7 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2607,10 +2569,7 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestAggregationBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t(a decimal(7, 6))")
Expand All @@ -2620,10 +2579,7 @@ func (s *testIntegrationSuite) TestAggregationBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestOtherBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

Expand Down Expand Up @@ -2707,11 +2663,7 @@ func (s *testIntegrationSuite) TestOtherBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestDateBuiltin(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()

defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
tk.MustExec("DROP TABLE IF EXISTS t;")
Expand Down Expand Up @@ -2798,11 +2750,7 @@ func (s *testIntegrationSuite) TestDateBuiltin(c *C) {
}

func (s *testIntegrationSuite) TestTimeLiteral(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()

defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)

r := tk.MustQuery("select time '117:01:12';")
Expand Down Expand Up @@ -2846,11 +2794,7 @@ func (s *testIntegrationSuite) TestTimeLiteral(c *C) {
}

func (s *testIntegrationSuite) TestTimestampLiteral(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()

defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)

r := tk.MustQuery("select timestamp '2017-01-01 00:00:00';")
Expand Down Expand Up @@ -2879,22 +2823,15 @@ func (s *testIntegrationSuite) TestTimestampLiteral(c *C) {
}

func (s *testIntegrationSuite) TestLiterals(c *C) {
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()

defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
r := tk.MustQuery("SELECT LENGTH(b''), LENGTH(B''), b''+1, b''-1, B''+1;")
r.Check(testkit.Rows("0 0 1 -1 1"))
}

func (s *testIntegrationSuite) TestFuncJSON(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
tk.MustExec("USE test;")
tk.MustExec("DROP TABLE IF EXISTS table_json;")
tk.MustExec("CREATE TABLE table_json(a json, b VARCHAR(255));")
Expand Down Expand Up @@ -2936,10 +2873,7 @@ func (s *testIntegrationSuite) TestFuncJSON(c *C) {

func (s *testIntegrationSuite) TestColumnInfoModified(c *C) {
testKit := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists tab0")
testKit.MustExec("CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER)")
Expand All @@ -2953,10 +2887,7 @@ func (s *testIntegrationSuite) TestColumnInfoModified(c *C) {

func (s *testIntegrationSuite) TestSetVariables(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
defer s.cleanEnv(c)
_, err := tk.Exec("set sql_mode='adfasdfadsfdasd';")
c.Assert(err, NotNil)
_, err = tk.Exec("set @@sql_mode='adfasdfadsfdasd';")
Expand Down