Does MySQL select lock the table?

Source: Internet
Author: User
Tags mysql query


Does MySQL select lock the table?

Some people say that MySQL's Select will lock the table, some people say that the MySQL query will not lock the table.
They are all right, no, but very one-sided.

In fact, whether the MySQL select will lock the table, this depends entirely on what the table is using the storage engine.


Here I take the most familiar storage engine InnoDB and MyISAM to illustrate this problem.

Table Select for MyISAM locks the table, causing other operations to hang and wait.
Table Select for InnoDB is not locked. In fact, the snapshot is used here. Snapshots are not discussed here.


Here is my corroboration:


Answer one:
Select Sql_no_cache * from tmp002
Table tmp002 has 5618288 data storage engine when InnoDB, all query out approximately about 5min

Answer two:
Update tmp002
Set num=6
where Mreasonid in (' 700098 ', ' 301001 ') limit 10000

We first execute the following statement. Re-execute two.


We're going to find out. 2 was executed soon.
Update tmp002
Set num=6
where Mreasonid in (' 700098 ', ' 301001 ') limit 10000

Rows affected: 0
Time: 0.120s

In reply to three kinds of execution under show full processlist.
No locking phenomenon was found.


To modify the table storage Engine:
ALTER TABLE tmp002 Engine=myisam
Rows affected: 5618288
Time: 17.510s


The same is the example above


Answer one:
Select Sql_no_cache * from tmp002
Table tmp002 has 5618288 data storage engine when InnoDB, all query out approximately about 5min

Answer two:
Update tmp002
Set num=6
where Mreasonid in (' 700098 ', ' 301001 ') limit 10000


We first execute the following statement. Re-execute two.

We'll get back to you. Always in the execution state without returning any information.

In reply to three kinds of execution under show full processlist.
13564441 root 192.168.53.41:53312 tempDB Query 2 Locked update tmp002 set num=6 where Mreasonid in (' 700098 ', ' 301001 ') l Imit 10000
13564671 Root 192.168.53.41:53736 TempDB Query 4 Writing to net Select Sql_no_cache * from tmp002

The 13564441 status is Locked, waiting for the release of the lock.


In summary, the following results can be obtained.
Table Select for MyISAM locks the table, causing other operations to hang and wait.
Table Select for InnoDB is not locked. In fact, the snapshot is used here. Snapshots are not discussed here.

This article is from the "SQL Server MySQL" blog, so be sure to keep this source http://dwchaoyue.blog.51cto.com/2826417/1411235

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.