SQL中Group分组获取Top N方法实现可首选row_number
网络编程 2021-07-05 13:42www.168986.cn编程入门
统计每个城市的最新10个产品本文采用了游标方法/Count查询/cross apply方法/row_number方法等等对比不难发现Group获取Top N场景时,可以首选row_number,游标cursor,两个就基本不考虑了
有产品表,包含id,name,city,addtime四个字段,因报表需要按城市分组,统计每个城市的最新10个产品,便向该表中插入了100万数据,做了如下系列测试
CREATE TABLE [dbo].[products](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[addtime] [datetime] NULL,
[city] [nvarchar](10) NULL,
CONSTRAINT [PK_products] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
1、采用row_number方法,执行5次,平均下来8秒左右,速度最快。
select no, id,name,city
from (select no =row_number() over (partition by city order by addtime desc), from products)t
where no< 11 order by city asc,addtime desc
2、采用cross apply方法,执行了3次,基本都在3分5秒以上,已经很慢了。
select distinct b.id,b.name,b.city from products a
cross apply (select 10 from products where city = a.city order by addtime desc) b
3、采用Count查询,只执行了两次,第一次执行到5分钟时,取消任务执行了;第二次执行到13分钟时,没有hold住又直接停止了,实在无法忍受。
select id,name,city from products a
where ( select count(city) from products where a.city = city and addtime>a.addtime) < 10
order by city asc,addtime desc
4、采用游标方法,这个测试的,执行了5次,每次都是10秒完成,感觉还不错。
declare @city nvarchar(10)
create table #Top(id int,name nvarchar(50),city nvarchar(10),addtime datetime)
declare mycursor cursor for
select distinct city from products order by city asc
open mycursor
fetch next from mycursor into @city
while @@fetch_status =0
begin
insert into #Top
select 10 id,name,city,addtime from products where city = @city
fetch next from mycursor into @city
end
close mycursor
deallocate mycursor
Select from #Top order by city asc,addtime desc
drop table #Top
通过上述对比不难发现,在面临Group获取Top N场景时,可以首选row_number,游标cursor,两个就基本不考虑了,数据量大的时候根本没法使用。
代码如下:
CREATE TABLE [dbo].[products](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[addtime] [datetime] NULL,
[city] [nvarchar](10) NULL,
CONSTRAINT [PK_products] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
1、采用row_number方法,执行5次,平均下来8秒左右,速度最快。
代码如下:
select no, id,name,city
from (select no =row_number() over (partition by city order by addtime desc), from products)t
where no< 11 order by city asc,addtime desc
2、采用cross apply方法,执行了3次,基本都在3分5秒以上,已经很慢了。
代码如下:
select distinct b.id,b.name,b.city from products a
cross apply (select 10 from products where city = a.city order by addtime desc) b
3、采用Count查询,只执行了两次,第一次执行到5分钟时,取消任务执行了;第二次执行到13分钟时,没有hold住又直接停止了,实在无法忍受。
代码如下:
select id,name,city from products a
where ( select count(city) from products where a.city = city and addtime>a.addtime) < 10
order by city asc,addtime desc
4、采用游标方法,这个测试的,执行了5次,每次都是10秒完成,感觉还不错。
代码如下:
declare @city nvarchar(10)
create table #Top(id int,name nvarchar(50),city nvarchar(10),addtime datetime)
declare mycursor cursor for
select distinct city from products order by city asc
open mycursor
fetch next from mycursor into @city
while @@fetch_status =0
begin
insert into #Top
select 10 id,name,city,addtime from products where city = @city
fetch next from mycursor into @city
end
close mycursor
deallocate mycursor
Select from #Top order by city asc,addtime desc
drop table #Top
通过上述对比不难发现,在面临Group获取Top N场景时,可以首选row_number,游标cursor,两个就基本不考虑了,数据量大的时候根本没法使用。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程