About row locks and table locks in Mysql

The default engine of mysql is innodb. Regarding the row lock table lock, it has been found as an example, select...for update (note: this is a statement to add a row lock, only the innodb engine can be used)

Next, we need to analyze a question: when to use row locks and when to use table locks, there are the following situations:

1. What kind of lock is added to the primary key index and the non-primary key index when there is an index

2. What kind of lock is added in the case of no index

Precautions:

This operation needs to be performed in two command windows, because the lock is placed in the begin/commit transaction, before committing

MySQL is submitted by default, you must cancel it first: set @@autocommit=0

 

1. What kind of lock is added to the primary key index and the non-primary key index when there is an index

Primary key index:

          

              1-1                                                1-2

 

In the case of the primary key index, the row lock is added to the id of 1, and the other rows are not locked, which belong to the row lock.

 

Non-primary key index:

        

              2-1                                          2-2

 

             2-3

2-1 and 2-2 query peers and find that they are locked. 2-3 queries other rows and finds that they can query

Therefore, a row lock is added in the case of a non-primary key index

 

2. In the case of no index

For primary key ID locking:

    

              3-1                                            3-2

It is found that the primary key ID is also a lock row

 

For non-primary key name lock:

  

              3-3                                            3-4

It is found that the non-primary key name is a lock table

 

Summarize:

Whether the primary key has an index or not, the lock is an up lock;

If the non-primary key has an index, it is a row lock, and if there is no index, it is a table lock.

InnoDB row locks are implemented by locking the index items on the index. InnoDB uses row-level locks only when data is retrieved through index conditions. Otherwise, InnoDB uses table locks.

 

The explanation that the primary key does not have an index set is also an uplink lock:

The primary key must be the unique index, and the unique index is not necessarily the primary key. 

 The so-called primary key is an attribute or attribute group that can uniquely identify a row in a table. A table can only have one primary key, but can have multiple candidate indexes. Because the primary key can uniquely identify a row of records, it can ensure that there will be no mistakes when performing data update and deletion. In addition to the above functions, the primary key often forms a referential integrity constraint with the foreign key to prevent data inconsistency. When designing a database, the primary key plays an important role. 

1. The primary key can ensure the uniqueness of the record and the non-empty primary key field. The database management system automatically generates a unique index for the primary key, so the primary key is also a special index. 

2. A table can have multiple unique indexes, but only one primary key. 

3. Primary key columns do not allow null values, while unique index columns allow null values. 

4. Indexes can improve the speed of queries. 

In fact, both the primary key and the index are keys, but the primary key is a logical key, and the index is a physical key, which means that the primary key does not actually exist, and the index actually exists in the database. Records and indexes can generally not be built, but if you need to query the table, it is best to build it, which can speed up the retrieval speed. 

 

Reference link:

https://blog.csdn.net/claram/article/details/54023216

http://www.jb51.net/article/50047.htm

Guess you like

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