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

Extremely inefficient GetWarnings() makes the query slow #39702

Closed
tiancaiamao opened this issue Dec 7, 2022 · 2 comments · Fixed by #39742
Closed

Extremely inefficient GetWarnings() makes the query slow #39702

tiancaiamao opened this issue Dec 7, 2022 · 2 comments · Fixed by #39742
Assignees
Labels
severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@tiancaiamao
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Originally encountered by an user https://asktug.com/t/topic/994126
A minimal reproduce can be:

CREATE TABLE `t2` (`col` decimal(20,4) DEFAULT NULL);
INSERT INTO `t2` VALUES (round(rand() * 0.49 + 0.0123, 4));
....  (repeat several times)
INSERT INTO `t2` SELECT * FROM `t2`;
.... (repeat several times to construct enough data)

CREATE TABLE `t1` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `c1` decimal(20,2) DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
)
INSERT INTO t1 (c1) SELECT * FROM t2;

2. What did you expect to see? (Required)

The last query finish in maybe 5~10s

3. What did you see instead (Required)

It takes nearly 8min to run the query!

mysql> insert into t1 (c1) select * from t2;
Query OK, 491520 rows affected, 65535 warnings (7 min 53.87 sec)
Records: 491520  Duplicates: 0  Warnings: 65535

4. What is your TiDB version? (Required)

nightly

mysql> select tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v6.5.0-alpha
Edition: Community
Git Commit Hash: dc6d9a01112a54af1ac7d0200521ea20b6d09b30
Git Branch: heads/refs/tags/v6.5.0-alpha
UTC Build Time: 2022-12-06 14:32:10
GoVersion: go1.19.3
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
@tiancaiamao tiancaiamao added the type/bug The issue is confirmed as a bug. label Dec 7, 2022
@tiancaiamao tiancaiamao self-assigned this Dec 7, 2022
@tiancaiamao tiancaiamao added the sig/sql-infra SIG: SQL Infra label Dec 7, 2022
@tiancaiamao
Copy link
Contributor Author

image

@tiancaiamao
Copy link
Contributor Author

2 necessary conditions to trigger this:

  • generate warning during execution
  • check system variables (fill default timestamp column value checks the @@timestamp variable here)

The root cause is that GetWarnings() use a copy on read which is too inefficient!

func (sc *StatementContext) GetWarnings() []SQLWarn {
sc.mu.Lock()
defer sc.mu.Unlock()
warns := make([]SQLWarn, len(sc.mu.warnings))
copy(warns, sc.mu.warnings)
return warns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants