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

executor: only rebase auto increment ID when needed #7515

Merged
merged 8 commits into from
Aug 31, 2018

Conversation

jackysp
Copy link
Member

@jackysp jackysp commented Aug 28, 2018

What problem does this PR solve?

Fix #7422 . It is better to compare the value of the auto-increment ID before rebasing it in updateRecord. Update an old value which was inserted from another TiDB when the auto-increment column has no change does not need to rebase its ID. After this PR, the increasing speed of the auto-increment ID value will be slower than before.

What is changed and how it works?

Compare the values before rebasing the ID.

Check List

Tests

Code changes

  • Has exported function/method change

Side effects

  • Increased code complexity

Related changes

  • Need to be included in the release note

PTAL @zimulala @coocood

@jackysp jackysp added the sig/execution SIG execution label Aug 28, 2018
@jackysp
Copy link
Member Author

jackysp commented Aug 28, 2018

/run-all-tests

c.Assert(err, IsNil)
c.Assert(s.ctx.Txn().Commit(context.Background()), IsNil)

tk.MustExec(`update t set b = 3 where a = 30001;`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add comments to this case,

Copy link
Contributor

@winkyao winkyao left a comment

Choose a reason for hiding this comment

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

Reset LGTM

@jackysp
Copy link
Member Author

jackysp commented Aug 28, 2018

PTAL @winkyao

Copy link
Contributor

@winkyao winkyao left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@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/LGT2 Indicates that a PR has LGTM 2. label Aug 28, 2018
Copy link
Contributor

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

it seems more complex...hold on

cmp, err := newData[i].CompareDatum(sc, &oldData[i])
if err != nil {
return false, handleChanged, newHandle, 0, errors.Trace(err)
}
// Rebase auto increment id if the field is changed.
if mysql.HasAutoIncrementFlag(col.Flag) {
Copy link
Member

Choose a reason for hiding this comment

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

How about

if mysql.HasAutoIncrementFlag(col.Flag) && cmp != 0 {

Copy link
Member Author

Choose a reason for hiding this comment

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

If cmp == 0, we still need to set the last_insert_id. Actually, we already have the cases at line 595 to 627.

Copy link
Contributor

Choose a reason for hiding this comment

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

if cmp != 0 still can not change last_insert_id when under on duplicate key update.

tidb:

mysql> show create table ti2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                           |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ti2   | CREATE TABLE `ti2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `v` int(11) DEFAULT NULL,
  `idx` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx` (`idx`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin AUTO_INCREMENT=30001 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from ti2;
+----+------+------+
| id | v    | idx  |
+----+------+------+
|  1 |    1 |    1 |
+----+------+------+
1 row in set (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into ti2 (idx) values (1) on duplicate key update id = 3;
Query OK, 3 rows affected (3.78 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

mysql:

mysql> show create table ti2;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                    |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ti2   | CREATE TABLE `ti2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `v` int(11) DEFAULT NULL,
  `idx` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx` (`idx`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from ti2;
+----+------+------+
| id | v    | idx  |
+----+------+------+
|  1 |    1 |    1 |
+----+------+------+
1 row in set (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into ti2 (idx) values (1) on duplicate key update id = 3;
Query OK, 2 rows affected (0.01 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, you're right. I think we need another pr to fix it.

// For issue 7422.
// There is no need to do the rebase when updating a record if the auto-increment ID not changed.
// This could make the auto ID increasing speed slower.
func (s *testSuite) TestRebaseIfNeeded(c *C) {
Copy link
Contributor

@lysu lysu Aug 28, 2018

Choose a reason for hiding this comment

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

Maybe need add insert on duplicate key update testcase, and check last_insert_id() value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Already have the cases at line 595 to 627.

Copy link
Contributor

@lysu lysu Aug 29, 2018

Choose a reason for hiding this comment

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

I means rebase auto_increment in on duplicate key update, current case seems not update id? e.g. use duplicate on second unique index then update primary key...

@lysu lysu removed the status/LGT2 Indicates that a PR has LGTM 2. label Aug 29, 2018
@jackysp
Copy link
Member Author

jackysp commented Aug 30, 2018

PTAL @lysu

@coocood
Copy link
Member

coocood commented Aug 30, 2018

LGTM

@jackysp
Copy link
Member Author

jackysp commented Aug 31, 2018

/run-all-tests

@jackysp jackysp merged commit 720e823 into pingcap:master Aug 31, 2018
@jackysp jackysp deleted the rebase-if-needed branch September 7, 2018 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants