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

statistics: Do not create pseudo statistics for the auto-analysis check process #51479

Merged

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Mar 4, 2024

What problem does this PR solve?

Issue Number: ref #50132

Problem Summary:

What changed and how does it work?

In this PR, we solved two problems:

  1. We shouldn't create pseudo stats during auto-analyze check processing. We only need the real row count of tables, so no need for pseudo stats. This would help us to reduce memory consumption and alleviate GC pressure.
  2. We should analyze static partitions one by one instead of analyzing the entry global table again and again.

So I made the following changes:

  1. I changed the refresher to use GetPartitionStatsForAutoAnalyze and GetTableStatsForAutoAnalyze to avoid creating the pseudo stats.
  2. I added a new type of job for the static partition. It will execute the analysis jobs in these ways:
    a. analyze table %n.%n partition %n
    b. analyze table %n.%n partition %n index %n

In the old implementation, we wrongly analyze the entire table.

I also did some refactorings to help us construct the job.

  • NewStaticPartitionTableAnalysisJob
  • NewNonPartitionedTableAnalysisJob
  • NewDynamicPartitionTableAnalysisJob

I think I can use generic to reduce the confusion of the job structure, but this PR is already big enough. So I will do it in my following PR.

Check List

Tests

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

Copy link

ti-chi-bot bot commented Mar 4, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-tests-checked 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. labels Mar 4, 2024
Copy link

tiprow bot commented Mar 4, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 4, 2024
@Rustin170506 Rustin170506 changed the title statistics: Do not create pseudo statistics for the auto-analysis check process WIP: statistics: Do not create pseudo statistics for the auto-analysis check process Mar 4, 2024
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/needs-tests-checked labels Mar 5, 2024
@Rustin170506 Rustin170506 changed the title WIP: statistics: Do not create pseudo statistics for the auto-analysis check process statistics: Do not create pseudo statistics for the auto-analysis check process Mar 5, 2024
@ti-chi-bot ti-chi-bot 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 Mar 5, 2024
@Rustin170506 Rustin170506 marked this pull request as ready for review March 5, 2024 03:21
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Mar 5, 2024
Copy link

codecov bot commented Mar 5, 2024

Codecov Report

Merging #51479 (1d025d1) into master (62afab3) will increase coverage by 2.2252%.
Report is 28 commits behind head on master.
The diff coverage is 71.7514%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #51479        +/-   ##
================================================
+ Coverage   70.7337%   72.9590%   +2.2252%     
================================================
  Files          1462       1464         +2     
  Lines        435276     440243      +4967     
================================================
+ Hits         307887     321197     +13310     
+ Misses       108095      99013      -9082     
- Partials      19294      20033       +739     
Flag Coverage Δ
integration 48.9482% <0.0000%> (?)
unit 70.5100% <71.7514%> (-0.0258%) ⬇️

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

Components Coverage Δ
dumpling 53.9957% <ø> (ø)
parser ∅ <ø> (∅)
br 51.8399% <ø> (+6.0073%) ⬆️

Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

🔢 Self-check (PR reviewed by myself and ready for feedback.)

@Rustin170506
Copy link
Member Author

Tested locally:

  1. Enable priority queue: SET GLOBAL tidb_enable_auto_analyze_priority_queue=ON;
  2. Create a table and insert data:
import pymysql
import random

# Connect to TiDB
conn = pymysql.connect(host='localhost', port=4000, user='root', passwd='', db='test')
cursor = conn.cursor()

# Create partitioned table
cursor.execute("""
    CREATE TABLE users (
      id INT NOT NULL,
      name VARCHAR(50) NOT NULL,
      age INT NOT NULL,
      country VARCHAR(2) NOT NULL
    )
    PARTITION BY RANGE (age) (
      PARTITION p0 VALUES LESS THAN (20),
      PARTITION p1 VALUES LESS THAN (40),
      PARTITION p2 VALUES LESS THAN (60)
    );
""")

# Generate and insert data
for i in range(2000):
  id = i
  name = 'User {}'.format(i)
  age = random.randint(15, 55)
  country = random.choice(['US', 'CN', 'IN'])

  cursor.execute("INSERT INTO users VALUES (%s, %s, %s, %s)", (id, name, age, country))

conn.commit()

print("Partitioned table created and data inserted successfully!")

conn.close()
  1. Check the log
[2024/03/05 15:13:12.300 +08:00] [INFO] [refresher.go:94] ["Auto analyze triggered"] [category=stats] [job="TableAnalysisJob: {AnalyzeType: partition, Partitions: p1, p2, Schema: test, Table: users, TableID: 104, TableStatsVer: 2, ChangePercentage: 1.00, Weight: 1.3792}"]
[2024/03/05 15:13:12.340 +08:00] [INFO] [save.go:200] ["incrementally update modifyCount"] [category=stats] [tableID=106] [curModifyCnt=3896] [results.BaseModifyCnt=3896] [modifyCount=0]
[2024/03/05 15:13:12.341 +08:00] [INFO] [save.go:222] ["directly update count"] [category=stats] [tableID=106] [results.Count=3896] [count=3896]
[2024/03/05 15:13:12.495 +08:00] [INFO] [analyze.go:745] ["analyze table `test`.`users` has finished"] [partition=p1] ["job info"="auto analyze table all columns with 256 buckets, 500 topn, 1 samplerate"] ["start time"=2024/03/05 15:13:12.315 +08:00] ["end time"=2024/03/05 15:13:12.493 +08:00] [cost=178.039666ms] ["sample rate reason"="use min(1, 110000/3896) as the sample-rate=1"]
[2024/03/05 15:13:12.497 +08:00] [INFO] [save.go:200] ["incrementally update modifyCount"] [category=stats] [tableID=107] [curModifyCnt=3118] [results.BaseModifyCnt=3118] [modifyCount=0]
[2024/03/05 15:13:12.497 +08:00] [INFO] [save.go:222] ["directly update count"] [category=stats] [tableID=107] [results.Count=3118] [count=3118]
[2024/03/05 15:13:12.620 +08:00] [INFO] [analyze.go:745] ["analyze table `test`.`users` has finished"] [partition=p2] ["job info"="auto analyze table all columns with 256 buckets, 500 topn, 1 samplerate"] ["start time"=2024/03/05 15:13:12.339 +08:00] ["end time"=2024/03/05 15:13:12.618 +08:00] [cost=278.853375ms] ["sample rate reason"="use min(1, 110000/3118) as the sample-rate=1"]
[2024/03/05 15:13:12.713 +08:00] [WARN] [global_stats.go:143] ["missing partition stats when merging global stats"] [table=users] [item=columns] [missing="[\"partition `p0` column `id` hist and topN\",\"partition `p0` column `name` hist and topN\",\"partition `p0` column `age` hist and topN\",\"partition `p0` column `country` hist and topN\"]"]

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 5, 2024
…ck process

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: ignore nil pseudo defs

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: add CreateStaticPartitionAnalysisJob

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

feat: analyze static partition

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

test: add more cases

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

test: add more cases

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

chore: rename

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

test: add tests for stringer

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: only check static partition

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

refactor: use crater

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: lint

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

test: add more cases

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix:build

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: build

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: remove useless code

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: do not crate pseudo stats

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

chore: remove todo

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

fix: use correct id

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

test: add more cases

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

chore: rename

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

chore: rename

Signed-off-by: hi-rustin <rustin.liu@gmail.com>

chore: rename

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@Rustin170506 Rustin170506 force-pushed the rustin-patch-auto-analyze-stats branch from d6d6c74 to 1d025d1 Compare March 6, 2024 02:30
Copy link

ti-chi-bot bot commented Mar 6, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hawkingrei, 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 Mar 6, 2024
Copy link

ti-chi-bot bot commented Mar 6, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-03-05 09:50:10.336478295 +0000 UTC m=+160637.358724683: ☑️ agreed by qw4990.
  • 2024-03-06 03:49:44.440413639 +0000 UTC m=+225411.462660026: ☑️ agreed by hawkingrei.

@hawkingrei
Copy link
Member

/retest

@ti-chi-bot ti-chi-bot bot merged commit 9b255d5 into pingcap:master Mar 6, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved component/statistics lgtm 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants