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

planner: rewrite count star to count not null column #39197

Merged
merged 23 commits into from
Nov 28, 2022

Conversation

elsa0520
Copy link
Contributor

@elsa0520 elsa0520 commented Nov 16, 2022

What problem does this PR solve?

Issue Number: close #37165

Problem Summary:

What is changed and how it works?

The main purpose of this pr is to improve the execution speed of count(), and rewrite count() to count (the narrowest non-null column) at the planning layer to achieve the purpose of improving performance.

  1. Added a countStarRewriter logical rule responsible for rewriting count(*) to count(the narrowest non-null column).
  2. When the columns of the datasource are empty, column pruning will supplement the narrowest non-null column instead of the row id column (in the case of TiFlash)

For detailed rewriting steps, see comments of countStarRewriter

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Nov 16, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • AilinKid
  • winoros

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 16, 2022
@elsa0520 elsa0520 marked this pull request as ready for review November 24, 2022 02:40
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 24, 2022
@qw4990 qw4990 added type/enhancement The issue or PR belongs to an enhancement. sig/planner SIG: Planner labels Nov 24, 2022
Copy link
Contributor

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

planner/core/rule_count_star_rewriter.go Show resolved Hide resolved
planner/core/physical_plan_test.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 24, 2022
parser/types/field_type.go Outdated Show resolved Hide resolved
planner/core/rule_column_pruning.go Outdated Show resolved Hide resolved
planner/core/rule_column_pruning.go Outdated Show resolved Hide resolved
planner/core/rule_count_star_rewriter.go Outdated Show resolved Hide resolved
planner/core/rule_column_pruning.go Outdated Show resolved Hide resolved
planner/core/rule_count_star_rewriter.go Show resolved Hide resolved
planner/core/rule_count_star_rewriter.go Outdated Show resolved Hide resolved
planner/core/rule_count_star_rewriter.go Outdated Show resolved Hide resolved
@elsa0520
Copy link
Contributor Author

/run-build

@elsa0520
Copy link
Contributor Author

/run-check_dev

@elsa0520
Copy link
Contributor Author

/run-check_dev_2

@elsa0520
Copy link
Contributor Author

/run-mysql-test

@elsa0520
Copy link
Contributor Author

/run-unit-test

@elsa0520
Copy link
Contributor Author

/run-all-tests

1 similar comment
@wuhuizuo
Copy link
Contributor

/run-all-tests

@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=2026

@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=2026

@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

2 similar comments
@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

1 similar comment
@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 28, 2022
@winoros
Copy link
Member

winoros commented Nov 28, 2022

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 83dd8a3

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 28, 2022
@elsa0520
Copy link
Contributor Author

/run-mysql-test tidb-test=pr/2026

@ti-chi-bot ti-chi-bot merged commit 37bd052 into pingcap:master Nov 28, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Nov 28, 2022

TiDB MergeCI notify

🔴 Bad News! New failing [2] after this pr merged.
These new failed integration tests seem to be caused by the current PR, please try to fix these new failed integration tests, thanks!

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/integration-common-test 🟥 failed 8, success 9, total 17 17 min New failing
idc-jenkins-ci-tidb/common-test 🟥 failed 2, success 9, total 11 10 min New failing
idc-jenkins-ci/integration-cdc-test 🟢 all 40 tests passed 21 min Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 12 min Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 6 min 15 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 6 min 1 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 54 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 4 min 44 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 3 min 21 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Comment on lines +46 to +51
Case2 there is no columns from datasource
Query: select count(*) from table
ColumnPruningRule: pick k1 as the narrowest not null column from origin table @Function.preferNotNullColumnFromTable
datasource.columns: k1
CountStarRewriterRule: rewrite count(*) -> count(k1)
Rewritten Query: select count(k1) from table
Copy link
Contributor

Choose a reason for hiding this comment

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

Any benchmark for this case? In TiFlash, there are extra three columns <row_id, version, delmark> which are used for MVCC. Therefore, if there are multi version of data, we need to scan <row_id, version, delmark> each query additionally. For count(1) we need to scan <row_id, version, delmark> and return row_id to aggregation, but for count(k) we need to scan <row_id, version, delmark, k1> and return k1 to aggregation. So the performance may degrade when there are multi version of data in TiFlash and k1 is wide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am doing performance testing.
Do you mean that in the case of multiple versions, even if the count(*) does not select the row_id column, the final read will still read the row_id column?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, we need to use it to filter out other data of other versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

planner: optimize the performance of SELECT count(*)
9 participants