Mysql Segmentation Query Usage Analysis _mysql

Source: Internet
Author: User

This article illustrates the usage of MySQL segmentation query. Share to everyone for your reference, specific as follows:

For large queries sometimes need to ' divide and conquer ', the large query is divided into small queries: Each query function exactly the same, but only to complete a small part of the original, each query returns only a small number of result sets.

Deleting old data is a good example. When you regularly clean up old data, a SQL that involves a large amount of data may lock multiple tables or rows at once, consuming a lot of system resources, and blocking many other small but important queries. When you cut a large DELETE statement into smaller queries, you minimize the performance that affects mSQL, while reducing the latency caused by MySQL replication.

For example, a statement that runs once a month to clean up data three months ago:

Copy Code code as follows:
Mysql> DELETE from Messages WHERE dt<date_sub (now (), INTERVAL 3 MONTH);

You can accomplish such a task in the following ways:

rows_affected = 0 do
{
   rows_affected = do_query (' DELETE from Messages WHERE dt<date_sub (now (), INTERVAL 3 MONTH )}while rows_affected>0 LIMIT 10000 ")


Deleting 10,000 rows at a time is a more efficient and less server-impacting practice. At the same time, if you pause for a moment each time you delete data, you can spread the original one-time pressure of the server into a longer period of time, reducing the time it will take to lock the table lock line when you delete it.

For more information about MySQL interested readers can view the site topics: "MySQL Transaction operation skills Summary", "MySQL stored process skills encyclopedia", "MySQL database lock related skills summary" and "MySQL common function large summary"

I hope this article will help you with the MySQL database meter.

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.