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

*: improve plan cache param eval and insert const #10746

Merged
merged 2 commits into from
Aug 27, 2019
Merged

*: improve plan cache param eval and insert const #10746

merged 2 commits into from
Aug 27, 2019

Conversation

lysu
Copy link
Contributor

@lysu lysu commented Jun 10, 2019

What problem does this PR solve?

  • prepare-plan-cache will generate builtinGetParamStringSig and interpreting running and also do cast from T to string then cast back to a string
  • almost time insert values .....' s values just const, but we interpreted for each value

What is changed and how it works?

  • no expression eval, directly get param from session
  • identity const value insert at parse phase and use the fast way to take the insert values

Check List

Tests

  • Unit test
  • Integration test
  • Bench

Code changes

  • N/A
    Side effects

  • N/A

Related changes

  • Need to cherry-pick to the release branch

This change is Reviewable

@lysu
Copy link
Contributor Author

lysu commented Jun 10, 2019

/bench

@codecov
Copy link

codecov bot commented Jun 10, 2019

Codecov Report

Merging #10746 into master will decrease coverage by 0.0071%.
The diff coverage is 88.8235%.

@@               Coverage Diff                @@
##             master     #10746        +/-   ##
================================================
- Coverage   81.4186%   81.4114%   -0.0072%     
================================================
  Files           438        434         -4     
  Lines         94993      93536      -1457     
================================================
- Hits          77342      76149      -1193     
+ Misses        12174      11917       -257     
+ Partials       5477       5470         -7

@lysu
Copy link
Contributor Author

lysu commented Jun 10, 2019

/bench

@lysu
Copy link
Contributor Author

lysu commented Jun 10, 2019

/bench

@zhouqiang-cl
Copy link
Contributor

@lysu /bench command is offline now due to unstable aws server. It will be online after about 2 weeks after there is enough server in new IDC

@lysu
Copy link
Contributor Author

lysu commented Jun 20, 2019

/build

@ngaut
Copy link
Member

ngaut commented Jul 11, 2019

Would you like to take a look? @dbjoa

@dbjoa
Copy link
Contributor

dbjoa commented Jul 12, 2019

@lysu
If the cost of accessing the parameter value with Eval() is not significant relatively and know the type of a given parameter in advance, we can remove the unnecessary string-typed casting by adding several typed parameter getting functions (e.g., builtinGetParamNumericSig(), builtinGetParamDatetimeSig(), etc.).

Could you share a benchmark result of the PR?

@lysu
Copy link
Contributor Author

lysu commented Jul 12, 2019

@lysu
If the cost of accessing the parameter value with Eval() is not significant relatively and know the type of a given parameter in advance, we can remove the unnecessary string-typed casting by adding several typed parameter getting functions (e.g., builtinGetParamNumericSig(), builtinGetParamDatetimeSig(), etc.).

Could you share a benchmark result of the PR?

@dbjoa I have a benchmark at #10884 (comment) that combined two PR.

In PrepareStmt parameter should always be a const value datum(which has type), we are better not pay cost to create function
object and calling eval(which will do many type switch), it will be more obvious if we are insert a wide table with many param holder.

The idea is we know param is simple const and we can directly use it, do less eval/cast. 😄

and I will continue to improve this pr..

@dbjoa
Copy link
Contributor

dbjoa commented Jul 12, 2019

@lysu
How about do we specialize Constant.Eval() by changing the small codes like below, which may be able to achieve the similar performance gain like using DeferredParam?

// Eval implements Expression interface.
func (c *Constant) Eval(_ chunk.Row) (dt types.Datum, err error) {
	if c.DeferredExpr != nil {
		if sf, sfOK := c.DeferredExpr.(*ScalarFunction); sfOK {
			if sf.FuncName.O == ast.GetParam {
				idx := sf.GetArgs()[0].(*Constant).Value.GetInt64()
				dt = sf.GetCtx().GetSessionVars().PreparedParams[idx]
			} else {
				dt, err = sf.Eval(chunk.Row{})
				if err != nil {
					return c.Value, err
				}
			}

@lysu
Copy link
Contributor Author

lysu commented Jul 12, 2019

@dbjoa yes, it's better but we also new complex function expression in

value.DeferredExpr = f
, maybe we can cut off it

@lysu lysu requested a review from coocood July 12, 2019 06:51
@zz-jason
Copy link
Member

zz-jason commented Aug 5, 2019

@lysu can we review this PR now?

@lysu lysu removed the status/WIP label Aug 7, 2019
@lysu lysu marked this pull request as ready for review August 7, 2019 04:26
@lysu lysu requested review from jackysp and crazycs520 August 7, 2019 04:27
@lysu lysu added the type/enhancement The issue or PR belongs to an enhancement. label Aug 7, 2019
@lysu
Copy link
Contributor Author

lysu commented Aug 7, 2019

/run-all-tests

@lysu lysu requested a review from zz-jason August 7, 2019 07:10
if err != nil {
return err
var row []types.Datum
if e.notAllConstant {
Copy link
Contributor

Choose a reason for hiding this comment

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

Try to avoid branch prediction failure.

    // put below out of loop.
	evalRowFunc := e.fastEvalRow
	if e.notAllConstant {
		evalRowFunc = e.evalRow	
	}

planner/core/point_get_plan.go Outdated Show resolved Hide resolved
planner/core/planbuilder.go Outdated Show resolved Hide resolved
planner/core/logical_plan_builder.go Outdated Show resolved Hide resolved
planner/core/common_plans.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
expression/constant_fold.go Outdated Show resolved Hide resolved
if err != nil {
return types.Duration{}, true, err
}
dur, err := types.ParseDuration(ctx.GetSessionVars().StmtCtx, val, types.MaxFsp)
Copy link
Member

Choose a reason for hiding this comment

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

It's better to wrap a cast string as duration function on this deferred param. A method is to transform the deferred param to a expression.Constant, construct a unfolded scalar Cast function upon that Constant.

Copy link
Contributor Author

@lysu lysu Aug 9, 2019

Choose a reason for hiding this comment

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

good catch, can we do this on next PR to avoid useless str<->duration?

we also need change

return pos, fmt.Sprintf("%s%d %02d:%02d:%02d", sign, days, hours, minutes, seconds)
to let dt in here be a duration datum, also some other types

expression/constant.go Outdated Show resolved Hide resolved
@@ -284,7 +412,6 @@ func (c *Constant) EvalJSON(ctx sessionctx.Context, _ chunk.Row) (json.BinaryJSO
if err != nil {
return json.BinaryJSON{}, true, err
}
fmt.Println("const eval json", val.GetMysqlJSON().String())
Copy link
Member

Choose a reason for hiding this comment

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

???

Copy link
Member

Choose a reason for hiding this comment

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

Could you use another PR to remove this line? We need to cherry pick this change to the release branches...

executor/update.go Outdated Show resolved Hide resolved
Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

LGTM

@jackysp
Copy link
Member

jackysp commented Aug 9, 2019

PTAL @crazycs520

Copy link
Contributor

@crazycs520 crazycs520 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

expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
expression/constant.go Outdated Show resolved Hide resolved
@@ -284,7 +412,6 @@ func (c *Constant) EvalJSON(ctx sessionctx.Context, _ chunk.Row) (json.BinaryJSO
if err != nil {
return json.BinaryJSON{}, true, err
}
fmt.Println("const eval json", val.GetMysqlJSON().String())
Copy link
Member

Choose a reason for hiding this comment

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

Could you use another PR to remove this line? We need to cherry pick this change to the release branches...

@lysu
Copy link
Contributor Author

lysu commented Aug 9, 2019

/run-all-tests

1 similar comment
@lysu
Copy link
Contributor Author

lysu commented Aug 10, 2019

/run-all-tests

@lysu
Copy link
Contributor Author

lysu commented Aug 10, 2019

/run-unit-test

@shenli
Copy link
Member

shenli commented Aug 11, 2019

Could we cherry-pick this PR to the release-3.0?

@jackysp
Copy link
Member

jackysp commented Aug 11, 2019

/run-unit-test

@lysu
Copy link
Contributor Author

lysu commented Aug 13, 2019

/run-all-tests

1 similar comment
@lysu
Copy link
Contributor Author

lysu commented Aug 15, 2019

/run-all-tests

@lysu
Copy link
Contributor Author

lysu commented Aug 15, 2019

@zz-jason ptal if free ~ thx

expression/constant.go Outdated Show resolved Hide resolved
@lysu
Copy link
Contributor Author

lysu commented Aug 20, 2019

/run-all-tests

@lysu
Copy link
Contributor Author

lysu commented Aug 21, 2019

last day, testcase meet a problem: wrong parameter type introduce a cast function and that cast let query can not using right index(cast couldn't push down to tikv now), and that problem has be fixed, but maybe we better keep this in master to protect the risk to introduce other cast function and using wrong index. we can cherry-pick this after more test or after cast can be pushed down to tikv(in roadmap)

PTAL @zz-jason if free, thx

@lysu lysu requested a review from zz-jason August 26, 2019 05:15
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason
Copy link
Member

@lysu please merge the master branch and resolve conflicts.

@lysu lysu added the status/can-merge Indicates a PR has been approved by a committer. label Aug 27, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Aug 27, 2019

/run-all-tests

@jackysp jackysp merged commit 4cd5d15 into pingcap:master Aug 27, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Aug 27, 2019

cherry pick to release-3.0 failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/server status/can-merge Indicates a PR has been approved by a committer. status/LGT1 Indicates that a PR has LGTM 1. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants