How MySQL improves the statistical query speed of tables

Source: Internet
Author: User

This tutorial is going to tell you about the MySQL tutorial how to improve the table's statistical query speed Oh, we talked about using the middle table to improve the query and statistical speed of the datasheet.

For tables with a large database tutorial, it is usually slower to make a statistical query , and also to consider whether the query will affect the online application, usually in which case we use the intermediate table to increase the query statistics speed. Below we will count the counttable to count the customer daily expense record, as follows.

Create Tabel counttable (Id,varchar), Custi decimal (16,2), coutdate date,custip-varchar ()}

As a result of a large number of customer records every day, so the data volume of this table will be very large, now the business unit has a request, want to count the total money, we look at the example below.

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

SUM (custi)

161666666666

1row int Set (3.95 sec)

Method Two, use the intermediate table to handle the Tmp_cout

Create Tabel Tmp_count (Cid,varchar), Custcount (Decimal (16,2), custdate date,custip varchar (20)};

Now to transfer the data to be counted in the middle table for statistical and statistical results

Mysq>insert into the 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 amount of customer spending this week.

Mysql>select sum (cust_count) from Tem_count;

SUM (cust_count)

1696666666.22

1row in Set (0.73 sec)

Well, from the top two ways, it takes a lot of difference to spend time,

Using partial MySQL data statistics in the middle table will not adversely affect the application

The intermediate table can increase the index flexibly, or add temporary new fields, so as to improve the effective method of statistical query.

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.