Skip to content

sql: incorporate lookupExpr cpu cost into cost model#66786

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
cucaroach:lookup-expr-cost
Jun 30, 2021
Merged

sql: incorporate lookupExpr cpu cost into cost model#66786
craig[bot] merged 1 commit intocockroachdb:masterfrom
cucaroach:lookup-expr-cost

Conversation

@cucaroach
Copy link
Copy Markdown
Contributor

@cucaroach cucaroach commented Jun 23, 2021

Informs #51576

This is a prep-the-patient change required for enabling the lookup join inequality conditions. It enables for instance partial indexes to be selected when there's a costing tie between a lookup join with a partial index vs a full index with the partial index qualifier as a lookup join condition.

Release note (sql): improve cost model to include cpu cost of lookupExprs used by lookup joins.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@cucaroach cucaroach requested review from a team and rytaft June 25, 2021 19:51
@cucaroach cucaroach linked an issue Jun 25, 2021 that may be closed by this pull request
@cucaroach cucaroach marked this pull request as ready for review June 25, 2021 20:00
Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Nice, this looks like it will be a good change. Glad to see it didn't change any plans.

nit: I don't think "sql" is a valid release note category. I'd call this "performance improvement". Also, I'd be a bit more verbose in the release note to explain how the change affects the end user.

Reviewed 2 of 2 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @cucaroach)


pkg/sql/opt/xform/coster.go, line 798 at r1 (raw file):

	// If we have extra lookupExprs beyond whats in the on conditions add those
	// as an extra cpu cost since the joinreader machinery isn't free.
	extraLookupJoinConditions := join.LookupExpr.Difference(join.On)

Is this Difference necessary? I think the LookupExpr should already be disjoint from On.


pkg/sql/opt/xform/coster.go, line 817 at r1 (raw file):

	flags memo.JoinFlags,
	localityOptimized bool,
) (memo.Cost, float64) {

I don't love that you need to return the lookupCount here. I would probably encapsulate the logic for the cost of the extra lookup conditions in a helper function that takes the join and lookupCount as parameters and returns the cost (if join is not a LookupJoinExpr or doesn't have LookupExpr set, just return 0). That would also allow us to add a cost for multiple lookupCols in the future, which might be worth doing.

But I don't feel too strongly... if you want to leave it as-is, just be sure to add a comment to the function explaining the new return param.

Copy link
Copy Markdown
Contributor Author

@cucaroach cucaroach left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rytaft)


pkg/sql/opt/xform/coster.go, line 798 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Is this Difference necessary? I think the LookupExpr should already be disjoint from On.

I swear I saw a case where lookupExpr was duping part of the on but I couldn't find it, I must have been duping myself.


pkg/sql/opt/xform/coster.go, line 817 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

I don't love that you need to return the lookupCount here. I would probably encapsulate the logic for the cost of the extra lookup conditions in a helper function that takes the join and lookupCount as parameters and returns the cost (if join is not a LookupJoinExpr or doesn't have LookupExpr set, just return 0). That would also allow us to add a cost for multiple lookupCols in the future, which might be worth doing.

But I don't feel too strongly... if you want to leave it as-is, just be sure to add a comment to the function explaining the new return param.

Me neither but updating the cost in computeLookupJoinCost lets me avoid type cast shenanigans required if I did it from computeIndexLookupJoinCost and I thought the return complexity was the lesser of two evils. Happy to change it to whatever you think it more idiomatic.

Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach)


pkg/sql/opt/xform/coster.go, line 817 at r1 (raw file):

Previously, cucaroach (Tommy Reilly) wrote…

Me neither but updating the cost in computeLookupJoinCost lets me avoid type cast shenanigans required if I did it from computeIndexLookupJoinCost and I thought the return complexity was the lesser of two evils. Happy to change it to whatever you think it more idiomatic.

Yea, I still have a slight preference towards calling the function from inside computeIndexLookupJoinCost, even if it needs a cast inside. I think it's a bit more idiomatic in the opt code base, since we try hard to keep top level function interfaces as clean and consistent as possible.

But computeIndexLookupJoinCost is already a bit of a special snowflake, so as I said, I don't feel too strongly in this case. (But do add a comment explaining the return param if you leave it as-is.)

Copy link
Copy Markdown
Contributor Author

@cucaroach cucaroach left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)


pkg/sql/opt/xform/coster.go, line 817 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Yea, I still have a slight preference towards calling the function from inside computeIndexLookupJoinCost, even if it needs a cast inside. I think it's a bit more idiomatic in the opt code base, since we try hard to keep top level function interfaces as clean and consistent as possible.

But computeIndexLookupJoinCost is already a bit of a special snowflake, so as I said, I don't feel too strongly in this case. (But do add a comment explaining the return param if you leave it as-is.)

Okay I went with the type cast, definitely came out cleaner!

Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @cucaroach)


pkg/sql/opt/xform/coster.go, line 924 at r3 (raw file):

		cost /= 2.5
	}
	cost += lookupExprCost(join, lookupCount)

I think it would be better to instead make this function not depend on lookupCount (just have it return 0 or cpuCostFactor * memo.Cost(len(lookupExpr.LookupExpr))), and add the result to perLookupCost above.

Copy link
Copy Markdown
Contributor Author

@cucaroach cucaroach left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)


pkg/sql/opt/xform/coster.go, line 924 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

I think it would be better to instead make this function not depend on lookupCount (just have it return 0 or cpuCostFactor * memo.Cost(len(lookupExpr.LookupExpr))), and add the result to perLookupCost above.

👍

Release note (performance improvement): adjust optimizer cost to include
cpu cost of lookupExprs when used by lookup joins.   When a lookup join
is chosen we can use two strategies, a simple 1 column lookup or a more
involved multi column lookup that makes more efficient use of indexes
but has higher CPU cost.  This change makes the cost model reflect that
extra cost.
@cucaroach
Copy link
Copy Markdown
Contributor Author

bors r=rytaft

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jun 30, 2021

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants