forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectivity_test.go
343 lines (305 loc) · 11.4 KB
/
selectivity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package statistics_test
import (
"fmt"
"math"
"os"
"runtime/pprof"
"testing"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tidb/util/testkit"
)
const eps = 1e-9
var _ = Suite(&testSelectivitySuite{})
type testSelectivitySuite struct {
store kv.Storage
dom *domain.Domain
}
func (s *testSelectivitySuite) SetUpSuite(c *C) {
store, dom, err := newStoreWithBootstrap(0)
c.Assert(err, IsNil)
s.dom = dom
s.store = store
}
func (s *testSelectivitySuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
}
// generateIntDatum will generate a datum slice, every dimension is begin from 0, end with num - 1.
// If dimension is x, num is y, the total number of datum is y^x. And This slice is sorted.
func (s *testSelectivitySuite) generateIntDatum(dimension, num int) ([]types.Datum, error) {
length := int(math.Pow(float64(num), float64(dimension)))
ret := make([]types.Datum, length)
if dimension == 1 {
for i := 0; i < num; i++ {
ret[i] = types.NewIntDatum(int64(i))
}
} else {
sc := &stmtctx.StatementContext{TimeZone: time.Local}
// In this way, we can guarantee the datum is in order.
for i := 0; i < length; i++ {
data := make([]types.Datum, dimension)
j := i
for k := 0; k < dimension; k++ {
data[dimension-k-1].SetInt64(int64(j % num))
j = j / num
}
bytes, err := codec.EncodeKey(sc, nil, data...)
if err != nil {
return nil, err
}
ret[i].SetBytes(bytes)
}
}
return ret, nil
}
// mockStatsHistogram will create a statistics.Histogram, of which the data is uniform distribution.
func mockStatsHistogram(id int64, values []types.Datum, repeat int64, tp *types.FieldType) *statistics.Histogram {
ndv := len(values)
histogram := statistics.NewHistogram(id, int64(ndv), 0, 0, tp, ndv, 0)
for i := 0; i < ndv; i++ {
histogram.AppendBucket(&values[i], &values[i], repeat*int64(i+1), repeat)
}
return histogram
}
func mockStatsTable(tbl *model.TableInfo, rowCount int64) *statistics.Table {
histColl := statistics.HistColl{
PhysicalID: tbl.ID,
HavePhysicalID: true,
Count: rowCount,
Columns: make(map[int64]*statistics.Column, len(tbl.Columns)),
Indices: make(map[int64]*statistics.Index, len(tbl.Indices)),
}
statsTbl := &statistics.Table{
HistColl: histColl,
}
return statsTbl
}
func (s *testSelectivitySuite) prepareSelectivity(testKit *testkit.TestKit, c *C) *statistics.Table {
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a int primary key, b int, c int, d int, e int, index idx_cd(c, d), index idx_de(d, e))")
is := s.dom.InfoSchema()
tb, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tbl := tb.Meta()
// mock the statistic table
statsTbl := mockStatsTable(tbl, 540)
// Set the value of columns' histogram.
colValues, _ := s.generateIntDatum(1, 54)
for i := 1; i <= 5; i++ {
statsTbl.Columns[int64(i)] = &statistics.Column{Histogram: *mockStatsHistogram(int64(i), colValues, 10, types.NewFieldType(mysql.TypeLonglong)), Info: tbl.Columns[i-1]}
}
// Set the value of two indices' histograms.
idxValues, err := s.generateIntDatum(2, 3)
c.Assert(err, IsNil)
tp := types.NewFieldType(mysql.TypeBlob)
statsTbl.Indices[1] = &statistics.Index{Histogram: *mockStatsHistogram(1, idxValues, 60, tp), Info: tbl.Indices[0]}
statsTbl.Indices[2] = &statistics.Index{Histogram: *mockStatsHistogram(2, idxValues, 60, tp), Info: tbl.Indices[1]}
return statsTbl
}
func (s *testSelectivitySuite) TestSelectivity(c *C) {
testKit := testkit.NewTestKit(c, s.store)
statsTbl := s.prepareSelectivity(testKit, c)
is := s.dom.InfoSchema()
tests := []struct {
exprs string
selectivity float64
}{
{
exprs: "a > 0 and a < 2",
selectivity: 0.01851851851,
},
{
exprs: "a >= 1 and a < 2",
selectivity: 0.01851851851,
},
{
exprs: "a >= 1 and b > 1 and a < 2",
selectivity: 0.01817558299,
},
{
exprs: "a >= 1 and c > 1 and a < 2",
selectivity: 0.00617283950,
},
{
exprs: "a >= 1 and c >= 1 and a < 2",
selectivity: 0.01234567901,
},
{
exprs: "d = 0 and e = 1",
selectivity: 0.11111111111,
},
{
exprs: "b > 1",
selectivity: 0.98148148148,
},
{
exprs: "a > 1 and b < 2 and c > 3 and d < 4 and e > 5",
selectivity: 0,
},
}
for _, tt := range tests {
sql := "select * from t where " + tt.exprs
comment := Commentf("for %s", tt.exprs)
ctx := testKit.Se.(sessionctx.Context)
stmts, err := session.Parse(ctx, sql)
c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprs))
c.Assert(stmts, HasLen, 1)
err = plannercore.Preprocess(ctx, stmts[0], is, false)
c.Assert(err, IsNil, comment)
p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)
c.Assert(err, IsNil, Commentf("error %v, for building plan, expr %s", err, tt.exprs))
sel := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
ds := sel.Children()[0].(*plannercore.DataSource)
histColl := statsTbl.GenerateHistCollFromColumnInfo(ds.Columns, ds.Schema().Columns)
ratio, err := histColl.Selectivity(ctx, sel.Conditions)
c.Assert(err, IsNil, comment)
c.Assert(math.Abs(ratio-tt.selectivity) < eps, IsTrue, Commentf("for %s, needed: %v, got: %v", tt.exprs, tt.selectivity, ratio))
histColl.Count *= 10
ratio, err = histColl.Selectivity(ctx, sel.Conditions)
c.Assert(err, IsNil, comment)
c.Assert(math.Abs(ratio-tt.selectivity) < eps, IsTrue, Commentf("for %s, needed: %v, got: %v", tt.exprs, tt.selectivity, ratio))
}
}
// TestDiscreteDistribution tests the estimation for discrete data distribution. This is more common when the index
// consists several columns, and the first column has small NDV.
func (s *testSelectivitySuite) TestDiscreteDistribution(c *C) {
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a char(10), b int, key idx(a, b))")
for i := 0; i < 499; i++ {
testKit.MustExec(fmt.Sprintf("insert into t values ('cn', %d)", i))
}
for i := 0; i < 10; i++ {
testKit.MustExec("insert into t values ('tw', 0)")
}
testKit.MustExec("analyze table t")
testKit.MustQuery("explain select * from t where a = 'tw' and b < 0").Check(testkit.Rows(
"IndexReader_9 0.00 root index:IndexScan_8",
"└─IndexScan_8 0.00 cop table:t, index:a, b, range:[\"tw\" -inf,\"tw\" 0), keep order:false"))
}
func (s *testSelectivitySuite) TestSelectCombinedLowBound(c *C) {
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(id int auto_increment, kid int, pid int, primary key(id), key(kid, pid))")
testKit.MustExec("insert into t (kid, pid) values (1,2), (1,3), (1,4),(1, 11), (1, 12), (1, 13), (1, 14), (2, 2), (2, 3), (2, 4)")
testKit.MustExec("analyze table t")
testKit.MustQuery("explain select * from t where kid = 1").Check(testkit.Rows(
"IndexReader_9 7.00 root index:IndexScan_8",
"└─IndexScan_8 7.00 cop table:t, index:kid, pid, range:[1,1], keep order:false"))
}
func getRange(start, end int64) []*ranger.Range {
ran := &ranger.Range{
LowVal: []types.Datum{types.NewIntDatum(start)},
HighVal: []types.Datum{types.NewIntDatum(end)},
}
return []*ranger.Range{ran}
}
func (s *testSelectivitySuite) TestEstimationForUnknownValues(c *C) {
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a int, b int, key idx(a, b))")
testKit.MustExec("analyze table t")
for i := 0; i < 10; i++ {
testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i, i))
}
h := s.dom.StatsHandle()
h.DumpStatsDeltaToKV(statistics.DumpAll)
testKit.MustExec("analyze table t")
for i := 0; i < 10; i++ {
testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i+10, i+10))
}
h.DumpStatsDeltaToKV(statistics.DumpAll)
c.Assert(h.Update(s.dom.InfoSchema()), IsNil)
table, err := s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
statsTbl := h.GetTableStats(table.Meta())
sc := &stmtctx.StatementContext{}
colID := table.Meta().Columns[0].ID
count, err := statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(30, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 2.0)
count, err = statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(9, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 4.2)
count, err = statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(9, math.MaxInt64))
c.Assert(err, IsNil)
c.Assert(count, Equals, 4.2)
idxID := table.Meta().Indices[0].ID
count, err = statsTbl.GetRowCountByIndexRanges(sc, idxID, getRange(30, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 0.2)
count, err = statsTbl.GetRowCountByIndexRanges(sc, idxID, getRange(9, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 2.2)
testKit.MustExec("truncate table t")
testKit.MustExec("insert into t values (null, null)")
testKit.MustExec("analyze table t")
table, err = s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
statsTbl = h.GetTableStats(table.Meta())
colID = table.Meta().Columns[0].ID
count, err = statsTbl.GetRowCountByColumnRanges(sc, colID, getRange(1, 30))
c.Assert(err, IsNil)
c.Assert(count, Equals, 0.0)
}
func BenchmarkSelectivity(b *testing.B) {
c := &C{}
s := &testSelectivitySuite{}
s.SetUpSuite(c)
defer s.TearDownSuite(c)
testKit := testkit.NewTestKit(c, s.store)
statsTbl := s.prepareSelectivity(testKit, c)
is := s.dom.InfoSchema()
exprs := "a > 1 and b < 2 and c > 3 and d < 4 and e > 5"
sql := "select * from t where " + exprs
comment := Commentf("for %s", exprs)
ctx := testKit.Se.(sessionctx.Context)
stmts, err := session.Parse(ctx, sql)
c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, exprs))
c.Assert(stmts, HasLen, 1)
err = plannercore.Preprocess(ctx, stmts[0], is, false)
c.Assert(err, IsNil, comment)
p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)
c.Assert(err, IsNil, Commentf("error %v, for building plan, expr %s", err, exprs))
file, _ := os.Create("cpu.profile")
defer file.Close()
pprof.StartCPUProfile(file)
b.Run("selectivity", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := statsTbl.Selectivity(ctx, p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection).Conditions)
c.Assert(err, IsNil)
}
b.ReportAllocs()
})
pprof.StopCPUProfile()
}