【Sql Server】存储过程通过作业定时执行按天统计记录

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 通过前两篇文章的学习,我们已经对创建表、存储过程、作业等功能点有所了解本次将结合前面所学习的知识点,创建统计表以及结合作业定时按天以及实时统计域名各个长度的记录值
作者:小5聊基础
简介:一只喜欢全栈方向的程序员,欢迎咨询,尽绵薄之力答疑解惑
编程原则:Write Less Do More

统计效果
image.png

【创建统计表】
以时间字符串作为主键,确保每天只能由唯一一条统计记录

create table domain_count_day(
  id int identity(1,1),
  length_three_count int,
  length_four_count int,
  length_five_count int,
  length_six_count int,
  length_seven_count int,
  length_eight_count int,
  create_time varchar(10) primary key
)

【临时表】
创建临时的同时,需要加一个判断
如果临时表存在,那么可以根据自己业务情况,先删除临时表,然后再通过select into的方法将查询到数据直接插入到临时表里,省了创建临时表设置的字段和类型

使用说明
1)使用object_id方法
2)特别要注意,一定要是(N'tempdb..#temp',N'U')格式,否则容易出现判断不准的情况
3)删除临时表使用drop table关键词,和删除表操作一样
4)select * from into 临时表 from(目标查询) as 别名

--创建临时表
if object_id(N'tempdb..#temp',N'U') is not null
begin
    --删除临时表
    drop table #temp2    
end
select * into #temp2 from(
    select 
    domain_length,
    count(1) as count_length
    from dbo.domain_table
    where convert(varchar(10),create_time,120)=@time_rows
    group by domain_length
    --order by domain_length asc
) as aaa

【创建存储过程】

逻辑说明
1)定义今天和昨天的记录值变量
用于判断当前时间节点是否生成了记录,有记录则更新操作,没有记录则添加操作
2)再定义今天和昨天的时间值变量
用于过滤筛选今天和昨天的统计记录
3)再定义对应38位长度的值变量
4)创建临时表
以今天或昨天时间为筛选记录,以域名长度为分组,进行记录统计,并设置域名长度和总数两个字段,追加到临时表里
5)通过38长度为筛选条件,赋值到对象变量里
6)最后根据是否存在统计记录进行添加和更新

--drop proc countDomainValueDay;
create procedure countDomainValueDay
as
begin 
    
    declare @prev_time_rows int
    declare @time_rows int
    declare @length_three_count int
    declare @length_four_count int
    declare @length_five_count int
    declare @length_six_count int
    declare @length_seven_count int
    declare @length_eight_count int
    declare @day_time_prev varchar(50)
    declare @day_time varchar(50)
    
    -----当前时间的前一天-----
    set @day_time_prev=convert(varchar(10),dateadd(day,-1,getdate()),120)
    
    --创建临时表
    if object_id(N'#temp') is not null
    begin
        --删除临时表
        drop table #temp    
    end
    select * into #temp from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@day_time_prev
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp where domain_length=3
    select @length_four_count=count_length from #temp where domain_length=4
    select @length_five_count=count_length from #temp where domain_length=5
    select @length_six_count=count_length from #temp where domain_length=6
    select @length_seven_count=count_length from #temp where domain_length=7
    select @length_eight_count=count_length from #temp where domain_length=8
    
    --判断日统计记录是否存在 - 前一天
    select @prev_time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time_prev
    
    if @prev_time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time_prev)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time_prev
    end
    -----/当前时间的前一天-----
    
    
    -----当前时间-----
    set @day_time=convert(varchar(10),getdate(),120)
    
    --创建临时表
    if object_id(N'#temp2') is not null
    begin
        --删除临时表
        drop table #temp2    
    end
    select * into #temp2 from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@@day_time
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp2 where domain_length=3
    select @length_four_count=count_length from #temp2 where domain_length=4
    select @length_five_count=count_length from #temp2 where domain_length=5
    select @length_six_count=count_length from #temp2 where domain_length=6
    select @length_seven_count=count_length from #temp2 where domain_length=7
    select @length_eight_count=count_length from #temp2 where domain_length=8
    
    --判断日统计记录是否存在 - 当前
    select @time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time
    
    if @time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time
    end
end

【开启作业】
1)作业基本信息
image.png

2)步骤设置 数据库这里容易选错,如果没注意的话
image.png

3)设置计划 可以使用上一篇文章设置好的定时计划
image.png

4)开始作业
image.png

【统计效果】
从统计效果可以得到一个有意思的结论
1)首位出现的概率稍微小一点
2)越靠近中间,值就越对称接近

image.png

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
7天前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第18天】SQL Server 存储过程具有提高性能、增强安全性、代码复用和易于维护等优点。它可以减少编译时间和网络传输开销,通过权限控制和参数验证提升安全性,支持代码共享和复用,并且便于维护和版本管理。然而,存储过程也存在可移植性差、开发和调试复杂、版本管理问题、性能调优困难和依赖数据库服务器等缺点。使用时需根据具体需求权衡利弊。
|
3天前
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
|
6天前
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
7 1
|
8天前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第17天】SQL Server 存储过程是预编译的 SQL 语句集,存于数据库中,可重复调用。它能提高性能、增强安全性和可维护性,但也有可移植性差、开发调试复杂及可能影响数据库性能等缺点。使用时需权衡利弊。
|
13天前
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
27 3
|
6天前
|
SQL 存储 关系型数据库
mysql 数据库空间统计sql
mysql 数据库空间统计sql
19 0
|
2月前
|
存储 SQL 数据库
如何使用 SQL Server 创建存储过程?
【8月更文挑战第31天】
104 0
|
2月前
|
SQL 存储 关系型数据库
SQL SERVER 查询所有表 统计每张表的大小
SQL SERVER 查询所有表 统计每张表的大小
37 0
|
存储 SQL 数据库
SQL Server——为什么要使用存储过程?不使用是什么样的?
提高数据库执行速度,可能第一次见到这句话的小伙伴们感觉到非常的匪夷所思叭!怎么就提高了它的执行速度捏,从哪方面可以表现出来呢?既然这里要说到的是为什么要使用存储过程,也就是说它的优点是什么。那我们肯定就要对使用和不使用存储过程两方面来进行对比才能看出它的优点对吧。
|
存储 SQL Go
SQL Server 存储过程
SQL Server 存储过程
150 0