MySQL Query Optimization Using scheduling and locking (1)

Source: Internet
Author: User
Tags mysql query optimization

In daily work, DBAs often encounter how to make separate queries faster. MySQL also allows you to change the statement scheduling priority so that queries from multiple clients can better collaborate, so that a single client will not wait for a long time due to locking. Changing the priority also ensures that queries of specific types are processed faster. This section describes the default Scheduling Policies of MySQL and the options 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, we call the client that executes the search (SELECT) as the "Reader" and perform the modification operations (DELETE, INSERT, REPLACE, or UPDATE) the client is called "Writer ".

The default scheduling policy available for MySQL is 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 the table operation, the lock is released. It is feasible to explicitly obtain or release the LOCK using the lock tables and unlock tables statements, but in general, the server's LOCK manager will automatically obtain the LOCK as needed, release the lock when it is no longer needed. The type of the obtained lock depends on whether the client writes or reads the lock.

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 when the data records are deleted, added, or modified, indexes on a data table may also 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. Obviously, it is unfavorable to allow two clients to write data to a table at the same time, because such an operation will quickly make the information in the 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 reading location may be changing (modifying), and the read result may not be real.

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 the execution scheduling of DELETE, INSERT, load data, REPLACE, and UPDATE statements. Generally, when a data table is being read, if a write operation arrives, the writer waits until the reader completes the operation (the query cannot be interrupted after the query starts, therefore, the reader is allowed to complete the operation ). If the writer is waiting and another read operation reaches, the read operation will be blocked because the default scheduling policy is that the writer 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 second reader arrives while the writer is waiting, 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, this scheduling modification implies that the write operation of LOW_PRIORITY may be blocked forever. If other read operations have been completed during the previous read operation, new requests will be inserted before the LOW_PRIORITY 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. 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.


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.