MySQL read and write operation Priority

Source: Internet
Author: User
Tags mysql query mysql query optimization

Update [low_priority] tbl_name set col_name1 = expr1, col_name2 = expr2 ,...
[Where where_definition] [limit #]

 

Scheduling and locking of MySQL query optimization Series Lectures

 

The previous section focuses on how to make separate queries faster. MySQL also allows you to change the statement scheduling priority, which enables better coordination of queries from multiple clients.
Single customer
The user end will not wait for a long time because of the lock. Changing the priority also ensures that queries of specific types are processed faster. This section describes the default Scheduling Policies of MySQL and the policies that can be used to influence these policies.
. It also talked about the use of concurrent insert operations and the impact of the storage engine lock level on the concurrency of the client. For the convenience of the discussion, the client that executes the search (select) is called "read
", The client that executes the modification operation (delete, insert, replace or update) is called" Writer ".

The default Scheduling Policies of MySQL can be summarized as follows:

· Write operations take precedence over read operations.

· Write operations on a data table can only occur once at a time point, and write requests are processed in the order they arrive.

· Multiple read operations on a data table can be performed simultaneously.

The MyISAM and memory storage engines use data table locks to implement such scheduling policies. When the client accesses a table, it must first obtain its lock. When the client completes table operations
The lock will be removed. Use lock tables and unlock
Tables statements can be used to explicitly obtain or release locks. However, the server's lock manager automatically acquires locks as needed, release the lock when it is no longer needed. Lock class obtained
Type depends on whether the client writes or reads data.

The client that writes data to a table must have exclusive (exclusive) Access lock. During the operation, the data table is in an inconsistent (inconsistent) state,
Because number
When a data record is deleted, added, or modified, the indexes on the data table may need to be updated to match each other. If other clients are allowed to access this data table during the change process, problems may occur. Very bright
Explicitly, it is not good to allow two clients to write a data table at the same time, because such an operation will quickly make the information in the data table a pile of useless garbage. However, it is not good to allow the client to read the changed data table,
Because the data in the position being read may be changing (modifying), The read result may not be true.

The client that reads a table must obtain a lock to prevent other clients from writing or changing the table during the reading process. However, this lock does not require exclusive access. The read operation does not change the data, so there is no reason for a reader to prevent other readers from accessing the table. Therefore, the read lock allows other clients to read the table at the same time.

MySQL provides several statement modifiers that allow you to modify its scheduling policy:

· The low_priority keyword is applied to delete, insert, load data, replace, and update.

· The high_priority keyword is applied to select and insert statements.

· The delayed keyword is applied to insert and replace statements.

Low_priority and high_priority regulators affect storage engines that use data table locks (such as MyISAM and memory ). The delayed modifier acts on the MyISAM and memory data tables.

Change statement scheduling priority

The low_priority keyword affects Delete, insert, and load.
Execute and Schedule Data, replace, and update statements. Generally, when a data table is being read, if a write operation arrives, the writer waits for the reader to complete the operation.

(The operation cannot be interrupted after the query starts, so the reader is allowed to complete the operation ). If the writer is waiting and another read operation arrives, the read operation will also be blocked because
The specified scheduling policy takes precedence over the reader. When the first reader completes the operation, the writer starts the operation and starts the operation until the writer completes the operation.

If the write operation is a low_priority (low priority) Request, the system will not consider it as having a higher priority than the read operation. In this case, if the writer is waiting
Hou, second
The second reader is allowed to be inserted before the writer. The writer is allowed to start operations only when there are no other readers. Theoretically, such scheduling modification implies that
The write operation of low_priority is always blocked. If other read operations have been completed during the previous read operation, new requests will be inserted
Low_priority before the write operation.

The high_priority (high priority) keyword of the SELECT query is similar. It allows select to insert a pending write operation, even if the write operation has a higher priority under normal circumstances. Another effect is that a high-priority SELECT statement is executed before a normal SELECT statement, because these statements are blocked by the write operation.

If you want all statements that support the low_priority option to be processed by default at a low priority, use the -- low-priority-Updates option.
To start the server. By using insert
High_priority is used to increase the insert statement to a normal write priority, which can eliminate the impact of this option on a single insert statement.

From: http://blog.sina.com.cn/s/blog_5677bc540100dtpf.html

 

My summary is as follows: [to be verified]

During MySQL write operations, read operations must wait. The write operation locks the table.

MySQL read operations can be performed at the same time, that is, concurrent read, but the write operation is waiting.

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.