阻止读取时超过锁定等待超时

时间:2018-02-07 06:02:16

标签: mysql locking innodb

我从show full processlist;命令获得了以下输出。

+-------+----------+----------------------+-----------+---------+------+--------------+----------------------------------------------------+
| Id    | User     | Host                 | db        | Command | Time | State        | Info                                               |
+-------+----------+----------------------+-----------+---------+------+--------------+----------------------------------------------------+
| 59589 | rdsadmin | localhost:62321      | mysql     | Sleep   |    3 |              | NULL                                               |
| 69883 | user1   | 118.63.145.213:49735 | dev_pq    | Sleep   |  720 |              | NULL                                               |
| 69889 | user1   | 118.63.145.213:50635 | dev_pq    | Sleep   |  720 |              | NULL                                               |
| 69945 | user1   | 118.63.145.213:51472 | dev_pq    | Execute |   23 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 69950 | user1   | 118.63.145.213:51481 | dev_pq    | Execute |   35 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 69956 | user1   | 118.63.145.213:51521 | dev_pq    | Sleep   | 3147 |              | NULL                                               |
| 69961 | user1   | 118.63.145.213:51561 | dev_pq    | Sleep   | 3090 |              | NULL                                               |
| 70014 | user1   | 118.63.145.213:51579 | dev_pq    | Sleep   | 2975 |              | NULL                                               |
| 70028 | user1   | 118.63.145.213:51595 | dev_pq    | Query   |    0 | starting     | show full processlist                              |
| 70030 | user1   | 118.63.145.213:51629 | dev_pq    | Execute |   33 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 70032 | user1   | 118.63.145.213:51639 | dev_pq    | Sleep   | 2589 |              | NULL                                               |
| 70047 | user1   | 118.63.145.213:51746 | dev_pq    | Execute |   35 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 70064 | user1   | 118.63.145.213:52076 | dev_pq    | Execute |   35 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 70066 | user1   | 118.63.145.213:52091 | dev_pq    | Sleep   |  983 |              | NULL                                               |
| 70068 | user1   | 118.63.145.213:52095 | dev_pq    | Sleep   |  970 |              | NULL                                               |
| 70070 | user1   | 118.63.145.213:52127 | dev_pq    | Execute |   35 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 70071 | user1   | 118.63.145.213:52128 | dev_pq    | Sleep   |  752 |              | NULL                                               |
| 70072 | user1   | 118.63.145.213:52129 | dev_pq    | Execute |   30 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
| 70073 | user1   | 118.63.145.213:52160 | dev_pq    | Execute |   36 | Sending data | SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE |
+-------+----------+----------------------+-----------+---------+------+--------------+----------------------------------------------------+

所有运行SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE查询的进程都处于Sending data状态。

我有REPEATABLE-READ作为事务隔离级别。

SHOW ENGINE INNODB STATUS\G

输出
InnoDB |      |
=====================================
2018-02-07 05:00:52 0x2b54587d2700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 41 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 329840 srv_active, 0 srv_shutdown, 12533664 srv_idle
srv_master_thread log flush and writes: 12861366
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 117121
OS WAIT ARRAY INFO: signal count 116944
RW-shared spins 0, rounds 232727, OS waits 116220
RW-excl spins 0, rounds 3874, OS waits 205
RW-sx spins 54, rounds 1620, OS waits 51
Spin rounds per wait: 232727.00 RW-shared, 3874.00 RW-excl, 30.00 RW-sx
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2018-02-06 06:21:36 0x2b54587d2700 Transaction:
TRANSACTION 680810, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
7 lock struct(s), heap size 1136, 27 row lock(s), undo log entries 1
MySQL thread id 69111, OS thread handle 47641261844224, query id 18020284 172.31.10.153 apremalal update
INSERT INTO `m_readings` (`id`, `m_id`, `a_id`, `type`, `content`, `checksum`, `fingerprint`, `deleted`, `created`, `updated`) VALUES ('E1B369DD-F981-403C-AF83-6F12AF6AF4C8', 'E3DD4DAB-3B08-4552-9D8A-42C84F96051E', 'CAA822F8-71B6-4CFB-B840-B344894BED1A', 'PunditBScan', '{\"columnIndex\":13,\"rowIndex\":0}', '', '0ce51d5a2e75df3f80e417acb5e1621b0c0cb097', 0, 1517898096892, 1517898096892) ON DUPLICATE KEY UPDATE `id`='E1B369DD-F981-403C-AF83-6F12AF6AF4C8', `m_id`='E3DD4DAB-3B08-4552-9D8A-42C84F96051E', `a_id`='CAA822F8-71B6-4CFB-B840-B344894BED1A', `type`='PunditBScan', `content`='{\"columnIndex
Foreign key constraint fails for table `pq_sg_stg`.`m_readings`:
,
 CONSTRAINT `m_readings_ibfk_2` FOREIGN KEY (`a_id`) REFERENCES `m_attachments` (`id`) ON DELETE CASCADE
Trying to add in child table, in index a_id tuple:
DATA TUPLE: 2 fields;
0: len 36; hex 43414138323246382d373142362d344346422d423834302d423334343839344245443141; asc CAA822F8-71B6-4CFB-B840-B344894BED1A;;
1: len 36; hex 45314233363944442d463938312d343033432d414638332d364631324146364146344338; asc E1B369DD-F981-403C-AF83-6F12AF6AF4C8;;

But in parent table `pq_sg_stg`.`m_attachments`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 11; compact format; info bits 0
0: len 30; hex 43414138333146462d323543422d343738452d413031392d423339354535; asc CAA831FF-25CB-478E-A019-B395E5; (total 36 bytes);
1: len 6; hex 00000003d8f9; asc       ;;
2: len 7; hex bf000002123a70; asc      :p;;
3: len 30; hex 39413936394445332d343237332d343843462d393138412d453442304134; asc 9A969DE3-4273-48CF-918A-E4B0A4; (total 36 bytes);
4: len 30; hex 45424532433442332d463544332d344436302d393837462d393334423139; asc EBE2C4B3-F5D3-4D60-987F-934B19; (total 36 bytes);
5: len 30; hex 726177446174612020202020202020202020202020202020202020202020; asc rawData                       ; (total 36 bytes);
6: len 30; hex 72656164696e672020202020202020202020202020202020202020202020; asc reading                       ; (total 36 bytes);
7: len 30; hex 38394235393738372d443344452d343745342d413130372d413633363045; asc 89B59787-D3DE-47E4-A107-A6360E; (total 36 bytes);
8: len 1; hex 81; asc  ;;
9: len 8; hex 0000000059f3117f; asc     Y   ;;
10: len 8; hex 0000000059ffddae; asc     Y   ;;

------------
TRANSACTIONS
------------
Trx id counter 756231
Purge done for trx's n:o < 755831 undo n:o < 0 state: running but idle
History list length 38
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 329115370529728, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370538848, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370535200, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370525168, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370527904, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370526080, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 329115370522432, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 756230, ACTIVE 21 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69945, OS thread handle 47641732331264, query id 18804992 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 21 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756230 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756229, ACTIVE 33 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69960, OS thread handle 47641714726656, query id 18804982 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 33 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756229 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756228, ACTIVE 33 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69950, OS thread handle 47641672632064, query id 18804981 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 33 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756228 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756227, ACTIVE 33 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69962, OS thread handle 47641785853696, query id 18804980 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 33 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756227 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756226, ACTIVE 33 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69952, OS thread handle 47641785587456, query id 18804979 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 33 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756226 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756225, ACTIVE 33 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69959, OS thread handle 47640544352000, query id 18804978 119.73.135.210 apremalal Sending data
SELECT u_id FROM u_context WHERE u_id=? FOR UPDATE
------- TRX HAS BEEN WAITING 33 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 177 page no 3 n bits 72 index PRIMARY of table `dev_pq`.`u_context` trx id 756225 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 8; hex 8000000000000001; asc         ;;
1: len 6; hex 0000000a9b61; asc      a;;
2: len 7; hex b6000001850110; asc        ;;
3: len 8; hex 80000000000003ea; asc         ;;
4: len 4; hex 5a798a03; asc Zy  ;;
5: len 4; hex 5a798a03; asc Zy  ;;

------------------
---TRANSACTION 756192, ACTIVE 116 sec
1 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 70014, OS thread handle 47641745172224, query id 18804671 119.73.135.210 apremalal
---TRANSACTION 755754, ACTIVE 231 sec
1 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 69961, OS thread handle 47641830123264, query id 18797574 119.73.135.210 apremalal
---TRANSACTION 755747, ACTIVE 237 sec
2 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 69956, OS thread handle 47641784788736, query id 18794724 119.73.135.210 apremalal
Trx read view will not see trx with id >= 755751, sees < 755748
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
770 OS file reads, 3261672 OS file writes, 1406811 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 276707, node heap has 26 buffer(s)
Hash table size 276707, node heap has 113 buffer(s)
Hash table size 276707, node heap has 63 buffer(s)
Hash table size 276707, node heap has 87 buffer(s)
Hash table size 276707, node heap has 88 buffer(s)
Hash table size 276707, node heap has 89 buffer(s)
Hash table size 276707, node heap has 3 buffer(s)
Hash table size 276707, node heap has 15 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 920177692
Log flushed up to   920177692
Pages flushed up to 920177692
Last checkpoint at  920177683
0 pending log flushes, 0 pending chkp writes
715503 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 1099431936
Dictionary memory allocated 1542657
Buffer pool size   65536
Free buffers       37126
Database pages     27926
Old database pages 10145
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 34, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 726, created 27200, written 2273135
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 27926, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size   8192
Free buffers       4717
Database pages     3413
Old database pages 1239
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 144, created 3269, written 225945
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3413, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size   8192
Free buffers       4729
Database pages     3404
Old database pages 1236
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 7, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 48, created 3356, written 92984
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3404, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size   8192
Free buffers       4520
Database pages     3612
Old database pages 1313
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 4, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 49, created 3563, written 142992
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3612, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size   8192
Free buffers       4566
Database pages     3557
Old database pages 1293
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 6, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 114, created 3443, written 180175
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3557, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size   8192
Free buffers       4640
Database pages     3495
Old database pages 1270
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 79, created 3416, written 204421
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3495, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size   8192
Free buffers       4752
Database pages     3377
Old database pages 1226
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 3, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 94, created 3283, written 117388
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3377, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size   8192
Free buffers       4589
Database pages     3544
Old database pages 1288
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 14, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 109, created 3435, written 154248
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3544, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size   8192
Free buffers       4613
Database pages     3524
Old database pages 1280
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 89, created 3435, written 1154982
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3524, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Process ID=12179, Main thread ID=47641691956992, state: sleeping
Number of rows inserted 1416020, updated 448488, deleted 244034, read 5102556611
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.10 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

u_context

的表定义
CREATE TABLE IF NOT EXISTS `u_context` (
  `id`         BIGINT(20) NOT NULL AUTO_INCREMENT,
  `u_id`       BIGINT(20) NOT NULL,
  `created`    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated`    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;

问题是如何找到哪个事务获得了给定u_id的锁定以及为什么它没有释放锁定这么久?

2 个答案:

答案 0 :(得分:1)

只需查询表格information_schema.innodb_lock_waits即可。这看起来像这样的例子:

mysql > select * from information_schema.innodb_lock_waits\G
*************************** 1. row ***************************
requesting_trx_id: 99035158909
requested_lock_id: 99035158909:45763:3:17
  blocking_trx_id: 99035158907
 blocking_lock_id: 99035158907:45763:3:17
1 row in set (0.00 sec)

使用blocking_trx_id,您可以查询表information_schema.innodb_trx

mysql > select * from information_schema.innodb_trx where trx_id = 99035158907\G
*************************** 1. row ***************************
                    trx_id: 99035158907
                 trx_state: RUNNING
               trx_started: 2018-02-07 09:05:12
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 2
       trx_mysql_thread_id: 11163
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         trx_tables_locked: 0
          trx_lock_structs: 2
     trx_lock_memory_bytes: 360
           trx_rows_locked: 17
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
 trx_adaptive_hash_latched: 0
 trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)

你有trx_mysql_thread_id。这是你想杀死的主题。

mysql > kill 11163;

它没有释放锁的原因很简单,该线程中的事务仍在运行。你有一个长时间运行的查询,或者看起来根本没有运行任何东西。然后有人忘了commitrollback

详细了解information_schema表格here

答案 1 :(得分:1)

添加INDEX(u_id)

由于查询频繁且需要根据u_id查找并锁定行,因此索引很重要,即使表格很小。

有些行由于缺少索引而被锁定。

添加索引的缺点很少。

我们可以看到SHOW CREATE TABLE u_context;可能会有相关的评论。