Skip to content

Conversation

hawkingrei
Copy link
Member

@hawkingrei hawkingrei commented May 20, 2025

What problem does this PR solve?

Issue Number: close #61155

Problem Summary:

When we use the cost trace's explain, it will calculate the cost twice. Unfortunately, when calculating secondly, the row count has been polluted by the first.

the first calculation:

1500000000*math.log2(46.17)*11.6+10000*math.log2(46.17)*11.6 = 96203219945.10272

1500000000 will become the 5.645217633333338e+07, because postOptimize‘s
tryEnableLateMaterialization can change the row count in the first plan generate.

func PushedDown(sel *PhysicalSelection, ts *PhysicalTableScan, selectedConds []expression.Expression, selectedSelectivity float64) {
// remove the pushed down conditions from selection
removeSpecificExprsFromSelection(sel, selectedConds)
// add the pushed down conditions to table scan
ts.LateMaterializationFilterCondition = selectedConds
// Update the row count of table scan after pushing down the conditions.
ts.StatsInfo().RowCount *= selectedSelectivity
}

the second calculation:

5.645217633333338e+07*math.log2(46.17)*11.6+10000*math.log2(46.17)*11.6 = 3621204637.5523133

So two formats have different costs.

What changed and how does it work?

Generate the cost trace in the first calculation. Remove the second calculation.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

planner: fix get wrong cost with cost tracer

修复使用 cost tracer 得到不正确的 cost 值

hawkingrei and others added 4 commits May 20, 2025 01:17
Signed-off-by: Weizhen Wang <[email protected]>
Signed-off-by: Weizhen Wang <[email protected]>
@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. sig/planner SIG: Planner and removed do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked labels May 20, 2025
@hawkingrei hawkingrei requested review from AilinKid and qw4990 May 20, 2025 02:58
@hawkingrei hawkingrei force-pushed the 61155_1 branch 2 times, most recently from d64104a to a64b07d Compare May 20, 2025 03:05
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 20, 2025
Copy link

codecov bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.4846%. Comparing base (9cc4672) to head (a64b07d).
Report is 175 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #61196        +/-   ##
================================================
+ Coverage   73.1823%   73.4846%   +0.3022%     
================================================
  Files          1724       1725         +1     
  Lines        477675     479888      +2213     
================================================
+ Hits         349574     352644      +3070     
+ Misses       106688     105835       -853     
+ Partials      21413      21409         -4     
Flag Coverage Δ
integration 42.6373% <100.0000%> (?)
unit 72.4701% <100.0000%> (+0.1011%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.7804% <ø> (ø)
parser ∅ <ø> (∅)
br 46.0916% <ø> (-1.6633%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels May 20, 2025
@hawkingrei hawkingrei changed the title planner: fix get wrong cost when use cost tracer planner: fix get wrong cost with cost tracer May 20, 2025
@hawkingrei hawkingrei requested a review from Copilot May 20, 2025 03:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where EXPLAIN FORMAT='cost_trace' would calculate plan cost twice (with a polluted row count on the second pass). It enables the optimize-tracing flag only during the initial build and removes the redundant recalculation, then adds a test helper to verify cost consistency.

  • Enable StmtCtx.EnableOptimizeTrace only when Format=cost_trace in buildExplain
  • Remove the second getPlanCost invocation in RenderResult
  • Add checkCost in TestQ1 to compare cost_trace vs verbose outputs and update test dependencies

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pkg/planner/core/planbuilder.go Wrap EnableOptimizeTrace in a conditional and defer restore for cost_trace
pkg/planner/core/common_plans.go Remove the extra getPlanCost call for cost_trace
pkg/planner/core/casetest/tpch/tpch_test.go Introduce checkCost helper, update TestQ1 to assert cost consistency
pkg/planner/core/casetest/tpch/BUILD.bazel Add @com_github_stretchr_testify//require to test dependencies
Comments suppressed due to low confidence (3)

pkg/planner/core/planbuilder.go:5553

  • [nitpick] The variable name origin is ambiguous; consider renaming it to oldEnableOptimizeTrace or similar to clarify its purpose.
origin := sctx.GetSessionVars().StmtCtx.EnableOptimizeTrace

pkg/planner/core/common_plans.go:1024

  • After removing the redundant cost-recalculation block, review and clean up any now-unused imports (e.g., optimizetrace, costusage) to keep the file tidy.
switch strings.ToLower(e.Format) {

pkg/planner/core/casetest/tpch/tpch_test.go:223

  • Consider adding a checkCost(t, tk, q4) call after this MPP-mode query to ensure cost_trace and verbose outputs remain consistent when tidb_enforce_mpp is enabled.
tk.MustQuery(briefFormat + q4).Check(testkit.Rows("Sort 1.00 root  olap.orders.o_orderpriority",

Copy link

ti-chi-bot bot commented May 20, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, qw4990

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 20, 2025
Copy link

ti-chi-bot bot commented May 20, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-05-20 03:20:58.661051043 +0000 UTC m=+59095.762218264: ☑️ agreed by qw4990.
  • 2025-05-20 05:27:01.533876501 +0000 UTC m=+66658.635043708: ☑️ agreed by AilinKid.

@ti-chi-bot ti-chi-bot bot merged commit e3478a5 into pingcap:master May 20, 2025
24 checks passed
@hawkingrei
Copy link
Member Author

/cherrypick release-8.5
/cherrypick release-8.1
/cherrypick release-7.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 20, 2025
@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request created to branch release-7.5: #61214.
But this PR has conflicts, please resolve them!

In response to this:

/cherrypick release-8.5
/cherrypick release-8.1
/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 20, 2025
@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request created to branch release-8.1: #61216.
But this PR has conflicts, please resolve them!

In response to this:

/cherrypick release-8.5
/cherrypick release-8.1
/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 20, 2025
@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request created to branch release-8.5: #61218.
But this PR has conflicts, please resolve them!

In response to this:

/cherrypick release-8.5
/cherrypick release-8.1
/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@hawkingrei
Copy link
Member Author

/cherrypick release-8.1
/cherrypick release-7.5

@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

In response to this:

/cherrypick release-8.1
/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jun 16, 2025
@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-8.1 from head ti-chi-bot:cherry-pick-61196-to-release-8.1: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-8.1."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

In response to this:

/cherrypick release-8.1
/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jun 16, 2025
@hawkingrei hawkingrei added needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. affects-8.1 This bug affects the 8.1.x(LTS) versions. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. labels Jun 16, 2025
@hawkingrei
Copy link
Member Author

/cherrypick release-7.5

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request could not be created: failed to create pull request against pingcap/tidb#release-8.1 from head ti-chi-bot:cherry-pick-61196-to-release-8.1: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-8.1."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jun 16, 2025
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jun 16, 2025
@ti-chi-bot
Copy link
Member

@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

In response to this:

/cherrypick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jun 16, 2025
hawkingrei added a commit to ti-chi-bot/tidb that referenced this pull request Jun 20, 2025
ti-chi-bot bot pushed a commit that referenced this pull request Jun 20, 2025
hawkingrei added a commit to ti-chi-bot/tidb that referenced this pull request Jul 14, 2025
hawkingrei added a commit to ti-chi-bot/tidb that referenced this pull request Jul 14, 2025
ti-chi-bot bot pushed a commit that referenced this pull request Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-8.1 This bug affects the 8.1.x(LTS) versions. approved lgtm needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

difference cost between format=verbose and format=cost_trace
4 participants