How can we achieve better statistical functions when the data volume is large?

Source: Internet
Author: User
How do I count the total number of times when the data volume is large? For example, a table contains 5 million data records, and the traditional method is count (), which is very inefficient. How do I count the total number of times when the data volume is large?

For example, a table contains 5 million data records, and the traditional method is count (), which is very inefficient.

Someone on the Internet said that mysql triggers were used to insert the statistical results into a result table.

Are there other methods? For example, can I insert an asynchronous execution method into a mysql result table?

Or can I put the statistical results into redis?

We also use scheduled execution, but we need to calculate the sum in real time in this scenario.

How do I count the total number of times when the data volume is large?

For example, a table contains 5 million data records, and the traditional method is count (), which is very inefficient.

1. someone on the Internet said that mysql triggers were used to insert the statistical results into a result table.

Are there other methods? For example, can I insert an asynchronous execution method into a mysql result table?

2. can I add the statistical results to redis?

We also use scheduled execution, but we need to calculate the sum in real time in this scenario.

3. you can create a new table to save the number of rows.
Table name (primary key) | data volume

If data is inserted during the trigger, the data volume of the corresponding table is + 1. if data is deleted, the value is reduced by 1.

4. Another solution is to calculate the increment of 1 minute at a time (based on your business's real-time requirements). The increment of the previous statistical value plus the increment of 1 minute is the current total number. Of course, this depends on whether the old data is deleted. This method is coupled with inserting statistical results into the table.

I tend to use redis for storage, insert + 1, delete-1. Db has no pressure and reduces coupling.

Use memcache or redis to count.
Check memcache first when you need to check the count. if you don't have it, go to the database count, and then set memcache. The next time you check memcache, you don't need to count.

The preceding figure shows how to achieve better statistical functions when the data volume is large? For more information, see The PHP Chinese website (www.php1.cn )!

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.