MySQL InnoDB locking only the affected rows?

旧巷老猫 提交于 2019-12-06 06:27:38

问题


I have an InnoDB table containing users, like this:

+--------------+-----------------------+
| user_id      | name                  |
+--------------+-----------------------+
| 1            | Bob                   |
| 1            | Marry                 |
| 2            | Bob                   |
| 1            | John                  |
| 3            | Bob                   |
| 2            | Marry                 |
+--------------+-----------------------+

On each insert I increment user_id by 1 so for example, the next user_id for Bob will be 4

I use this query to do that:

INSERT INTO users (user_id, name)
SELECT 1 + coalesce((SELECT max(user_id) FROM users WHERE name='Bob'), 0), 'Bob';

Now I need to make sure that two different users don't add a 'Bob' at the same time. I don't want any two Bobs with user_id 4. Each of Bob's user ids must be different.

Is is possible to write and update lock all of Bob's rows when the above insert query is run? I cannot lock the whole table because many other users still need full access to their rows. Also, all rows should be readable at all times. How would I do this?


回答1:


Assuming InnoDB here, you can use SELECT ... LOCK IN SHARE MODE to lock the rows when you select them.




回答2:


To solve the first part of your question, create a unique index on user_id and name.

alter table `users` add unique index ak_user_id_name(user_id,name);

That will prevent duplicate user_id,name records.




回答3:


The answer to this question is the same as this one. InnoDB will lock more rows here than you expect for safety with statement-based binary logging.

The solution: use row based binary logging with the read-committed isolation level.



来源:https://stackoverflow.com/questions/1590648/mysql-innodb-locking-only-the-affected-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!