github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/explain_test.go (about)

     1  // Copyright 2016 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor_test
    15  
    16  import (
    17  	. "github.com/insionng/yougam/libraries/pingcap/check"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testkit"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    20  )
    21  
    22  func (s *testSuite) TestExplain(c *C) {
    23  	defer testleak.AfterTest(c)()
    24  	tk := testkit.NewTestKit(c, s.store)
    25  	tk.MustExec("use test")
    26  	tk.MustExec("drop table if exists t1, t2")
    27  	tk.MustExec("create table t1 (c1 int primary key, c2 int, index c2 (c2))")
    28  	tk.MustExec("create table t2 (c1 int unique, c2 int)")
    29  
    30  	cases := []struct {
    31  		sql    string
    32  		result []string
    33  	}{
    34  		{
    35  			"select * from t1",
    36  			[]string{
    37  				"1 | SIMPLE | t1 | ALL | <nil> | <nil> | <nil> | <nil> | 0 | <nil>",
    38  			},
    39  		},
    40  		{
    41  			"select * from t1 order by c2",
    42  			[]string{
    43  				"1 | SIMPLE | t1 | index | c2 | c2 | <nil> | <nil> | 0 | <nil>",
    44  			},
    45  		},
    46  		{
    47  			"select * from t2 order by c2",
    48  			[]string{
    49  				"1 | SIMPLE | t2 | ALL | <nil> | <nil> | <nil> | <nil> | 0 | Using filesort",
    50  			},
    51  		},
    52  		{
    53  			"select * from t1 where t1.c1 > 0",
    54  			[]string{
    55  				"1 | SIMPLE | t1 | range | PRIMARY | PRIMARY | 8 | <nil> | 0 | Using where",
    56  			},
    57  		},
    58  		{
    59  			"select * from t1 where t1.c1 = 1",
    60  			[]string{
    61  				"1 | SIMPLE | t1 | const | PRIMARY | PRIMARY | 8 | <nil> | 0 | Using where",
    62  			},
    63  		},
    64  		{
    65  			"select * from t1 where t1.c2 = 1",
    66  			[]string{
    67  				"1 | SIMPLE | t1 | range | c2 | c2 | -1 | <nil> | 0 | Using where",
    68  			},
    69  		},
    70  		{
    71  			"select * from t1 left join t2 on t1.c2 = t2.c1 where t1.c1 > 1",
    72  			[]string{
    73  				"1 | SIMPLE | t1 | range | PRIMARY | PRIMARY | 8 | <nil> | 0 | Using where",
    74  				"1 | SIMPLE | t2 | eq_ref | c1 | c1 | -1 | <nil> | 0 | Using where",
    75  			},
    76  		},
    77  		{
    78  			"update t1 set t1.c2 = 2 where t1.c1 = 1",
    79  			[]string{
    80  				"1 | SIMPLE | t1 | const | PRIMARY | PRIMARY | 8 | <nil> | 0 | Using where",
    81  			},
    82  		},
    83  		{
    84  			"delete from t1 where t1.c2 = 1",
    85  			[]string{
    86  				"1 | SIMPLE | t1 | range | c2 | c2 | -1 | <nil> | 0 | Using where",
    87  			},
    88  		},
    89  	}
    90  	for _, ca := range cases {
    91  		result := tk.MustQuery("explain " + ca.sql)
    92  		result.Check(testkit.RowsWithSep(" | ", ca.result...))
    93  	}
    94  }