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

*: trace the execution of the insert operation #11667

Merged
merged 7 commits into from Aug 9, 2019

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

This simple insert operation takes 30ms! I'm trying to figure out why it's so damn slow.

insert into t values (21, 66, 6, 27, 23) on duplicate key update id = id + 1,a = a+1 , b = b+1, c = c+1, d = d+1;
mysql> show create table t;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                           |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id` int(11) NOT NULL,
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  `c` int(11) DEFAULT NULL,
  `d` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `i_a` (`a`),
  KEY `i_b` (`b`),
  KEY `i_c` (`c`),
  KEY `i_d` (`d`),
  UNIQUE KEY `i_ac` (`a`,`c`),
  UNIQUE KEY `i_bd` (`b`,`d`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from t;
+----+------+------+------+------+
| id | a    | b    | c    | d    |
+----+------+------+------+------+
| 11 |    7 |    8 |    9 |   10 |
| 12 |    6 |    7 |    8 |    9 |
| 21 |   16 |   17 |   18 |   19 |
+----+------+------+------+------+
3 rows in set (0.01 sec)

What is changed and how it works?

Add code to trace the execution of the insert operation.
I'd like to trace all operations that visit the network.

And here is why:

mysql> trace format = 'row' insert into t values (21, 66, 6, 27, 23) on duplicate key update id = id + 1,a = a+1 , b = b+1, c = c+1, d = d+1;
+-------------------------------------------------+-----------------+-------------+
| operation                                       | startTS         | duration    |
+-------------------------------------------------+-----------------+-------------+
| session.Execute                                 | 19:41:40.208872 | 30.3379ms   |
|   ├─session.ParseSQL                            | 19:41:40.208968 | 178.753µs   |
|   ├─executor.Compile                            | 19:41:40.209208 | 310.554µs   |
|   ├─session.runStmt                             | 19:41:40.209576 | 29.580799ms |
|   │ ├─executor.handleNoDelayExecutor            | 19:41:40.209624 | 5.633641ms  |
|   │ │ └─*executor.InsertExec.Next               | 19:41:40.209640 | 5.586157ms  |
|   │ │   ├─tikvTxn.BatchGet                      | 19:41:40.209789 | 1.142624ms  |
|   │ │   ├─batchCheck.initDupOldRowValue         | 19:41:40.210996 | 43.709µs    |
|   │ │   │ └─tikvTxn.BatchGet                    | 19:41:40.211011 | 3.167µs     |
|   │ │   └─InsertExec.updateDupRow               | 19:41:40.211071 | 4.126649ms  |
|   │ │     └─executor.updateRecord               | 19:41:40.211143 | 3.929455ms  |
|   │ │       ├─index.Create                      | 19:41:40.212195 | 933.116µs   |
|   │ │       └─index.Create                      | 19:41:40.213309 | 1.622564ms  |
|   │ └─session.CommitTxn                         | 19:41:40.215382 | 23.746444ms |
|   │   └─tikvTxn.Commit                          | 19:41:40.215403 | 23.690628ms |
|   │     ├─twoPhaseCommitter.prewriteKeys        | 19:41:40.215501 | 10.378328ms |
|   │     ├─tikvStore.getTimestampWithRetry       | 19:41:40.226032 | 1.411919ms  |
|   │     └─twoPhaseCommitter.commitKeys          | 19:41:40.227547 | 11.485828ms |
|   └─session.getTxnFuture                        | 19:41:40.208879 | 13.02µs     |
+-------------------------------------------------+-----------------+-------------+
19 rows in set (0.03 sec)

From the result, I can see that we still did not do a good job on the unique indices.

BatchGet twice in the batchChecker;
visit tikv in index.Create to check the unique conditions;
and I'm still investigating why prewrite & commit take so long...

WIP

Check List

Tests

  • No code

Code changes

  • Has exported function/method change

Side effects

  • Increased code complexity

Related changes

  • Need to cherry-pick to the release branch

It's better to cherry-pick this PR because without it we'll meet more conflicts.

@tiancaiamao tiancaiamao changed the title *: trace the execution of the insert operation [WIP] *: trace the execution of the insert operation Aug 8, 2019
@codecov
Copy link

codecov bot commented Aug 8, 2019

Codecov Report

Merging #11667 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #11667   +/-   ##
===========================================
  Coverage   81.6406%   81.6406%           
===========================================
  Files           428        428           
  Lines         93707      93707           
===========================================
  Hits          76503      76503           
  Misses        11788      11788           
  Partials       5416       5416

@tiancaiamao
Copy link
Contributor Author

I try to make the trace reaches to rpcClient.SendRequest
Now it looks like this:

mysql> trace format = 'row' insert into t values (43, 23, 12, 77, 32) on duplicate key update id = id + 1,a = a+1 , b = b+1, c = c+1, d = d+1;
+-------------------------------------------------+-----------------+-------------+
| operation                                       | startTS         | duration    |
+-------------------------------------------------+-----------------+-------------+
| session.Execute                                 | 16:24:19.572421 | 39.994665ms |
|   ├─session.ParseSQL                            | 16:24:19.572534 | 162.915µs   |
|   ├─executor.Compile                            | 16:24:19.572756 | 267.989µs   |
|   ├─session.runStmt                             | 16:24:19.573076 | 39.304889ms |
|   │ ├─executor.handleNoDelayExecutor            | 16:24:19.573119 | 6.052298ms  |
|   │ │ └─*executor.InsertExec.Next               | 16:24:19.573135 | 6.015389ms  |
|   │ │   ├─tikvTxn.BatchGet                      | 16:24:19.573350 | 1.543199ms  |
|   │ │   │ └─rpcClient.SendRequest               | 16:24:19.573403 | 1.423014ms  |
|   │ │   ├─batchCheck.initDupOldRowValue         | 16:24:19.574930 | 1.090234ms  |
|   │ │   │ └─tikvTxn.BatchGet                    | 16:24:19.574959 | 1.029877ms  |
|   │ │   │   └─rpcClient.SendRequest             | 16:24:19.574990 | 925.623µs   |
|   │ │   └─InsertExec.updateDupRow               | 16:24:19.576058 | 3.027731ms  |
|   │ │     └─executor.updateRecord               | 16:24:19.576247 | 2.749795ms  |
|   │ │       ├─rpcClient.SendRequest             | 16:24:19.576370 | 687.618µs   |
|   │ │       ├─index.Create                      | 16:24:19.577181 | 851.474µs   |
|   │ │       │ └─rpcClient.SendRequest           | 16:24:19.577207 | 735.859µs   |
|   │ │       └─index.Create                      | 16:24:19.578069 | 844.256µs   |
|   │ │         └─rpcClient.SendRequest           | 16:24:19.578108 | 721.879µs   |
|   │ └─session.CommitTxn                         | 16:24:19.579276 | 33.0969ms   |
|   │   └─tikvTxn.Commit                          | 16:24:19.579297 | 33.062775ms |
|   │     ├─twoPhaseCommitter.prewriteKeys        | 16:24:19.579425 | 29.441927ms |
|   │     │ └─rpcClient.SendRequest               | 16:24:19.579498 | 29.34235ms  |
|   │     ├─tikvStore.getTimestampWithRetry       | 16:24:19.608877 | 180.765µs   |
|   │     └─twoPhaseCommitter.commitKeys          | 16:24:19.609081 | 3.262209ms  |
|   │       └─rpcClient.SendRequest               | 16:24:19.609119 | 3.198497ms  |
|   └─session.getTxnFuture                        | 16:24:19.572427 | 23.292µs    |
+-------------------------------------------------+-----------------+-------------+
26 rows in set (0.04 sec)

@ngaut
Copy link
Member

ngaut commented Aug 8, 2019

Could you remove startTS column?

@tiancaiamao
Copy link
Contributor Author

/rebuild

@ngaut Although the tree reveals the call stack, the duration of the parent span duration may not always greater than it's children.
If we take concurrency into consideration, the startTS can give us more information.

For example:

mysql> trace format = 'row' select * from t;
+--------------------------------------------+-----------------+------------+
| operation                                  | startTS         | duration   |
+--------------------------------------------+-----------------+------------+
| session.Execute                            | 17:01:21.201420 | 1.575289ms |
|   ├─session.ParseSQL                       | 17:01:21.201505 | 58.211µs   |
|   ├─executor.Compile                       | 17:01:21.201604 | 513.539µs  |
|   ├─session.runStmt                        | 17:01:21.202175 | 747.428µs  |
|   │ ├─tableReader.Open                     | 17:01:21.202670 | 156.727µs  |
|   │ │ └─distsql.Select                     | 17:01:21.202712 | 62.119µs   |
|   │ │   └─rpcClient.SendRequest            | 17:01:21.202964 | 1.063196ms |
|   │ └─session.CommitTxn                    | 17:01:21.202878 | 14.917µs   |
|   ├─session.getTxnFuture                   | 17:01:21.201427 | 13.092µs   |
|   ├─*executor.TableReaderExecutor.Next     | 17:01:21.203028 | 1.314519ms |
|   └─*executor.TableReaderExecutor.Next     | 17:01:21.204439 | 8.407µs    |
+--------------------------------------------+-----------------+------------+
11 rows in set (0.00 sec)

distsql.Select spawn a goroutine to run the fetch operation which calls rpcClient.SendRequest, so the duration of distsql.Select (62.119µs) is smaller than rpcClient.SendRequest (1.063196ms)

If we run some asynchronous code, this can be misleading:

| session.Execute                            | 4ms |
|   ├─session.runStmt                        | 1ms   |
|   │ └─session.getTxnFuture                 | 100ms |
|   ├─session.CommitTxn                      | 1ms   |
|   ├─*executor.TableReaderExecutor.Next     | 1ms   |
|   └─*executor.TableReaderExecutor.Next     | 1ms   |

Does the whole process take 4ms or 100ms? which one?
We can be more clear about the asynchronous process with the startTS column:

| session.Execute                            | 17:01:21.202000 | 4ms |
|   ├─session.runStmt                        | 17:01:21.202100 | 1ms   |
|   │ └─session.getTxnFuture                 | 17:01:21.202106 | 100ms |
|   ├─session.CommitTxn                      | 17:01:21.202200 | 1ms   |
|   ├─*executor.TableReaderExecutor.Next     | 17:01:21.202300 | 1ms   |
|   └─*executor.TableReaderExecutor.Next     | 17:01:21.202400 | 1ms   |

@ngaut
Copy link
Member

ngaut commented Aug 8, 2019

Thanks

@tiancaiamao
Copy link
Contributor Author

PTAL @jackysp @lysu

Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

Please resolve the conflicts.

@tiancaiamao
Copy link
Contributor Author

Conflict resolved @jackysp

@lysu lysu self-requested a review August 9, 2019 05:47
Copy link
Collaborator

@lysu lysu left a comment

Choose a reason for hiding this comment

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

LGTM

@lysu lysu added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 9, 2019
Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

LGTM

@jackysp jackysp added the status/can-merge Indicates a PR has been approved by a committer. label Aug 9, 2019
@tiancaiamao
Copy link
Contributor Author

tiancaiamao commented Aug 9, 2019

/run-all-tests

1 similar comment
@sre-bot
Copy link
Contributor

sre-bot commented Aug 9, 2019

/run-all-tests

@sre-bot sre-bot merged commit d210889 into pingcap:master Aug 9, 2019
@tiancaiamao tiancaiamao deleted the trace-insert branch August 9, 2019 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/can-merge Indicates a PR has been approved by a committer. status/LGT1 Indicates that a PR has LGTM 1. type/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants