3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQL 行ロック、テーブルロックの確認

Posted at

MySQL 行ロック、テーブルロックの動きを確認

#行ロック

メインSQL
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;
UPDATE table_name SET field_name = field_name + 1 WHERE id = 1;
SELECT SLEEP(10);
COMMIT;

↓↓↓ 「メインSQL」実行中は、IDが同じで更新処理なので、アクセスNG

SQL
UPDATE table_name SET field_name = field_name + 1 WHERE id = 1;

↓↓↓ 「メインSQL」実行中は、IDが同じであるが、読み込み処理なので、アクセスOK

SQL
SELECT count_share from table_name WHERE id = 1;

「REPEATABLE READ」なので、処理完了後にカウント数が更新される。

↓↓↓ 「メインSQL」実行中は、IDが違うので、アクセスOK

SQL
UPDATE table_name SET field_name = field_name + 1 WHERE id = 2;
SELECT field_name from table_name WHERE id = 2;

テストの為に、GLOBALにしているが、実使用の際は「SESSION」で!

SQL
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

#テーブルロック

メインSQL
LOCK TABLES fdn_content_count WRITE;
UPDATE table_name SET field_name = field_name + 1 WHERE id = 1;
SELECT SLEEP(10);
UNLOCK TABLES;

↓↓↓ 「メインSQL」実行中は、IDが同じなので、アクセスNG

SQL
UPDATE table_name SET field_name = field_name - 1 WHERE id = 1;

↓↓↓ 「メインSQL」実行中は、IDが違うけど、アクセスNG

SQL
UPDATE table_name SET field_name = field_name - 1 WHERE id = 2;
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?