当前位置:网站首页>Can MySQL use aggregate functions without GROUP BY?
Can MySQL use aggregate functions without GROUP BY?
2022-08-05 09:32:00 【CSDN q&a】
I am a new data product, and I wrote a query today for a store that has not generated orders in the last 15 days.The SQL written by Data Development is 8 times faster than the results I wrote.
There is one place that I really don't understand, please ask for help humbly.
Written by me
SELECT s.id FROM `store`s JOIN ( SELECT `store_id` , MAX( `create_time`)t FROM `order` GROUP BY `store_id` )aON s.id = a.store_idWHERE s.channel = 2 ANDs.show = 1 AND DATEDIFF(now(), a.t ) > 15
Written by Data Development
SELECT idFROM `store`s where s.channel = 2 AND s.show = 1 AND DATEDIFF(now(), (SELECT MAX( `create_time`) FROM `order` o where o.store_id = s.id)) > 15
After reading the performance analysis of my SQL, it is because there are too many rows of order table data (about tens of millions), and the GROPU BY grouping is used to cause performance.
The development and writing avoided the use of GROPU BY, but my skills are relatively shallow, so I can't understand it:
In AND DATEDIFF(now(), (SELECT MAX(`create_time`) FROM `order` o where o.store_id = s.id)) > 15
In this query condition, the subquery statement outputs MAX time, but if group aggregation is not used, the output is notShould there be only one row of data?Why can I achieve my requirement of grouping by store_id?Although I probably guessed that the core lies in the following o.store_id = s.id, but I didn't understand it
边栏推荐
- Creo 9.0 基准特征:基准坐标系
- 为什么我推荐使用智能化async?
- 无题一
- Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
- 开源一夏|OpenHarmony如何查询设备类型(eTS)
- (转)[Json]net.sf.json 和org.json 的差别及用法
- 21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
- MySQL使用聚合函数可以不搭配GROUP BY分组吗?
- 仿SBUS与串口数据固定转换
- 阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
猜你喜欢
随机推荐
无题十一
使用HBuilder离线本地打包ipa教程
茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
无题十二
shell脚本实例
无题九
leetcode refers to Offer 10- II. Frog jumping steps
C语言-数组
Is there a problem with writing this?How to synchronize data in sql-client
dotnet OpenXML 解析 PPT 图表 面积图入门
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
15.1.1、md—md的基础语法,快速的写文本备忘录
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
How to realize the short press and long press detection of the button?
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
PAT Level B - B1021 Single Digit Statistics (15)
The Seven Weapons of Programmers
Two-table query average grouping in sql server
交换机端口的三种类型详解与hybrid端口实验