English 中文(简体)
MySQL选择更新行为
原标题:MySQL select for update behaviour

根据MySql文件,MySql支持多采样。

case-1

开放式终端-1:

页: 1

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select id, status from tracking_number limit 5 for update;
+----+--------+
| id | status |
+----+--------+
|  1 |      0 |
|  2 |      0 |
|  3 |      0 |
|  4 |      0 |
|  5 |      0 |
+----+--------+
5 rows in set (0.00 sec)
mysql> 

离开机场,开放终端-2:

页: 1

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select id, status from tracking_number limit 5 for update;

<!-- Hangs here. and after some time it says-->
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

尽管有大量的浏览器可以检索,但T2等到t1完成。

case-2

Left terminal-1 as is.Now in terminal-2:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

<!-- case 2.1 -->
mysql> select id, status from tracking_number where id=1;
+----+--------+
| id | status |
+----+--------+
|  1 |      0 |
+----+--------+
1 row in set (0.00 sec)

mysql> select id, status from tracking_number where id=2;
+----+--------+
| id | status |
+----+--------+
|  2 |      0 |
+----+--------+
1 row in set (0.00 sec)

<!-- case 2.2 -->
mysql> select * from tracking_number where id=2 for update;
<!-- Hangs here. and after some time -->
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
  1. 但是,为什么在第1号案件中,T2等待的是T1所锁的那套牢房?

  2. 这是否意味着未经约束的选点(即使有气候参数)。 我也尝试过不同的范围,把整个桌子 blocks开?

  3. Is there any way to let transactions to lock independently without specifying the field of the record(i.e., without using where field=value)?
  4. Generally (or as per Java concurrent locking), write lock is exclusive and read is not. In case 2.1, though the records are in write lock mode, how T2 can read the same records? Since this is allowed what is the point in locking it?
  5. Case 2.2 is understood.

1. 开放终端和交易:

mysql> update tracking_number set status=4 where status=0 limit 5;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

离开那里并开启另一个终点站和交易:

mysql> update tracking_number set status=5 where status=0 limit 5; 

T2在承诺(或退约)T1之前没有成功。

  1. Why is this behavior?
最佳回答

请允许我谈谈你的案例,并解释这些障碍是如何运作的:

<><>1>

T1希望更新你测试表中的几行。 这项交易在头5个行中排第9锁,X锁。

T2 wants to update some rows in your test table. This transaction puts IX (because IX compatible with IX) lock on all table and tries to first 5 rows but it can t do it because X is not compatible with X

因此,我们是完美的。

T1希望更新你测试表中的几行。 这项交易在头5个行中排在9个桌旁,X锁。

T2 wants to select some rows from your test table. And it does not place any locks (because InnoDB provides non-locking reads)

T1希望更新你测试表中的几行。 这项交易在头5个行中排在9个桌旁,X锁。

T2希望更新(选择更新)你测试表的几行。 将IS放在整个桌旁,并试图将S锁放在囚室上,因为X和S是无法兼容的。


Also always be aware of isolation level: different level cause different mechanism to free/acquire locks

希望会有助于

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签