Python全系列 教程
3567个小节阅读:5929.2k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
select brand_id,item_id,count(user_id) number
from user_log
where brand_id is not null
and item_id is not null
group by brand_id,item_id
limit 30;
limit 30是为了暂时方便分析结果:
xxxxxxxxxx
brand_id item_id number
7347 3 124
2079 7 3
6170 10 2
3158 14 2
2746 18 90
6910 22 22
5714 34 36
4529 41 155
4755 59 4
8455 87 14
82 98 24
3473 113 1
768 23 7
6585 24 132
4549 28 17
698 33 7
404 43 124
712 47 6
4866 57 18
7592 71 9
4777 72 3
3074 8 51
650 16 2
2516 26 10
4844 42 4
5434 48 9
3700 50 91
1654 52 1
1374 60 18
2736 78 6
Time taken: 128.722 seconds, Fetched: 30 row(s)
想将相同品牌的数据放到一起。
xxxxxxxxxx
select brand_id,item_id,count(user_id) number
from user_log
where brand_id is not null
and item_id is not null
group by brand_id,item_id
cluster by brand_id
limit 30;
查询结果:
xxxxxxxxxx
brand_id item_id number
1 255761 14
1 250434 3
1 240697 3
1 207315 17
1 203876 45
1 199849 4
1 199557 7
1 196609 444
1 173853 5
1 163662 43
1 163179 8
1 116900 47
1 116563 90
1 94789 5
1 92538 6
1 74928 1
1 86801 2
1 85789 14
1 19314 9
1 63220 3
1 43587 98
1 42165 8
1 40379 16
1 39192 40
1 39001 22
1 30362 3
1 21420 22
1 86957 1
1 17301 21
1 8355 2
Time taken: 122.022 seconds, Fetched: 30 row(s)
以上为真实结果,为了方便分析,我们通过模拟数据进行分析:
xxxxxxxxxx
brand_id item_id number
1 255761 14
1 199849 4
1 199557 7
1 196609 444
2 250434 3
2 240697 3
2 207315 17
2 203876 45
xxxxxxxxxx
brand_id item_id number rk
1 196609 444 1
1 255761 14 2
1 199557 7 3
1 199849 4 4
2 203876 45 1
2 207315 17 2
2 250434 3 3
2 240697 3 4
优化sql语句:
xxxxxxxxxx
select brand_id,item_id,number,rk
from (
select brand_id,item_id,number,
row_number() over(partition by brand_id order by number desc) rk
from (
select brand_id,item_id,count(user_id) number
from user_log
where brand_id is not null
and item_id is not null
group by brand_id,item_id
cluster by brand_id) tba) tbb
where tbb.rk<=3;
查询结果:
xxxxxxxxxx
8475 103981 23 1
8475 977504 20 2
8475 752808 8 3
8476 38860 1916 1
8476 871890 1427 2
8476 1054 793 3
8477 852870 49 1
8477 201803 46 2
8477 179560 40 3