当前位置:网站首页>Technology sharing | a clustering incremental statistical SQL requirement in MySQL
Technology sharing | a clustering incremental statistical SQL requirement in MySQL
2022-06-21 13:48:00 【Aikesheng open source community】
author : Liu Chen
Net name bisal , More than 10 years of experience in application operation and maintenance , At present, he is mainly engaged in the work related to the improvement of database application R & D capability and technical management ,Oracle ACE , Tencent cloud TVP, Have Oracle OCM & OCP 、EXIN DevOps Master 、SCJP And other international certification , The first batch in China Oracle YEP member ,OCMU member ,《DevOps Best practices 》 One of the Chinese translators ,CSDN & ITPub Expert bloggers , official account "bisal My personal grocery store ", Sharing technical articles for a long time , Share technical topics online and offline many times .
In this paper, the source : Original contribution
* Produced by aikesheng open source community , Original content is not allowed to be used without authorization , For reprint, please contact the editor and indicate the source .
My colleague raised a question MySQL In the database SQL The problem of statistics , I use test data to simulate , As shown below , surface tt There are three fields ,code Is the identification name ,cdate Is the corresponding date ,ctotal It's a statistic
The original statistical statement is as follows , according to code and cdate Clustering , Count out each code The number of records accumulated each month ,
But his need is to be able to follow code+cdate The statistical value of is displayed accumulatively , For example, the first line in the above figure total yes 10 , Second elements total Show 10+9=19 , In the third line total Show 10+9+11=30 , And so on .
What he needs is total Line by line ,MySQL Can be achieved by defining variable accumulation , As shown below , utilize @i Line by line ,
It really looks like it has come true , But there's a problem , Different code , No zeroing is done , For example, in the figure above ,code=BBB The first line of the record ,total Should be 5 , Current 35 Is obviously through 30+5 Got .
There should be another logic , Namely, in accordance with the code While clustering sum Sum calculation , As shown below , Now you can see ,g_total according to code and cdate Add up , Different code , It will return to zero , Recalculate , Meet the actual needs ,
In fact, this one can be optimized ,MySQL 8.0 Support with , Above SQL Chinese vs tt The table was read twice , With the help of with , It can be reduced to read only once tt surface ,
in addition , Combined with the actual retrieval scenario requirements , Consider adding indexes to related fields , Further improve the efficiency of data retrieval .
Of course , The above SQL There may be other alternatives or better solutions , This article only gives one of the solutions .
therefore , adopt SQL Achieve business needs , On the one hand, we need to fully understand the meaning of requirements , Can accurately map to specific SQL logically , On the other hand, you should know the functions supported by the database 、 function 、 Characteristics, etc. , Whether there is an implementation that can meet this requirement , besides , Non functional factors , It can't be ignored , The right index 、 Avoid reading data repeatedly 、 Avoiding unnecessary sorting and so on are the means we can use .
边栏推荐
- Write efficient defect reports
- 3. function improvement
- [test process and theory - software development process and project management]
- Collection reference type in JS
- Kubernetes快速實戰與核心原理剖析
- TOOD: Task-aligned One-stage Object Detection
- Alibaba cloud link tracking is on the Net project (Jaeger trace)
- 4. procedure flow structure
- 如何使用搜索引擎?
- Kube-prometheus grafana安装插件和grafana-image-renderer
猜你喜欢

Setting of Seaborn drawing style

SCCM creates a client collection based on the installed app and periodically pushes application updates

Set up ZABBIX monitoring and email alarm

MySQL - table constraints

3D slicer import label and view label

服务治理的工作内容

MySQL - transaction management

Lamp Architecture 3 -- compilation and use of PHP source code

Declare war on uncivilized code I

Babbitt yuancosmos daily must read: wechat may ban a official account for the first time on the grounds of "involving secondary transactions in digital collections", and the new regulations of the pla
随机推荐
C language elementary (VII) structure
MySQL - built in functions
Lamp architecture 4 -- MySQL source code compilation and use
MySQL - index
Collection reference type in JS
7. pointer
What is software testing?
Summary of the latest remote deployment O & M tools
Hands on data analysis unit 2 section 4 data visualization
hands-on-data-analysis 第二单元 第四节数据可视化
Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection
如何使用搜索引擎?
MySQL - transaction management
Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
3. function improvement
8 most common SQL error usages
Chapter IX Cisco ASA application nat
服务治理的工作内容
Kubernets Rapid Practical fighting and Core Principle Analysis
Customize view to draw line chart (support zoom)