sql优化

This commit is contained in:
2025-06-30 13:11:05 +08:00
parent 49a3ce2c11
commit cebd845af1
13 changed files with 356 additions and 169 deletions

View File

@ -115,7 +115,7 @@
</delete>
<select id="getAmmeterDetailInfo" parameterType="String" resultType="com.xzzn.ems.domain.vo.AmmeterDataDetailInfo">
select t.category as category,
SELECT t.category as category,
t.total_kwh as totalKwh,
t.sharp_kwh as sharpKwh,
t.flat_kwh as flatKwh,
@ -123,10 +123,15 @@
t.valley_kwh as valleyKwh,
t.device_id as deviceId,
Max(t.data_update_time) as updateTime
from ems_ammeter_data t
where t.site_id = #{siteId}
and t.data_update_time = (select MAX(tmp.data_update_time) from ems_ammeter_data tmp where tmp.site_id = t.site_id and tmp.device_id = t.device_id)
group by t.category,t.total_kwh,t.flat_kwh,t.peak_kwh,t.valley_kwh,t.device_id
FROM ems_ammeter_data t
INNER JOIN (
SELECT p.site_id, p.device_id,p.category,MAX(p.data_update_time) AS max_update_time
FROM ems_ammeter_data p
WHERE p.site_id = #{siteId}
GROUP BY p.site_id,p.device_id,p.category
) latest on t.device_id = latest.device_id and t.data_update_time = latest.max_update_time
WHERE t.site_id = #{siteId}
group by t.category,t.total_kwh,t.sharp_kwh,t.flat_kwh,t.peak_kwh,t.valley_kwh,t.device_id
order by updateTime desc
</select>
</mapper>

View File

@ -311,27 +311,45 @@
<select id="getBMSBatteryCluster" parameterType="String" resultType="com.xzzn.ems.domain.vo.BMSBatteryClusterVo">
select td.device_name as deviceName, tmp.work_status as workStatus,
tmp.pcs_communication_status as pcsCommunicationStatus, tmp.ems_communication_status as emsCommunicationStatus,
tmp.cluster_voltage as clusterVoltage,tmp.chargeable_capacity as chargeableCapacity, tmp.total_charged_capacity as totalChargedCapacity,
tmp.cluster_current as clusterCurrent,tmp.dischargeable_capacity as dischargeableCapacity, tmp.total_discharged_capacity as totalDischargedCapacity,
tmp.soh as soh,tmp.average_temperature as averageTemperature,tmp.insulation_resistance as insulationResistance,
tmp.current_soc as currentSoc,tmp.site_id as siteId,tmp.device_id as deviceId
from ems_battery_cluster tmp left join ems_devices_setting td on tmp.device_id = td.id and tmp.site_id = td.site_id
tmp.pcs_communication_status as pcsCommunicationStatus, tmp.ems_communication_status as emsCommunicationStatus,
tmp.cluster_voltage as clusterVoltage,tmp.chargeable_capacity as chargeableCapacity,
tmp.total_charged_capacity as totalChargedCapacity,
tmp.cluster_current as clusterCurrent,tmp.dischargeable_capacity as dischargeableCapacity,
tmp.total_discharged_capacity as totalDischargedCapacity,
tmp.soh as soh,tmp.average_temperature as averageTemperature,tmp.insulation_resistance as insulationResistance,
tmp.current_soc as currentSoc,tmp.site_id as siteId,tmp.device_id as deviceId,
tmp.avg_cell_voltage as avgVoltage,tmp.avg_cell_temp as avgTemp,tmp.avg_cell_temp as avgSoc,
tmp.max_cell_voltage as maxVoltage,tmp.max_cell_voltage_id as maxVoltageId,
tmp.min_cell_voltage as minVolatage,tmp.min_cell_voltage_id as minVoltageId,
tmp.max_cell_temp as maxTem,tmp.max_cell_temp_id as maxTempId,
tmp.min_cell_temp as minTemp,tmp.min_cell_temp_id as minTempId,
tmp.max_cell_soc as maxSoc,tmp.max_cell_soc_id as maxSocId,
tmp.min_cell_soc as minSoc,tmp.min_cell_soc_id as minSocId
from ems_battery_cluster tmp left join ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
INNER JOIN (
SELECT p.site_id, p.device_id, MAX(p.update_time) AS max_update_time
FROM ems_battery_cluster p
WHERE p.site_id = #{siteId}
GROUP BY p.site_id,p.device_id
) latest on tmp.device_id = latest.device_id and tmp.update_time = latest.max_update_time
where tmp.site_id = #{siteId}
and tmp.update_time = (select MAX(t.update_time) FROM ems_battery_cluster t where t.site_id = tmp.site_id
and t.device_id = tmp.device_id)
order by tmp.device_id
</select>
<select id="getBmsBatteryData" resultType="com.xzzn.ems.domain.vo.BMSBatteryDataList">
select t.cluster_voltage as clusterVoltage,t.cluster_current as clusterCurrent,
t.current_soc as currentSoc,MAX(tb.voltage) as maxVoltage,MIN(tb.voltage) as minVoltage,
MAX(tb.temperature) as maxTemperature,MIN(tb.temperature) as minTemperature,
tb.site_id as siteId,tb.cluster_device_id as clusterId,tb.device_id as stackDeviceId
from ems_battery_cluster t left join ems_battery_data tb on t.device_id = tb.cluster_device_id
SELECT t.cluster_voltage as clusterVoltage,t.cluster_current as clusterCurrent,
t.current_soc as currentSoc,t.site_id as siteId,t.device_id as clusterId,
t.max_cell_voltage as maxVoltage,t.min_cell_voltage as minVoltage,
t.max_cell_temp as maxTemperature,t.min_cell_temp as minTemperature
FROM ems_battery_cluster t
INNER JOIN (
SELECT p.site_id, p.device_id, MAX(p.update_time) AS max_update_time
FROM ems_battery_cluster p
WHERE p.site_id = #{siteId} and p.stack_device_id = #{stackDeviceId}
GROUP BY p.site_id,p.device_id
) latest on t.device_id = latest.device_id and t.update_time = latest.max_update_time
where t.site_id = #{siteId} and t.stack_device_id = #{stackDeviceId}
and t.update_time = (select MAX(update_time) FROM ems_battery_cluster where site_id = t.site_id
and device_id = t.device_id and stack_device_id = t.stack_device_id)
group by t.cluster_voltage,t.cluster_current,t.current_soc,tb.site_id,tb.cluster_device_id,tb.device_id
group by t.cluster_voltage,t.cluster_current,t.current_soc,t.site_id,t.device_id,t.max_cell_voltage,t.min_cell_voltage,
t.max_cell_temp,t.min_cell_temp
</select>
</mapper>

View File

@ -126,8 +126,8 @@
<select id="getBatteryDataBySiteId" parameterType="String" resultMap="EmsBatteryDataResult">
<include refid="selectEmsBatteryDataVo"/>
where site_id = #{siteId}
and DATE(data_timestamp) = DATE(NOW())
WHERE site_id = #{siteId} AND data_timestamp &gt;= CURDATE()
ORDER BY data_timestamp desc limit 1
</select>
<select id="getBatteryDataByClusterId" resultType="com.xzzn.ems.domain.vo.BatteryClusterDataDetailVo">

View File

@ -305,16 +305,23 @@
</delete>
<select id="selectEmsBatteryStackBySiteId" parameterType="String" resultType="com.xzzn.ems.domain.vo.BMSOverViewVo">
select td.device_name as deviceName,tmp.work_status as workStatus,
tmp.pcs_communication_status as pcsCommunicationStatus,tmp.ems_communication_status as emsCommunicationStatus,
tmp.total_voltage as totalVoltage,tmp.chargeable_capacity as chargeableCapacity,tmp.total_charged_capacity as totalChargedCapacity,
tmp.total_current as totalCurrent,tmp.dischargeable_capacity as dischargeableCapacity,tmp.total_discharged_capacity as totalDischargedCapacity,
tmp.soh as soh,tmp.average_temperature as averageTemperature,tmp.insulation_resistance as insulationResistance,
tmp.current_soc as currentSoc,tmp.site_id as siteId,tmp.device_id as deviceId
from ems_battery_stack tmp left join ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
where tmp.site_id = #{siteId}
and tmp.update_time = (select MAX(t.update_time) FROM ems_battery_stack t where t.site_id = tmp.site_id
and t.device_id = tmp.device_id)
SELECT td.device_name as deviceName,tmp.work_status as workStatus,
tmp.pcs_communication_status as pcsCommunicationStatus,tmp.ems_communication_status as emsCommunicationStatus,
tmp.total_voltage as totalVoltage,tmp.chargeable_capacity as chargeableCapacity,
tmp.total_charged_capacity as totalChargedCapacity,
tmp.total_current as totalCurrent,tmp.dischargeable_capacity as dischargeableCapacity,
tmp.total_discharged_capacity as totalDischargedCapacity,
tmp.soh as soh,tmp.average_temperature as averageTemperature,tmp.insulation_resistance as insulationResistance,
tmp.current_soc as currentSoc,tmp.site_id as siteId,tmp.device_id as deviceId
FROM ems_battery_stack tmp
left join ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
INNER JOIN (
SELECT p.site_id, p.device_id, MAX(p.update_time) AS max_update_time
FROM ems_battery_stack p
WHERE p.site_id = #{siteId}
GROUP BY p.site_id,p.device_id
) latest on tmp.device_id = latest.device_id and tmp.update_time = latest.max_update_time
WHERE tmp.site_id = #{siteId}
order by tmp.device_id
</select>
</mapper>

View File

@ -214,7 +214,7 @@
WHERE t.site_id = #{siteId} AND t.device_id = #{deviceId}
) tmp
WHERE rn = 1
ORDER BY tmp.branch_id;
ORDER BY tmp.branch_id
</select>
<insert id="insertPcsBranchDataList" parameterType="java.util.List">

View File

@ -279,45 +279,57 @@
</foreach>
</delete>
<select id="getPcsDataBySiteId" parameterType="String" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
select CONCAT(date_month,'/',date_day) as ammeterDate,
sum(daily_ac_charge_energy) as chargedCap,
sum(daily_ac_discharge_energy) as disChargedCap
from ems_pcs_data
where site_id = #{siteId}
and date_sub(CURDATE(), interval 6 day) &lt;= create_time
group by date_month,date_day
</select>
<select id="getGridNrtPower" parameterType="String" resultType="java.math.BigDecimal">
select sum(total_active_power) as gridNrtPower
from ems_pcs_data
where site_id = #{siteId}
<select id="getPcsDataBySiteId" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
select CONCAT(t.date_month,'/',t.date_day) as ammeterDate,
sum(t.daily_ac_charge_energy) as chargedCap,
sum(t.daily_ac_discharge_energy) as disChargedCap
from (
SELECT p.site_id, p.device_id,p.date_month,p.date_day, MAX(p.data_update_time) AS max_update_time
FROM ems_pcs_data p
where p.site_id = #{siteId} and p.create_time >= CURDATE() - INTERVAL #{limitTime} DAY
GROUP BY p.site_id,p.device_id,p.date_month,p.date_day
) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
AND latest.device_id = t.device_id
AND latest.max_update_time = t.data_update_time
and latest.date_month = t.date_month
and latest.date_day = t.date_day
group by ammeterDate
order by ammeterDate desc;
</select>
<select id="getPcsTotalChargeData" resultType="map">
select SUM(t.daily_ac_charge_energy) as totalChargedCap,
SUM(t.daily_ac_discharge_energy) as totalDischargedCap
from ems_pcs_data t
where t.data_update_time = (select MAX(data_update_time) FROM ems_pcs_data
where site_id = t.site_id and device_id = t.device_id and date_month = t.date_month and date_day = t.date_day)
SELECT
SUM(t.total_ac_charge_energy) AS totalChargedCap,
SUM(t.total_ac_discharge_energy) AS totalDischargedCap,
SUM(t.total_active_power) as gridNrtPower
FROM (
SELECT p.site_id, p.device_id, MAX(p.data_update_time) AS max_update_time
FROM ems_pcs_data p inner join ems_site_setting s on p.site_id = s.site_id
<if test="siteId != null and siteId != ''">
where p.site_id = #{siteId}
</if>
GROUP BY p.site_id,p.device_id
) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
AND latest.device_id = t.device_id
AND latest.max_update_time = t.data_update_time
</select>
<select id="getSiteRunningHeadInfo" parameterType="String" resultType="com.xzzn.ems.domain.vo.SiteMonitorRunningHeadInfoVo">
select sum(a.total_active_power) as totalActivePower,
sum(a.total_reactive_power) as totalReactivePower,
sum(a.daily_ac_charge_energy) as dayChargedCap,
sum(a.daily_ac_discharge_energy) as dayDisChargedCap
from (
select t.total_active_power,t.total_reactive_power,
t.daily_ac_charge_energy,t.daily_ac_discharge_energy,
t.device_id,t.site_id,t.create_time
from ems_pcs_data t
where t.site_id = #{siteId}
and t.create_time = (select MAX(create_time) FROM ems_pcs_data where site_id = t.site_id and device_id = t.device_id)
group by t.device_id,t.total_active_power, total_reactive_power,
t.daily_ac_charge_energy,t.daily_ac_discharge_energy,t.site_id,t.create_time
) a
SELECT
SUM(t.total_active_power) as totalActivePower,
SUM(t.total_reactive_power) as totalReactivePower,
sum(t.daily_ac_charge_energy) as dayChargedCap,
sum(t.daily_ac_discharge_energy) as dayDisChargedCap,
#{siteId} as siteId
FROM (
SELECT p.device_id, MAX(p.data_update_time) AS max_update_time
FROM ems_pcs_data p
where p.site_id = #{siteId}
and p.data_update_time &gt;= CURDATE()
GROUP BY p.device_id
) latest inner join ems_pcs_data t ON latest.device_id = t.device_id
AND latest.max_update_time = t.data_update_time
AND t.site_id = #{siteId}
</select>
<select id="getElectDataList" resultType="com.xzzn.ems.domain.vo.ElectricIndexList">
@ -335,7 +347,7 @@
</select>
<select id="getPcsDetailInfoBySiteId" parameterType="String" resultType="com.xzzn.ems.domain.vo.PcsDetailInfoVo">
select td.device_name as deviceName,
SELECT td.device_name as deviceName,
td.communication_status as communicationStatus,
tmp.site_id as siteId,
tmp.device_id as deviceId,
@ -349,10 +361,14 @@
tmp.c_phase_current as cPhaseVoltage,tmp.c_phase_current as cPhaseCurrent,
tmp.total_power_factor as totalPowerFactor,
tmp.pcs_environment_temperature as pcsEnvironmentTemperature,tmp.ac_frequency as acFrequency
from ems_pcs_data tmp left join ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
where tmp.site_id = #{siteId}
and tmp.data_update_time = (select MAX(data_update_time) FROM ems_pcs_data where site_id = tmp.site_id
and device_id = tmp.device_id)
FROM ems_pcs_data tmp
LEFT JOIN ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
INNER JOIN ( SELECT p.site_id, p.device_id, MAX(p.data_update_time) AS max_update_time
FROM ems_pcs_data p
WHERE p.site_id = #{siteId}
GROUP BY p.site_id,p.device_id
) latest on tmp.device_id = latest.device_id and tmp.data_update_time = latest.max_update_time
WHERE tmp.site_id = #{siteId}
order by tmp.device_id
</select>
</mapper>