. NET to java.05. Why doesn't MySQL have nolock

Source: Internet
Author: User

Today, a problem suddenly came to mind, originally in order to improve SQL Server performance, the Company rules query statements are generally added with (NOLOCK)

Now turn Java, use MySQL why not mention this thing?

First in MySQL wrote a query statement, than the appearance of adding nolock, suggesting that the syntax is not correct, is it used readuncommitted? Still hints that the syntax is not correct,

It seems that MySQL does not support the syntax of NOLOCK.

Then the question becomes, Why does MySQL need not support syntax such as NOLOCK, or if MySQL does not support NOLOCK, modify the record to cause the lock table?

So I did the following experiment.

Two MySQL connection was opened, (by the way, because the client is SQLyog, I thought the same as SQL Server Management Studio every "Ask" is a connection, actually not, each connection to "create a new Connection", I have been testing myself for a long while to find this problem)

The first MySQL connection executes a query

TRANSACTION ; UPDATE SET NAME='newvalue'WHERE id=1

Because the transaction is not committed, if it is SQL Server by default, the second connection queries the same record, which is bound to be blocked. If the SQL Server query adds a nolock read to the dirty value "newvalue" that is not yet commit

Second MySQL connection I execute query

SELECT *  from ' testtable '

I found that neither the blocking nor the dirty reads occurred , the old values were queried, and the new values were not read to the uncommitted newvalue

This means that MySQL and SQL Server have different mechanisms for maintaining transactions by default.

SQL Server by default, a transaction modifies a value, before the transaction commits, blocking other connections to read the value of the change, if the NOLOCK read is modified to commit the value (that is, dirty read, because the value may eventually be rolled back)

MySQL by default, a transaction modifies a value that, before the transaction commits, does not block the other connection to read the value in the modification, and reads the value before the modification.

For internet companies, most scenarios do not want to write a transaction to block the read,

So SQL Server recommends adding NOLOCK

MySQL itself does not block, nolock also meaningless.

. NET to java.05. Why doesn't MySQL have nolock

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.