1 表无索引 update任何行,不提交简直是灾难
1 在不通过索引条件查询时,Innodb会锁定表中所有记录。
session 1
set autocommit=0;
update ta set user='chenliguo' where user='rp1';
session 2
update ta set user='wangbaosong' where user='root';
对于索引的表,session2 会被session 1 锁住。
mysql> select * from data_lock_waits\G
*************************** 1. row ***************************
ENGINE: INNODB
REQUESTING_ENGINE_LOCK_ID: 282791232:0:43:2:242839520
REQUESTING_ENGINE_TRANSACTION_ID: 434473
REQUESTING_THREAD_ID: 48
REQUESTING_EVENT_ID: 31
REQUESTING_OBJECT_INSTANCE_BEGIN: 242839520
BLOCKING_ENGINE_LOCK_ID: 282792056:0:43:2:242842776
BLOCKING_ENGINE_TRANSACTION_ID: 434472
BLOCKING_THREAD_ID: 49
BLOCKING_EVENT_ID: 14
BLOCKING_OBJECT_INSTANCE_BEGIN: 242842776s
mysql> desc performance_schema.threads;
+---------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------+-------+
| THREAD_ID | bigint unsigned | NO | PRI | NULL | |
| NAME | varchar(128) | NO | MUL | NULL | |
| TYPE | varchar(10) | NO | | NULL | |
| PROCESSLIST_ID | bigint unsigned | YES | MUL | NULL | |
| PROCESSLIST_USER | varchar(32) | YES | MUL | NULL | |
| PROCESSLIST_HOST | varchar(255) | YES | MUL | NULL | |
| PROCESSLIST_DB | varchar(64) | YES | | NULL | |
| PROCESSLIST_COMMAND | varchar(16) | YES | | NULL | |
| PROCESSLIST_TIME | bigint | YES | | NULL | |
| PROCESSLIST_STATE | varchar(64) | YES | | NULL | |
| PROCESSLIST_INFO | longtext | YES | | NULL | |
| PARENT_THREAD_ID | bigint unsigned | YES | | NULL | |
| ROLE | varchar(64) | YES | | NULL | |
| INSTRUMENTED | enum('YES','NO') | NO | | NULL | |
| HISTORY | enum('YES','NO') | NO | | NULL | |
| CONNECTION_TYPE | varchar(16) | YES | | NULL | |
| THREAD_OS_ID | bigint unsigned | YES | MUL | NULL | |
| RESOURCE_GROUP | varchar(64) | YES | MUL | NULL | |
+---------------------+------------------+------+-----+---------+-------+
18 rows in set (0.00 sec)
mysql> desc performance_schema.processlist;
+---------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------+------+-----+---------+-------+
| ID | bigint unsigned | NO | PRI | NULL | |
| USER | varchar(32) | YES | | NULL | |
| HOST | varchar(255) | YES | | NULL | |
| DB | varchar(64) | YES | | NULL | |
| COMMAND | varchar(16) | YES | | NULL | |
| TIME | bigint | YES | | NULL | |
| STATE | varchar(64) | YES | | NULL | |
| INFO | longtext | YES | | NULL | |
+---------+-----------------+------+-----+---------+-------+
8 rows in set (0.01 sec)
select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from performance_schema.data_lock_waits a,performance_schema.threads c
where a.REQUESTING_THREAD_ID=c.thread_id
union all
select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from performance_schema.data_lock_waits a,performance_schema.threads c
where a.BLOCKING_THREAD_ID=c.thread_id;
mysql> select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from per
formance_schema.data_lock_waits a,performance_schema.threads c
-> where a.REQUESTING_THREAD_ID=c.thread_id
-> union all
-> select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from per
formance_schema.data_lock_waits a,performance_schema.threads c
-> where a.BLOCKING_THREAD_ID=c.thread_id;
+------------------+------------------+---------------------+-------------------+
| processlist_user | processlist_host | processlist_command | processlist_state |
+------------------+------------------+---------------------+-------------------+
| root | localhost | Query | updating |
| root | localhost | Sleep | NULL |
+------------------+------------------+---------------------+-------------------+
2 rows in set (0.00 sec)
mysql>
2 mysql行锁是针对索引加的锁,不是针对所有记录加的锁,索引虽然访问的是不同行的记录,但是如果使用相同的索引键,会出现锁冲突的。
表tab1(id int,name varchar(100)) id列有索引,name列无索引。
则update1
update tab1 set name='1' where id=1 and name='CHENLIGUO';
update tab1 set name='2' where id=1 and name='LIZONGYUAN'
会发生阻塞。