0

クエリを使用してテーブルを更新しようとすると

 UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703

この特定のスレッド (3086C6) は、別のスレッド (3086C0) が同じロックを持っているため、ロックで待機します。テーブルは、エイリアスで一意であり、heroes_id でプライマリ インデックスです。私は他のプロセスを実行しておらず、「プロセスリストを表示」しています。それを確認します。

2 つのスレッドは、同じデータ項目 "283703" (account_id でプライマリ インデックス) を X ロックでロックしていることを示しています。3086C0 はどのように生成されたのですか?

person.heroes には、別のテーブルを更新するトリガーが 1 つあります。

    sql>show engine innodb status;
    SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 388, signal count 394
Mutex spin waits 128, rounds 1354, OS waits 39
RW-shared spins 365, rounds 10714, OS waits 345
RW-excl spins 0, rounds 144, OS waits 4
Spin rounds per wait: 10.58 mutex, 29.35 RW-shared, 144.00 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 3086C7
Purge done for trx's n:o < 3086B4 undo n:o < 0
History list length 1690
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 3086C3, not started
MySQL thread id 191, OS thread handle 0x7ff590948700, query id 48008 192.168.104.19 game
---TRANSACTION 3086C4, not started
MySQL thread id 192, OS thread handle 0x7ff58fd18700, query id 48012 192.168.104.19 game
---TRANSACTION 3079C4, not started
MySQL thread id 164, OS thread handle 0x7ff590885700, query id 44155 192.168.0.12 game
---TRANSACTION 308597, not started
MySQL thread id 167, OS thread handle 0x7ff5908c6700, query id 47224 192.168.0.12 game
---TRANSACTION 307FCF, not started
MySQL thread id 166, OS thread handle 0x7ff624054700, query id 45727 192.168.0.12 game
---TRANSACTION 307FC9, not started
MySQL thread id 163, OS thread handle 0x7ff590b91700, query id 45717 192.168.0.12 game
---TRANSACTION 307A1C, not started
MySQL thread id 155, OS thread handle 0x7ff62692d700, query id 45722 192.168.0.11 game
---TRANSACTION 3085ED, not started
MySQL thread id 146, OS thread handle 0x7ff590b50700, query id 47411 192.168.0.11 game
---TRANSACTION 308677, not started
MySQL thread id 130, OS thread handle 0x7ff590b0f700, query id 48016 localhost root
show engine innodb status
---TRANSACTION 303B8E, not started
MySQL thread id 125, OS thread handle 0x7ff590a4c700, query id 13780 localhost root
---TRANSACTION 308615, not started
MySQL thread id 122, OS thread handle 0x7ff590a8d700, query id 47458 192.168.0.11 game
---TRANSACTION 30865E, not started
MySQL thread id 120, OS thread handle 0x7ff590a0b700, query id 47545 192.168.0.11 game
---TRANSACTION 3086C6, ACTIVE 3 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 193, OS thread handle 0x7ff6268ec700, query id 48015 192.168.104.19 game Updating
UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703
Trx read view will not see trx with id >= 3086C7, sees < 3086C0
------- TRX HAS BEEN WAITING 3 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C6 lock_mode X locks rec but not gap waiting
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

------------------
TABLE LOCK table `person`.`heroes` trx id 3086C6 lock mode IX
RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C6 lock_mode X locks rec but not gap waiting
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

---TRANSACTION 3086C0, ACTIVE 3 sec
3 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 190, OS thread handle 0x7ff590ace700, query id 47999 192.168.104.19 game
TABLE LOCK table `person`.`heroes` trx id 3086C0 lock mode IX
RECORD LOCKS space id 783 page no 18 n bits 624 index `alias` of table `person`.`heroes` trx id 3086C0 lock_mode X locks rec but not gap
Record lock, heap no 508 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 1: len 4; hex 80045437; asc   T7;;

RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C0 lock_mode X locks rec but not gap
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
1343 OS file reads, 6867 OS file writes, 2731 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.45 writes/s, 0.09 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 30 merges
merged operations:
 insert 30, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 4425293, node heap has 17 buffer(s)
29.09 hash searches/s, 3.91 non-hash searches/s
---
LOG
---
Log sequence number 682871332
Log flushed up to   682871332
Last checkpoint at  682869899
0 pending log writes, 0 pending chkp writes
2311 log i/o's done, 0.45 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 2197815296; in additional pool allocated 0
Dictionary memory allocated 714369
Buffer pool size   131071
Free buffers       129785
Database pages     1269
Old database pages 488
Modified db pages  14
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 2, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 1228, created 41, written 4623
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: 1269, 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
2 read views open inside InnoDB
Main thread process no. 1460, id 140692705335040, state: sleeping
Number of rows inserted 1366, updated 709, deleted 191, read 912575
0.09 inserts/s, 0.36 updates/s, 0.09 deletes/s, 33.72 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
 |
+--------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
+------------------+-------------+-----------+-----------+--------------------+------------+------------+-----------+----------+-----------+
| lock_id          | lock_trx_id | lock_mode | lock_type | lock_table         | lock_index | lock_space | lock_page | lock_rec | lock_data |
+------------------+-------------+-----------+-----------+--------------------+------------+------------+-----------+----------+-----------+
| 3086C6:783:20:20 | 3086C6      | X         | RECORD    | `person`.`heroes` | `PRIMARY`  |        783 |        20 |       20 | 283703    |
| 3086C0:783:20:20 | 3086C0      | X         | RECORD    | `person`.`heroes` | `PRIMARY`  |        783 |        20 |       20 | 283703    |
+------------------+-------------+-----------+-----------+--------------------+------------+------------+-----------+----------+-----------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX\G
*************************** 1. row ***************************
                trx_id: 3086C6
             trx_state: LOCK WAIT
           trx_started: 2013-06-27 15:28:46
     trx_requested_lock_id: 3086C6:783:20:20
      trx_wait_started: 2013-06-27 15:28:46
            trx_weight: 2
       trx_mysql_thread_id: 193
             trx_query: UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703
       trx_operation_state: starting index read
     trx_tables_in_use: 1
     trx_tables_locked: 1
      trx_lock_structs: 2
     trx_lock_memory_bytes: 376
       trx_rows_locked: 1
     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: 9999
*************************** 2. row ***************************
                trx_id: 3086C0
             trx_state: RUNNING
           trx_started: 2013-06-27 15:28:46
     trx_requested_lock_id: NULL
      trx_wait_started: NULL
            trx_weight: 4
       trx_mysql_thread_id: 190
             trx_query: NULL
       trx_operation_state: NULL
     trx_tables_in_use: 0
     trx_tables_locked: 0
      trx_lock_structs: 3
     trx_lock_memory_bytes: 1248
       trx_rows_locked: 2
     trx_rows_modified: 1
   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
2 rows in set (0.00 sec)

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
+-------------------+-------------------+-----------------+------------------+
| requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
+-------------------+-------------------+-----------------+------------------+
| 3086C6            | 3086C6:783:20:20  | 3086C0          | 3086C0:783:20:20 |
+-------------------+-------------------+-----------------+------------------+
1 row in set (0.00 sec)

mysql のバージョン: 5.5.31

誰でも光を当てることができますか?

4

1 に答える 1

4

別の MySQL がトランザクション内の同じレコードまたはそれに近いレコードにアクセスし、レコード ロックを開いたままにして、トランザクションがコミットまたはロールバックされるまで、他のスレッドがレコードを変更できないようにしています。

他の mysql スレッドを特定するには、すでに正しい軌道に乗っています。

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX\G

ブロックされた InnoDB トランザクション (上記の例では 30250F) と、他のトランザクション (302509) が表示されます。mysql プロセス/スレッド リストを確認します。

show processlist;

各スレッドにはスレッド ID があります。

mysql> show processlist;
+------------+---------+--------------------+------+-------------+---------+-----------------------------------------------------------------------+------------------+
| Id         | User    | Host               | db   | Command     | Time    | State                                                                 | Info             |
+------------+---------+--------------------+------+-------------+---------+-----------------------------------------------------------------------+------------------+
|        112 | myuser  | localhost          | mydb | Sleep       |      95 |                                                                       | NULL             |

また、ブロックしているトランザクションのスレッド ID (112) がリストに表示さtrx_mysql_thread_idINNODB_TRXます。

コマンドでスレッド 112 を終了する場合kill 112;、そのトランザクションはロールバックされ (現在のトランザクションで行われた変更は元に戻されます)、ロックが解放され、ブロックされたスレッドが続行できるようになります。

あなたがコメントで言ったように、そのスレッドはおそらく現在スリープ状態です:以前に行を変更した (trx_rows_modified: 1) ため、2 つの行がロックされました (trx_rows_locked: 2)。今すぐ変更します。まだトランザクションをコミットしていないため、ロックがまだ有効です。

実際の修正に関しては、アプリケーションはCOMMIT;すべてのトランザクションの後に実行する必要があります。トランザクションを使用しておらず、今すぐ学習したくない場合はSET autocommit=1;、データベースに接続した後、操作を実行する前に、自動コミットを有効にすることをお勧めします。MySQL では自動コミットがデフォルトで有効になっているため、コードで現在無効にしている可能性もあります。これは、接続ごとに個別に行う必要があることに注意してください。

自動コミットが有効になってCOMMITいる場合、単純なステートメントを発行するだけであれば必要ありませんが、BEGINまたはSTART TRANSACTIONコマンドを発行してトランザクションを明示的に開始することはできCOMMITますROLLBACKBEGINまたはステートメントのコードを確認START TRANSACTIONし、コミットを行っていない場合は削除してください。

自動コミットはグローバル オプションとして調整することもできます (自動コミットをグローバルに false に設定する方法) が、アプリケーションはその設定を持つデータベース サーバー構成に依存します。

トランザクションを使用する場合、操作の最後に必ず次のいずれかを実行する必要があります。

  1. 専念
  2. ロールバック
  3. 接続を閉じます (コミットされていない場合はロールバックが発生します)

Web サービスで永続的な接続を使用すると、予期せず終了する HTTP クライアント接続により、コミットまたはロールバックなしでスクリプトの実行が終了し、トランザクションの途中で接続が開いたままになる可能性があります。

関連する質問、トランザクションの基本的な紹介: データベース トランザクションとは何ですか?

http://dev.mysql.com/doc/refman/5.6/en/commit.html

于 2013-06-27T07:17:00.387 に答える