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

plan, tidb: support plan cache for SELECT statement #4644

Merged
merged 56 commits into from
Oct 12, 2017

Conversation

zz-jason
Copy link
Member

main change:

  1. add a simple lru cache, defined in plan/cache/lru.go
  2. in function func (s *session) Execute(sql string) ([]ast.RecordSet, error), we check the opportunity to use plan cache first
  3. currently, we only add select statement into cache

@shenli
Copy link
Member

shenli commented Sep 26, 2017

@zz-jason Good job!
Any benchmark result?

@zz-jason
Copy link
Member Author

@shenli WIP, benchmark result will be provided later.

@zz-jason zz-jason added the WIP label Sep 26, 2017
@winoros
Copy link
Member

winoros commented Sep 26, 2017

When transaction changed from read only to not read only. The plan will be changed since dirty table exists.

@zz-jason zz-jason removed the WIP label Sep 27, 2017
@zimulala
Copy link
Contributor

Please fix CI @zz-jason

@zz-jason zz-jason added the WIP label Sep 28, 2017
@zz-jason zz-jason removed the WIP label Sep 28, 2017
p, err := plan.Optimize(ctx, node, is)
if err != nil {
return nil, errors.Trace(err)
}

_, isDual := p.(*plan.TableDual)
if isDual {
cacheable = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dual cannot be cached?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create table t(a bigint);
select * from t where exists (select * from t);

this query will be optimized to a TableDual

@zz-jason
Copy link
Member Author

@coocood PTAL

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rest LGTM

c.Assert(lru.size, Equals, lru.capacity)
c.Assert(lru.size, Equals, int64(3))

// test for nonexistence elements
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ nonexistence/ non-existent

c.Assert(element, IsNil)
}

// test for in existence elements
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ in existence/ existent

lru.Put(keys[i], vals[i])
}

// test for nonexistence elements
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

switch expr := exprNode.(type) {
case *ast.VariableExpr, *ast.ExistsSubqueryExpr:
return false
case *ast.UnaryOperationExpr:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, V will be visited.

// Enter implements Visitor interface.
func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch node := in.(type) {
case *ast.SelectStmt:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do this, every expression in SelectStmt will be visited.

}

// Leave implements Visitor interface.
func (checker *cacheableChecker) Leave(in ast.Node) (out ast.Node, skipChildren bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just switch the type of in here will do.

if err := plan.Preprocess(node, is, ctx); err != nil {
return nil, errors.Trace(err)
// Compile compiles an ast.StmtNode to a physical plan.
func (c *Compiler) Compile(ctx context.Context, node ast.StmtNode) (is infoschema.InfoSchema, p plan.Plan, expensive bool, cacheable bool, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has too many return values, how about just return ExecStmt?

session.go Outdated
// Step3: Cache the physical plan if possiable.
if cache.PlanCacheEnabled && cacheable && len(stmtNodes) == 1 {
if _, isSelect := stmtNodes[0].(*ast.SelectStmt); isSelect {
cache.GlobalPlanCache.Put(cacheKey, cache.NewSQLCacheValue(stmtNode, plan, expensive))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSelect is covered by cacheable already.

// PlanCacheEnabled stores the global config "plan-cache-enabled".
PlanCacheEnabled bool
// PlanCacheShards stores the global config "plan-cache-shards".
PlanCacheShards int64 = 200
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a power of 2 value is more efficient.

func (key *sqlCacheKey) Hash() []byte {
if key.hash == nil {
var (
userBytes = []byte(key.user)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use hack.Slice to avoid memory allocation.

@zz-jason
Copy link
Member Author

/run-all-test

@zz-jason
Copy link
Member Author

@coocood @XuHuaiyu @lamxTyler PTAL

@XuHuaiyu
Copy link
Contributor

LGTM

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Oct 11, 2017
)

// Cacheable checks whether the input ast is cacheable.
func Cacheable(node ast.Node) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need some test for this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

// Enter implements Visitor interface.
func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch in.(type) {
case *ast.VariableExpr, *ast.ExistsSubqueryExpr:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query contains non-deterministic function like Now(), RAND() should not be cacheable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. btw, rand() can be cached

session.go Outdated
}
sessionExecuteCompileDuration.Observe(time.Since(startTS).Seconds())

// Step3: Cache the physical plan if possiable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

config/config.go Outdated
@@ -121,6 +129,11 @@ var defaultConf = Config{
XHost: "0.0.0.0",
XPort: 14000,
},
PlanCache: PlanCache{
Enabled: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set default to true is not safe for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only for the ci test, will be disabled in the last commit of this pr

@coocood
Copy link
Member

coocood commented Oct 12, 2017

LGTM

@coocood coocood merged commit d88344f into master Oct 12, 2017
@coocood coocood deleted the zz-jason/plancache/lru branch October 12, 2017 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT1 Indicates that a PR has LGTM 1. type/performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants