它确实将行锁定在user. 您可以使用出色的innotop工具 来验证这一点。
- 运行 innotop 并按“L”键以显示 InnoDB 锁屏幕。
- 打开另一个会话,登录 MySQL 并开始交易。
- 执行您显示的更新,但不要提交。
- 在 innotop 屏幕中查看锁。
例如,我在运行 MySQL 5.5 的测试 VM 上创建了表 user 和 user_profile,并执行了上面列出的步骤。这是输出:
[RO] Locks (? for help) localhost, 08:34.568, InnoDB 10s :-), 0.10 QPS, 2/0/0 con/run/cac thds, 5.5.
__________________________________________ InnoDB Locks __________________________________________
ID Type Waiting Wait Active Mode DB Table Index Ins Intent Special
2 TABLE 0 00:00 02:35 IX test user 0
2 RECORD 0 00:00 02:35 X test user PRIMARY 0 rec but not gap
2 TABLE 0 00:00 02:35 IX test user_profile 0
2 RECORD 0 00:00 02:35 X test user_profile PRIMARY 0 rec but not gap