158 lines
4.8 KiB
Go
158 lines
4.8 KiB
Go
// Copyright 2017 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 core_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/pingcap/tidb/pkg/planner"
|
|
"github.com/pingcap/tidb/pkg/planner/core"
|
|
"github.com/pingcap/tidb/pkg/planner/core/resolve"
|
|
"github.com/pingcap/tidb/pkg/session"
|
|
"github.com/pingcap/tidb/pkg/testkit"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func constructInsertSQL(i, n int) string {
|
|
sql := "insert into t (a,b,c,e)values "
|
|
for j := 0; j < n; j++ {
|
|
sql += fmt.Sprintf("(%d, %d, '%d', %d)", i*n+j, i, i+j, i*n+j)
|
|
if j != n-1 {
|
|
sql += ", "
|
|
}
|
|
}
|
|
return sql
|
|
}
|
|
|
|
func BenchmarkOptimize(b *testing.B) {
|
|
store := testkit.CreateMockStore(b)
|
|
|
|
testKit := testkit.NewTestKit(b, store)
|
|
testKit.MustExec("use test")
|
|
testKit.MustExec("drop table if exists t")
|
|
testKit.MustExec("create table t (a int primary key, b int, c varchar(200), d datetime DEFAULT CURRENT_TIMESTAMP, e int, ts timestamp DEFAULT CURRENT_TIMESTAMP)")
|
|
testKit.MustExec("create index b on t (b)")
|
|
testKit.MustExec("create index d on t (d)")
|
|
testKit.MustExec("create index e on t (e)")
|
|
testKit.MustExec("create index b_c on t (b,c)")
|
|
testKit.MustExec("create index ts on t (ts)")
|
|
for i := 0; i < 100; i++ {
|
|
testKit.MustExec(constructInsertSQL(i, 100))
|
|
}
|
|
testKit.MustExec("analyze table t")
|
|
tests := []struct {
|
|
sql string
|
|
best string
|
|
}{
|
|
{
|
|
sql: "select count(*) from t group by e",
|
|
best: "IndexReader(Index(t.e)[[NULL,+inf]])->StreamAgg",
|
|
},
|
|
{
|
|
sql: "select count(*) from t where e <= 10 group by e",
|
|
best: "IndexReader(Index(t.e)[[-inf,10]])->StreamAgg",
|
|
},
|
|
{
|
|
sql: "select count(*) from t where e <= 50",
|
|
best: "IndexReader(Index(t.e)[[-inf,50]]->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(*) from t where c > '1' group by b",
|
|
best: "IndexReader(Index(t.b_c)[[NULL,+inf]]->Sel([gt(test.t.c, 1)]))->StreamAgg",
|
|
},
|
|
{
|
|
sql: "select count(*) from t where e = 1 group by b",
|
|
best: "IndexLookUp(Index(t.e)[[1,1]], Table(t)->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(*) from t where e > 1 group by b",
|
|
best: "TableReader(Table(t)->Sel([gt(test.t.e, 1)])->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(e) from t where t.b <= 20",
|
|
best: "IndexLookUp(Index(t.b)[[-inf,20]], Table(t)->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(e) from t where t.b <= 30",
|
|
best: "IndexLookUp(Index(t.b)[[-inf,30]], Table(t)->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(e) from t where t.b <= 40",
|
|
best: "IndexLookUp(Index(t.b)[[-inf,40]], Table(t)->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select count(e) from t where t.b <= 50",
|
|
best: "TableReader(Table(t)->Sel([le(test.t.b, 50)])->HashAgg)->HashAgg",
|
|
},
|
|
{
|
|
sql: "select * from t where t.b <= 40",
|
|
best: "IndexLookUp(Index(t.b)[[-inf,40]], Table(t))",
|
|
},
|
|
{
|
|
sql: "select * from t where t.b <= 50",
|
|
best: "TableReader(Table(t)->Sel([le(test.t.b, 50)]))",
|
|
},
|
|
// test panic
|
|
{
|
|
sql: "select * from t where 1 and t.b <= 50",
|
|
best: "TableReader(Table(t)->Sel([le(test.t.b, 50)]))",
|
|
},
|
|
{
|
|
sql: "select * from t where t.b <= 100 order by t.a limit 1",
|
|
best: "TableReader(Table(t)->Sel([le(test.t.b, 100)])->Limit)->Limit",
|
|
},
|
|
{
|
|
sql: "select * from t where t.b <= 1 order by t.a limit 10",
|
|
best: "IndexLookUp(Index(t.b)[[-inf,1]]->TopN([test.t.a],0,10), Table(t))->TopN([test.t.a],0,10)",
|
|
},
|
|
{
|
|
sql: "select * from t use index(b) where b = 1 order by a",
|
|
best: "IndexLookUp(Index(t.b)[[1,1]], Table(t))->Sort",
|
|
},
|
|
// test datetime
|
|
{
|
|
sql: "select * from t where d < cast('1991-09-05' as datetime)",
|
|
best: "IndexLookUp(Index(t.d)[[-inf,1991-09-05 00:00:00)], Table(t))",
|
|
},
|
|
// test timestamp
|
|
{
|
|
sql: "select * from t where ts < '1991-09-05'",
|
|
best: "IndexLookUp(Index(t.ts)[[-inf,1991-09-05 00:00:00)], Table(t))",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
ctx := testKit.Session()
|
|
stmts, err := session.Parse(ctx, tt.sql)
|
|
require.NoError(b, err)
|
|
require.Len(b, stmts, 1)
|
|
stmt := stmts[0]
|
|
ret := &core.PreprocessorReturn{}
|
|
nodeW := resolve.NewNodeW(stmt)
|
|
err = core.Preprocess(context.Background(), ctx, nodeW, core.WithPreprocessorReturn(ret))
|
|
require.NoError(b, err)
|
|
|
|
b.Run(tt.sql, func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, _, err := planner.Optimize(context.TODO(), ctx, nodeW, ret.InfoSchema)
|
|
require.NoError(b, err)
|
|
}
|
|
b.ReportAllocs()
|
|
})
|
|
}
|
|
}
|