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

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

Example code for evenly distributing elements using css3 flex layout

This article mainly introduces how to evenly dist...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Implementation of Docker packaging image and configuration modification

I have encountered many problems in learning Dock...

js realizes the magnifying glass effect of shopping website products

This article shares the specific code of js to ac...

CentOS 7 method to modify the gateway and configure the IP example

When installing the centos7 version, choose to co...

Solution to the conflict between Linux kernel and SVN versions

Phenomenon The system could compile the Linux sys...

Zabbix monitors mysql instance method

1. Monitoring planning Before creating a monitori...

Tips on disabling IE8 and IE9's compatibility view mode using HTML

Starting from IE 8, IE added a compatibility mode,...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

Vue3 draggable left and right panel split component implementation

Table of contents Breaking down components Left P...

Detailed steps to change the default password when installing MySQL in Ubuntu

Step 1: Enter the directory: cd /etc/mysql, view ...