fix(ems): 优化物联网数据查询 SQL

- 调整温度、湿度、噪声的正常范围为 [0, 79]
- 在查询中加入设备类型过滤,排除无效数据,根据设备类型过滤掉负责字段为0的数据
boardTest
zch 1 month ago
parent 790914a082
commit bd86f818a5

@ -130,40 +130,56 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 使用 UNION ALL 从多个表查询物联网数据列表 -->
<select id="selectRecordIotenvInstantListFromTables" resultMap="RecordIotenvInstantResult">
<foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid, monitorId, temperature, humidity, illuminance, noise, concentration,
vibration_speed, vibration_displacement, vibration_acceleration, vibration_temp,
collectTime, recodeTime
FROM ${tableName}
SELECT t.objid, t.monitorId, t.temperature, t.humidity, t.illuminance, t.noise, t.concentration,
t.vibration_speed, t.vibration_displacement, t.vibration_acceleration, t.vibration_temp,
t.collectTime, t.recodeTime
FROM ${tableName} t
LEFT JOIN ems_base_monitor_info ebmi ON t.monitorId = ebmi.monitor_code
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and t.monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and t.temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and t.humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and t.illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and t.noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and t.concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and t.vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and t.vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and t.vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and t.vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and t.collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and t.recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
AND t.recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
AND t.monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
</if>
<!-- 过滤异常数据:温度、湿度、噪声范围 [1, 79] -->
<!-- 过滤异常数据:温度、湿度、噪声范围 [0, 79] -->
<!-- 优化使用BETWEEN和简化NULL检查提高TiDB性能 -->
AND (temperature IS NULL OR temperature BETWEEN 1 AND 79)
AND (humidity IS NULL OR humidity BETWEEN 1 AND 79)
AND (noise IS NULL OR noise BETWEEN 1 AND 79)
AND (t.temperature IS NULL OR t.temperature BETWEEN 0 AND 79)
AND (t.humidity IS NULL OR t.humidity BETWEEN 0 AND 79)
AND (t.noise IS NULL OR t.noise BETWEEN 0 AND 79)
<!-- 根据设备类型过滤掉负责字段为0的数据 -->
<!-- 对于没有设备信息的记录保留所有非0数据 -->
AND (
-- 温度设备(type=5)过滤温度为0的数据
(ebmi.monitor_type = 5 AND t.temperature > 0) OR
-- 温湿度设备(type=6)过滤温度和湿度都为0的数据
(ebmi.monitor_type = 6 AND (t.temperature > 0 OR t.humidity > 0)) OR
-- 噪声设备(type=7)过滤噪声为0的数据
(ebmi.monitor_type = 7 AND t.noise > 0) OR
-- 振动设备(type=10)过滤振动相关字段都为0的数据
(ebmi.monitor_type = 10 AND (t.vibration_speed > 0 OR t.vibration_displacement > 0 OR t.vibration_acceleration > 0 OR t.vibration_temp > 0)) OR
-- 其他类型设备或无设备信息:保留所有有效数据
(ebmi.monitor_type NOT IN (5, 6, 7, 10) OR ebmi.monitor_type IS NULL)
)
</where>
</foreach>
@ -223,29 +239,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
(SELECT t.objid, t.monitorId, t.temperature, t.humidity, t.illuminance, t.noise, t.concentration,
t.vibration_speed, t.vibration_displacement, t.vibration_acceleration, t.vibration_temp,
t.collectTime, t.recodeTime,
ebmi.monitor_name
COALESCE(ebmi.monitor_name, t.monitorId) as monitor_name
FROM ${tableName} t
left join ems_base_monitor_info ebmi on t.monitorId = ebmi.monitor_code
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and t.monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and t.temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and t.humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and t.illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and t.noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and t.concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and t.vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and t.vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and t.vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and t.vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and t.collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and t.recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
AND t.recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
AND t.monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
@ -253,9 +269,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 过滤异常数据:温度、湿度、噪声范围 [1, 79] -->
<!-- 优化使用BETWEEN和简化NULL检查提高TiDB性能 -->
AND (temperature IS NULL OR temperature BETWEEN 1 AND 79)
AND (humidity IS NULL OR humidity BETWEEN 1 AND 79)
AND (noise IS NULL OR noise BETWEEN 1 AND 79)
AND (t.temperature IS NULL OR t.temperature BETWEEN 0 AND 79)
AND (t.humidity IS NULL OR t.humidity BETWEEN 0 AND 79)
AND (t.noise IS NULL OR t.noise BETWEEN 0 AND 79)
<!-- 根据设备类型过滤掉负责字段为0的数据 -->
<!-- 对于没有设备信息的记录保留所有非0数据 -->
AND (
-- 温度设备(type=5)过滤温度为0的数据
(ebmi.monitor_type = 5 AND t.temperature > 0) OR
-- 温湿度设备(type=6)过滤温度和湿度都为0的数据
(ebmi.monitor_type = 6 AND (t.temperature > 0 OR t.humidity > 0)) OR
-- 噪声设备(type=7)过滤噪声为0的数据
(ebmi.monitor_type = 7 AND t.noise > 0) OR
-- 振动设备(type=10)过滤振动相关字段都为0的数据
(ebmi.monitor_type = 10 AND (t.vibration_speed > 0 OR t.vibration_displacement > 0 OR t.vibration_acceleration > 0 OR t.vibration_temp > 0)) OR
-- 其他类型设备或无设备信息:保留所有有效数据
(ebmi.monitor_type NOT IN (5, 6, 7, 10) OR ebmi.monitor_type IS NULL)
)
</where>
)
@ -276,28 +307,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="countFromTables" resultType="java.lang.Integer">
SELECT COUNT(*) FROM (
<foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid
FROM ${tableName}
SELECT t.objid
FROM ${tableName} t
LEFT JOIN ems_base_monitor_info ebmi ON t.monitorId = ebmi.monitor_code
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and t.monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and t.temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and t.humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and t.illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and t.noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and t.concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and t.vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and t.vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and t.vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and t.vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and t.collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and t.recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
AND t.recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
AND t.monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
@ -305,9 +337,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 过滤异常数据:温度、湿度、噪声范围 [1, 79] -->
<!-- 优化使用BETWEEN和简化NULL检查提高TiDB性能 -->
AND (temperature IS NULL OR temperature BETWEEN 1 AND 79)
AND (humidity IS NULL OR humidity BETWEEN 1 AND 79)
AND (noise IS NULL OR noise BETWEEN 1 AND 79)
AND (t.temperature IS NULL OR t.temperature BETWEEN 0 AND 79)
AND (t.humidity IS NULL OR t.humidity BETWEEN 0 AND 79)
AND (t.noise IS NULL OR t.noise BETWEEN 0 AND 79)
<!-- 根据设备类型过滤掉负责字段为0的数据 -->
<!-- 对于没有设备信息的记录保留所有非0数据 -->
AND (
-- 温度设备(type=5)过滤温度为0的数据
(ebmi.monitor_type = 5 AND t.temperature > 0) OR
-- 温湿度设备(type=6)过滤温度和湿度都为0的数据
(ebmi.monitor_type = 6 AND (t.temperature > 0 OR t.humidity > 0)) OR
-- 噪声设备(type=7)过滤噪声为0的数据
(ebmi.monitor_type = 7 AND t.noise > 0) OR
-- 振动设备(type=10)过滤振动相关字段都为0的数据
(ebmi.monitor_type = 10 AND (t.vibration_speed > 0 OR t.vibration_displacement > 0 OR t.vibration_acceleration > 0 OR t.vibration_temp > 0)) OR
-- 其他类型设备或无设备信息:保留所有有效数据
(ebmi.monitor_type NOT IN (5, 6, 7, 10) OR ebmi.monitor_type IS NULL)
)
</where>
</foreach>
@ -375,31 +422,48 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
t1.vibration_speed, t1.vibration_displacement, t1.vibration_acceleration, t1.vibration_temp,
t1.collectTime, t1.recodeTime
FROM ${tableName} t1
LEFT JOIN ems_base_monitor_info ebmi ON t1.monitorId = ebmi.monitor_code
INNER JOIN (
SELECT
monitorId,
FLOOR(UNIX_TIMESTAMP(recodeTime) / (#{recordIotenvInstant.samplingInterval} * 60)) as time_slot,
MAX(recodeTime) as max_time
FROM ${tableName}
t.monitorId,
FLOOR(UNIX_TIMESTAMP(t.recodeTime) / (#{recordIotenvInstant.samplingInterval} * 60)) as time_slot,
MAX(t.recodeTime) as max_time
FROM ${tableName} t
LEFT JOIN ems_base_monitor_info ebmi2 ON t.monitorId = ebmi2.monitor_code
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''">
AND monitorId = #{recordIotenvInstant.monitorId}
AND t.monitorId = #{recordIotenvInstant.monitorId}
</if>
<if test="recordIotenvInstant.params.beginRecordTime != null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime &gt;= #{recordIotenvInstant.params.beginRecordTime}
AND recodeTime &lt;= #{recordIotenvInstant.params.endRecordTime}
AND t.recodeTime &gt;= #{recordIotenvInstant.params.beginRecordTime}
AND t.recodeTime &lt;= #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
AND t.monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
</if>
AND (temperature IS NULL OR temperature BETWEEN 1 AND 79)
AND (humidity IS NULL OR humidity BETWEEN 1 AND 79)
AND (noise IS NULL OR noise BETWEEN 1 AND 79)
AND (t.temperature IS NULL OR t.temperature BETWEEN 0 AND 79)
AND (t.humidity IS NULL OR t.humidity BETWEEN 0 AND 79)
AND (t.noise IS NULL OR t.noise BETWEEN 0 AND 79)
<!-- 根据设备类型过滤掉负责字段为0的数据 -->
<!-- 对于没有设备信息的记录保留所有非0数据 -->
AND (
-- 温度设备(type=5)过滤温度为0的数据
(ebmi2.monitor_type = 5 AND t.temperature > 0) OR
-- 温湿度设备(type=6)过滤温度和湿度都为0的数据
(ebmi2.monitor_type = 6 AND (t.temperature > 0 OR t.humidity > 0)) OR
-- 噪声设备(type=7)过滤噪声为0的数据
(ebmi2.monitor_type = 7 AND t.noise > 0) OR
-- 振动设备(type=10)过滤振动相关字段都为0的数据
(ebmi2.monitor_type = 10 AND (t.vibration_speed > 0 OR t.vibration_displacement > 0 OR t.vibration_acceleration > 0 OR t.vibration_temp > 0)) OR
-- 其他类型设备或无设备信息:保留所有有效数据
(ebmi2.monitor_type NOT IN (5, 6, 7, 10) OR ebmi2.monitor_type IS NULL)
)
</where>
GROUP BY monitorId, time_slot
GROUP BY t.monitorId, time_slot
) t2 ON t1.monitorId = t2.monitorId AND t1.recodeTime = t2.max_time
</foreach>
ORDER BY monitorId ASC, recodeTime ASC

Loading…
Cancel
Save