SELECT DeviceId, CAST(ROUND(AVG(distance / time_diff), 5, 1) AS decimal(18,5)) AS '移动速率' FROM ( SELECT t1.DeviceId, t1.StartTime, t1.V1, t1.V2, t1.V3, DATEDIFF(SECOND, t2.StartTime, t1.StartTime) AS time_diff, SQRT(POWER(t1.V1 - t2.V1, 2) + POWER(t1.V2 - t2.V2, 2) + POWER(t1.V3 - t2.V3, 2)) AS distance FROM T_BMWY_ResolveDeviceData t1 JOIN T_BMWY_ResolveDeviceData t2 ON t1.DeviceId = t2.DeviceId AND t1.StartTime > t2.StartTime WHERE t1.StartTime >= DATEADD(DAY, -5, GETDATE()) ) t WHERE time_diff > 0 GROUP BY DeviceId 优化一下查询速度

时间: 2024-01-24 13:01:41 浏览: 24
可以考虑在WHERE子句中添加一些限制条件来减少查询的数据量。例如,可以根据DeviceId和StartTime将T_BMWY_ResolveDeviceData表分为多个分区,并对查询进行分区限制,只查询最近几天的数据。同时,可以考虑使用索引来加快查询速度。 以下是一个优化后的查询语句: ``` SELECT DeviceId, CAST(ROUND(AVG(distance / time_diff), 5, 1) AS decimal(18,5)) AS '移动速率' FROM ( SELECT t1.DeviceId, t1.StartTime, t1.V1, t1.V2, t1.V3, DATEDIFF(SECOND, t2.StartTime, t1.StartTime) AS time_diff, SQRT(POWER(t1.V1 - t2.V1, 2) + POWER(t1.V2 - t2.V2, 2) + POWER(t1.V3 - t2.V3, 2)) AS distance FROM T_BMWY_ResolveDeviceData t1 JOIN T_BMWY_ResolveDeviceData t2 ON t1.DeviceId = t2.DeviceId AND t1.StartTime > t2.StartTime WHERE t1.StartTime >= DATEADD(DAY, -5, GETDATE()) AND t1.StartTime >= '2021-01-01' -- 限制查询的起始时间 AND t1.DeviceId BETWEEN 1 AND 1000 -- 限制查询的DeviceId范围 ) t WHERE time_diff > 0 GROUP BY DeviceId; ``` 此外,还可以考虑使用分页查询,每次只查询部分结果,减少单次查询的数据量。例如,可以使用ROW_NUMBER()函数进行分页查询。

相关推荐

select concat(radio_mcc,Radio_MNC) as plmn,Radio_Brand,Radio_Network_Generation,left(Test_Timestamp,7) as month,traffic_bytes,traffic_direction,Location_Longitude as longtition,Location_Latitude as latitude cast((180.0 * (cast(floor(cast(Location_Longitude as double) * 20037508.34 / 180.0 / 300) as int) * 300 + 50) / 20037508.34) as decimal(30,10)) as grid_longitude, case when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() >1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() < -1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(-20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) else cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() * 20037508/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) end as grid_latitude, avg(throughput_receive) as download_bps, sum(test_samples) as samples from public_standard.ps04_p3_ntr__no where Radio_Network_Generation not in ('WiFi','Unknown','2G') and concat(radio_mcc,Radio_MNC)!='' and concat(radio_mcc,Radio_MNC) in (select a.plmn from (select concat(radio_mcc,Radio_MNC) as plmn,count(0) as num from public_standard.ps04_p3_ntr__no where concat(radio_mcc,Radio_MNC)!='' group by concat(radio_mcc,Radio_MNC) order by num desc limit 4)a ) group by radio_mcc,Radio_MNC,Radio_Brand,Radio_Network_Generation,month,traffic_bytes,traffic_direction,grid_longitude,grid_latitude,Location_Longitude,Location_Latitude;改为正确写法

请以最详细的方式解释这段代码with a01 as( select sm_id, isname, case when isname in ('冰箱', '冷柜') then '制冷' when isname = '商空' then '楼宇' when isname = '家空' then '空调' else isname end as cyx, factory_code, mtname, curstatus, model_code, model_name, start_time, --'保养起始时间' expire_time, --'保养到期时间' substring(start_time, 1, 7) byqsny, substring(expire_time, 1, 7) byjssj, case when stype = '2' then '二保' when stype = '3' then '三保' else stype end as bylx, case when mm_type = '2' then '二保' when mm_type = '3' then '三保' when mm_type = '4' then '三保带二保' else mm_type end as mmtype, --实际保养类型 upper_time, --上次保养时间 mm_time, --实际保养时间 scustcode, -- 实供应商编码 scustname, --实供应商名称 mould_status, --模具维保状态 mm_status, --实际是否保养 smould_status --模具实际维保状态 from dwd_mm_staymaintain_all a where by_status <> '无需保养' and length(factory_code) = 4 and curstatus not in ('报废中', '已报废', '冻结数据', '垃圾数据') and isname in ('冰箱', '冷柜', '家空', '商空', '洗涤', '厨电', '热水器') and substring(expire_time, 1, 10) <= from_unixtime(unix_timestamp(), 'yyyy-MM-dd') and expire_time is not null and expire_time <> '' ), b01 as( select cyx, byjssj, count(sm_id) by_ying from a01 group by cyx, byjssj ), b02 as( select cyx, byjssj, count(sm_id) by_shi from a01 where mm_status = '已保养' group by cyx, byjssj ),c01 as( select from_unixtime(unix_timestamp(), 'yyyy-MM') months, t1.cyx industry, t1.byjssj byjsyf, cast(t1.by_ying as string) by_ying, cast(t2.by_shi as string) by_shi, cast(t2.by_shi/t1.by_ying as decimal(8,5)) bywcl from b01 t1 left join b02 t2 on t1.cyx = t2.cyx and t1.byjssj = t2.byjssj) insert overwrite table dh_yf.tt_mould_asset_maintenance_overview select months, industry, byjsyf, by_ying, by_shi, cast(bywcl as string) bywcl, from_unixtime(unix_timestamp(), 'yyyy-MM-dd') etl_date, '%' unit from c01

create table $x00256702.$pr01_temp_p3_ntr_grid_new__1604666481550 as select concat(radio_mcc,Radio_MNC) as plmn,Radio_Brand,Radio_Network_Generation, left(Test_Timestamp,7) as month, cast((180.0 * (cast(floor(cast(Location_Longitude as double) * 20037508.34 / 180.0 / 300) as int) * 300 + 50) / 20037508.34) as decimal(30,10)) as grid_longitude, case when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() >1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() < -1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(-20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) else cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() * 20037508/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) end as grid_latitude, avg(throughput_receive) as download_bps,sum(test_samples) as samples from $public_standard.$ps04_p3_ntr__za where Radio_RXLevel>=-150 and Radio_RXLevel<=-10 and Radio_Network_Generation!='WiFi' and traffic_direction='Downlink' and concat(radio_mcc,Radio_MNC)!='' and concat(radio_mcc,Radio_MNC) in (select a.plmn from (select concat(radio_mcc,Radio_MNC) as plmn,count(0) as num from $public_standard.$ps04_p3_ntr__za where concat(radio_mcc,Radio_MNC)!='' group by concat(radio_mcc,Radio_MNC) order by num desc limit 4)a ) and left(test_timestamp,7) in (left(cast(months_add(now(),0) as string),7), left(cast(months_add(now(),-1) as string),7), left(cast(months_add(now(),-2) as string),7), left(cast(months_add(now(),-3) as string),7), left(cast(months_add(now(),-4) as string),7), left(cast(months_add(now(),-5) as string),7), left(cast(months_add(now(),-6) as string),7)) group by radio_mcc,Radio_MNC,Radio_Brand,Radio_Network_Generation,month,grid_longitude,grid_latitude ;解析一下这个算法脚本

最新推荐

recommend-type

Python decimal模块使用方法详解

主要介绍了Python decimal模块使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

C#中decimal保留2位有效小数的实现方法

主要介绍了C#中decimal保留2位有效小数的实现方法,针对decimal变量保留2位有效小数有多种方法,可以使用Math.Round方法以及ToString先转换为字符串等操作来实现。具体实现方法感兴趣的朋友跟随小编一起看看吧
recommend-type

MySQL数据类型中DECIMAL的用法实例详解

2 3 4 5 6,则结果为1. 2。如果将相同的值存入FLOAT(8, 4) 的列中,则结果为1. 2 3 4 6。 这表示应该定义具有足够位数的浮点列以便得到尽可能精确的值。如果想精确到千分之一,那就不要定义使该类型仅有两位小数。 ...
recommend-type

详解MySQL数据类型DECIMAL(N,M)中N和M分别表示的含义

关于MySQL数据类型decimal中n和m分别表示什么含义?本文就此问题作了简单论述,并创建相关表进行验证,需要的朋友可以了解下。
recommend-type

基于matlab实现V2G系统simulink仿真图以及电动汽车充电和放电图.rar

基于matlab实现V2G系统simulink仿真图以及电动汽车充电和放电图.rar
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

info-center source defatult

这是一个 Cisco IOS 命令,用于配置 Info Center 默认源。Info Center 是 Cisco 设备的日志记录和报告工具,可以用于收集和查看设备的事件、警报和错误信息。该命令用于配置 Info Center 默认源,即设备的默认日志记录和报告服务器。在命令行界面中输入该命令后,可以使用其他命令来配置默认源的 IP 地址、端口号和协议等参数。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。