Usage of mysql (for update)

This article is well written. For the primary key, as long as this row is an index row, the effect of row-level locking can be achieved. For projects with thousands of simultaneous online users, for update can be used, and large projects need to be optimized separately, and This method is also not available for spikes 


===================== MySQL InnoDB table lock and row lock ========================= = */

Since InnoDB defaults to Row-Level Lock, MySQL will only execute Row Lock (lock only the selected data example) only if the primary key is "explicitly" specified, otherwise MySQL will execute Table Lock (lock the entire data table) live).

For example: Suppose there is a form products, which has two fields id and name, id is the primary key.

Example 1: (Explicitly specify the primary key, and have this data, row lock)

code show as below:
SELECT * FROM products WHERE id='3' FOR UPDATE;
SELECT * FROM products WHERE id='3' and type=1 FOR UPDATE;

Example 2: (Specify the primary key explicitly, if there is no such data, there is no lock)

code show as below:
SELECT * FROM products WHERE id='-1' FOR UPDATE;

Example 3: (no primary key, table lock)

code show as below:
SELECT * FROM products WHERE name='Mouse' FOR UPDATE;

Example 4: (Primary key is ambiguous, table lock)

code show as below:
SELECT * FROM products WHERE id<>'3' FOR UPDATE;

Example 5: (Primary key is ambiguous, table lock)

code show as below:
SELECT * FROM products WHERE id LIKE '3' FOR UPDATE;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792056&siteId=291194637