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

@ -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>