当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
开源一夏|OpenHarmony如何查询设备类型(eTS)
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
Xcode 12 ld: symbol(s) not found for architecture armv64
How to realize the short press and long press detection of the button?
ffmpeg drawtext add text watermark
施一公:科学需要想象,想象来自阅读
Rotation of the displayed value on the button
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
放大器OPA855的噪声计算实例
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
sql server中 两表查询 平均数 分组
Why do I recommend using smart async?
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
After Keil upgrades to AC6, what changes?
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
汇编语言(8)x86内联汇编
19.服务器端会话技术Session
tensorflow.keras无法引入layers
请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
Happens-before rules for threads