Ask a question about the lock of MySQL? What happens when MySQL writes data at the same time?

Source: Internet
Author: User
A simple normal (no lock) SQL, regardless of the transaction situation

1: If a query operation and a write operation are performed at the same time, will MySQL automatically lock?

2: What if the operation of two or more queries is performed concurrently?

3: What if two write operations are performed simultaneously?

3: What if one reads and one writes at the same time?

does not consider the case of a transaction.

Reply content:

A simple normal (no lock) SQL, regardless of the transaction situation

1: If a query operation and a write operation are performed at the same time, will MySQL automatically lock?

2: What if the operation of two or more queries is performed concurrently?

3: What if two write operations are performed simultaneously?

3: What if one reads and one writes at the same time?

does not consider the case of a transaction.

Give yourself a supplementary answer:
1:innodb the different select does not have any lock, MyISAM will automatically add the table to share the read lock;

2:innodb There is no lock, so there is no problem, MyISAM is a table shared read lock, shared read locks are not conflicting with each other, is parallel;

3:myisam is the table exclusive write lock, table-level lock, is serial, write lock will block each other, must write the SQL is a piece of execution. InnoDB the situation is a bit more complicated, InnoDB uses the row lock, of course, with respect to the index (impact plus lock), will automatically add row lock:

Exclusive Lock (X): A transaction that allows an exclusive lock to update data, preventing other transactions from acquiring shared read and exclusive write locks of the same data set.

Therefore, if there is no row lock conflict, there is no effect on each other, or there will be lock blocking.

, lock granularity may also appear deadlock, that is: waiting for the lock will be a deadlock, when a thing found it to wait for the release of the lock transaction while also waiting for it to release the lock, it will occur deadlock exit, release the lock, let the former get lock;

4: For the ordinary SELECT statement, InnoDB will not add any locks, so read and write at the same time no problem, read as snapshot read, write as the current read, for MyISAM to see which process first to obtain a table lock, read and write is serial.

Reference: http://blog.csdn.net/xifeijian/article/details/20313977
Reference: http://blog.csdn.net/xifeijian/article/details/20312557

Auto Lock, Update is atomic operation

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.