How does mysql increase the statistical query speed of a table?

Source: Internet
Author: User
Tags mysql tutorial

For large tables in the database, statistical queries are usually slow, and you need to consider whether the query will affect online applications, in this case, we can use an intermediate table to increase the query statistics speed. Next we will count counttable to count the daily consumption records of customers, as shown below.

This tutorial will tell you aboutHow to improve the statistical query speed of a table in the mysql tutorialOh, here we are talking about using an intermediate table to speed up data Table query and statistics.

For tables with large database tutorialsStatistics QueryThe query speed is usually slow, and it also needs to be considered whether the query will affect online applications. In this case, we can use an intermediate table to increase the query statistics speed, next we will count counttable to count the daily consumption records of customers, as shown below.

Create tabel counttable (id, varchar (10), custi decimal (16, 2), coutdate date, custip varchar (20 )}

Because a large number of customer records are generated every day, the amount of data in this table will be large. Now the business department has a requirement to count the total amount of money consumed. Let's look at the example below.

 

Mysq> select sum (custi) from counttable where custdate> adddate (now (),-7 );

Sum (custi)

161666666666

1row int set (3.95 sec)

Method 2: Use an intermediate table to process tmp_cout

Create tabel tmp_count (cid, varchar (10), custcount (decimal (16, 2), custdate date, custip varchar (20 )};

Now, transfer the data to the intermediate table for statistics and generate the results.

Mysq> insert into tmp_count select * from counttalbe wher custdate <adddate (now,-7 );

Query OK 15777777 rows affected (6.67 sec)

Records 15777777 duplicates: 0 warings 0;

Now let's count the customer's consumption amount this week.

Mysql> select sum (cust_count) from tem_count;

Sum (cust_count)

1696666666.22

1row in set (0.73 sec)

Well, from the two methods above, there are many differences in the time spent,

Use mysql of the intermediate tableData Statistics, Will not have a negative impact on the Application

An intermediate table can flexibly add indexes or add temporary new fields to improve the effective statistical query method.

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.