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

Detailed explanation of nginx upstream configuration and function

Configuration Example upstream backend { server b...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

nginx+tomcat example of accessing the project through the domain name

I was curious about how to access the project usi...

How to implement Docker volume mounting

The creation of the simplest hello world output i...

How to achieve the maximum number of connections in mysql

Table of contents What is the reason for the sudd...

HTML background color gradient achieved through CSS

Effect screenshots: Implementation code: Copy code...

MySQL Basics Quick Start Knowledge Summary (with Mind Map)

Table of contents Preface 1. Basic knowledge of d...

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

mysql backup script and keep it for 7 days

Script requirements: Back up the MySQL database e...

How to understand semantic HTML structure

I believe everyone knows HTML and CSS, knows the ...

Detailed explanation of writing multiple conditions of CSS: not

The :not pseudo-class selector can filter element...