[MySQL] About split Query

Source: Internet
Author: User
For large queries, you sometimes need to divide them into small queries: each query has the same function, but only a small part of the original is completed, only a small number of result sets are returned for each query.


Deleting old data is a good example. When regular cleaning of old data, if an SQL statement involves a large amount of data, it may lock Multiple tables or rows, consuming a lot of system resources, but it blocks many other small but important queries. When dividing a large Delete statement into smaller queries, You can minimize the impact on msql performance and reduce the latency caused by MySQL replication.
For example, a statement is run once a month to clear data three months ago:
Mysql> Delete from messages where dt <date_sub (now (), interval 3 month );
You can use the following methods to complete such a task:
Rows_affected = 0 Do { Rows_affected = do_query ("delete from messages where dt <date_sub (now (), interval 3 month) limit 10000 ") } While rows_affected> 0
Deleting 10 thousand rows of data at a time is more efficient and has little impact on the server. At the same time, if the data is paused for a while each time the server is deleted, the original one-time pressure on the server can be distributed to a longer period of time, thus reducing the time for locking the table lock row during deletion.
(Refer to "High Performance MySQL")

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.