-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
executor: implement ANALYZE TABLE #1327
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,16 +15,20 @@ package executor | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/context" | ||
"github.com/pingcap/tidb/evaluator" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/meta" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/plan/statistics" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/sessionctx/db" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
|
@@ -310,6 +314,105 @@ func (e *SimpleExec) executeSetPwd(s *ast.SetPwdStmt) error { | |
} | ||
|
||
func (e *SimpleExec) executeAnalyzeTable(s *ast.AnalyzeTableStmt) error { | ||
// TODO: implement analyze table. | ||
for _, table := range s.TableNames { | ||
err := e.createStatisticsForTable(table) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const ( | ||
maxSampleCount = 10000 | ||
defaultBucketCount = 256 | ||
) | ||
|
||
func (e *SimpleExec) createStatisticsForTable(tn *ast.TableName) error { | ||
var tableName string | ||
if tn.Schema.L == "" { | ||
tableName = tn.Name.L | ||
} else { | ||
tableName = tn.Schema.L + "." + tn.Name.L | ||
} | ||
sql := "select * from " + tableName | ||
result, err := e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
count, samples, err := e.collectSamples(result) | ||
result.Close() | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = e.buildStatisticsAndSaveToKV(tn, count, samples) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
// collectSamples collects sample from the result set, use Reservoir Sampling algorithm. | ||
// See https://en.wikipedia.org/wiki/Reservoir_sampling | ||
func (e *SimpleExec) collectSamples(result ast.RecordSet) (count int64, samples []*ast.Row, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can add a reference for your sampling algorithm. |
||
ran := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
for { | ||
var row *ast.Row | ||
row, err = result.Next() | ||
if err != nil { | ||
return count, samples, errors.Trace(err) | ||
} | ||
if row == nil { | ||
break | ||
} | ||
if len(samples) < maxSampleCount { | ||
samples = append(samples, row) | ||
} else { | ||
shouldAdd := ran.Float64() < float64(maxSampleCount)/float64(count) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not rand a int between 0 and count + 1 ? |
||
if shouldAdd { | ||
idx := ran.Intn(maxSampleCount) | ||
samples[idx] = row | ||
} | ||
} | ||
count++ | ||
} | ||
return count, samples, nil | ||
} | ||
|
||
func (e *SimpleExec) buildStatisticsAndSaveToKV(tn *ast.TableName, count int64, sampleRows []*ast.Row) error { | ||
txn, err := e.ctx.GetTxn(false) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
columnSamples := rowsToColumnSamples(sampleRows) | ||
t, err := statistics.NewTable(tn.TableInfo, int64(txn.StartTS()), count, defaultBucketCount, columnSamples) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
tpb, err := t.ToPB() | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
m := meta.NewMeta(txn) | ||
err = m.SetTableStats(tn.TableInfo.ID, tpb) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
func rowsToColumnSamples(rows []*ast.Row) [][]types.Datum { | ||
if len(rows) == 0 { | ||
return nil | ||
} | ||
columnSamples := make([][]types.Datum, len(rows[0].Data)) | ||
for i := range columnSamples { | ||
columnSamples[i] = make([]types.Datum, len(rows)) | ||
} | ||
for j, row := range rows { | ||
for i, val := range row.Data { | ||
columnSamples[i][j] = val | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use copy ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to put values of a column in a single slice, but original columns are stored in a row. |
||
} | ||
} | ||
return columnSamples | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use -> using ?