Analysis of the principle and usage of MySQL continuous aggregation

Analysis of the principle and usage of MySQL continuous aggregation

This article uses examples to illustrate the principle and usage of MySQL continuous aggregation. Share with you for your reference, the details are as follows:

Continuous aggregation is the operation of aggregating ordered data in time order.

In the following examples, the EmpOrders table is used to store the order quantity of each employee each month.

Run the following code to create the EmpOrders table and populate it with sample data.

CREATE TABLE EmpOrders (
empid INT NOT NULL,
ordermonth DATE NOT NULL,
qty INT NOT NULL,test
PRIMARY KEY (empid,ordermonth)
);

Query the order table and orderdetails table and insert the orders for each month into the EmpOrder table. The SQL statement is as follows (the trick is to group by month)

INSERT INTO EmpOrders
SELECT a.employeeid,orderdate AS Order date,SUM(quantity) AS qty
FROM orders a
INNER JOIN orderdetails b
ON a.orderid=b.orderid
GROUP BY employid,DATE_FORMAT(orderdate,'%Y-m');

The following is the PHP file that generates sample data

<?php
$sql = "INSERT INTO emporders SELECT %s,'%s-%02d-01',%s;".'<br />';
$insert_sql = '';
for($empid=1;$empid<=8;$empid++)
{
  for($year=2009;$year<=2015;$year++)
  {
    for($month=1;$month<=12;$month++)
    {
      $num = rand(20,800);
      $insert_sql .= sprintf($sql,$empid,$year,$month,$num);
    }
    $insert_sql .= '<br />';
  }
}
echo $insert_sql;

The following is part of the data of the employee order table EmpOrder

The following discusses three continuous aggregation issues based on the EmpOrders table: cumulative, sliding, and year-to-date.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • Detailed explanation of MySQL commonly used aggregate functions
  • How to add conditional expressions to aggregate functions in MySql
  • php+mysql open source XNA aggregation program released for download
  • Mysql cannot select non-aggregate columns
  • Detailed explanation of MySQL single table query operation examples [syntax, constraints, grouping, aggregation, filtering, sorting, etc.]
  • Analysis of MySQL query sorting and query aggregation function usage
  • MySQL uses aggregate functions to query a single table
  • MySQL grouping queries and aggregate functions
  • Optimizing the slow query of MySQL aggregate statistics data

<<:  jQuery implements HTML element hiding and display

>>:  How to use Celery and Docker to handle periodic tasks in Django

Recommend

FlashFXP ftp client software registration cracking method

The download address of FlashFXP is: https://www....

MariaDB under Linux starts with the root user (recommended)

Recently, due to the need to test security produc...

React.js framework Redux basic case detailed explanation

react.js framework Redux https://github.com/react...

Analysis of MySql index usage strategy

MySql Index Index advantages 1. You can ensure th...

MySQL 8.0.13 download and installation tutorial with pictures and text

MySQL is the most commonly used database. You mus...

Usage of Vue filters and timestamp conversion issues

Table of contents 1. Quickly recognize the concep...

CSS realizes div completely centered without setting height

Require The div under the body is vertically cent...

Singleton design pattern in JavaScript

Table of contents 1. What is a design pattern? 2....

Solution to the problem that the InnoDB engine is disabled when MySQL is started

Find the problem Today at work, when copying tabl...

How to switch directories efficiently in Linux

When it comes to switching directories under Linu...

React Router 5.1.0 uses useHistory to implement page jump navigation

Table of contents 1. Use the withRouter component...

Use of vuex namespace

Table of contents Since Vuex uses a single state ...

Understanding of web design layout

<br />A contradiction arises. In small works...