Browse Source

优化sql

jwy_category
review512jwy@163.com 1 month ago
parent
commit
8e750bac46
  1. 79
      src/main/java/com/techsor/datacenter/sender/dao/DashboardAlertDao.java

79
src/main/java/com/techsor/datacenter/sender/dao/DashboardAlertDao.java

@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -80,46 +81,48 @@ public class DashboardAlertDao {
DataSourceContextHolder.setCurrentDataSourceKey(dsKey);
// 批量执行 upsert
jdbcTemplate.batchUpdate(
StringBuilder sql = new StringBuilder(
"INSERT INTO device_rawdata_realtime (" +
"device_id, building_id, status, receive_ts, alert_title, alert_content, alert_cancel_title," +
"alert_cancel_content, raw_data, upload_year, upload_month, upload_day) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE " +
"building_id = VALUES(building_id), " +
"status = VALUES(status), " +
"receive_ts = VALUES(receive_ts)," +
"alert_title = VALUES(alert_title)," +
"alert_content = VALUES(alert_content)," +
"alert_cancel_title = VALUES(alert_cancel_title)," +
"alert_cancel_content = VALUES(alert_cancel_content)," +
"raw_data = VALUES(raw_data)," +
"upload_year = VALUES(upload_year)," +
"upload_month = VALUES(upload_month)," +
"upload_day = VALUES(upload_day)",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
DynamodbEntity entity = subList.get(i).getEntity();
ps.setString(1, entity.getDeviceId());
ps.setLong(2, entity.getDbBuildingId());
ps.setString(3, entity.getStatus());
ps.setLong(4, entity.getReceive_ts());
ps.setString(5, entity.getAlertTitle());
ps.setString(6, entity.getAlertContent());
ps.setString(7, entity.getAlertCancelTitle());
ps.setString(8, entity.getAlertCancelContent());
ps.setString(9, entity.getRawData());
ps.setInt(10, Integer.parseInt(entity.getYearKey()));
ps.setInt(11, Integer.parseInt(entity.getMonthKey()));
ps.setInt(12, Integer.parseInt(entity.getDayKey()));
}
public int getBatchSize() {
return subList.size();
}
}
"device_id, building_id, status, receive_ts, " +
"alert_title, alert_content, alert_cancel_title, alert_cancel_content, " +
"raw_data, upload_year, upload_month, upload_day) VALUES "
);
List<Object> params = new ArrayList<>();
for (int i = 0; i < subList.size(); i++) {
DynamodbEntity entity = subList.get(i).getEntity();
sql.append("(?,?,?,?,?,?,?,?,?,?,?,?)");
if (i < subList.size() - 1) {
sql.append(",");
}
params.add(entity.getDeviceId());
params.add(entity.getDbBuildingId());
params.add(entity.getStatus());
params.add(entity.getReceive_ts());
params.add(entity.getAlertTitle());
params.add(entity.getAlertContent());
params.add(entity.getAlertCancelTitle());
params.add(entity.getAlertCancelContent());
params.add(entity.getRawData());
params.add(Integer.parseInt(entity.getYearKey()));
params.add(Integer.parseInt(entity.getMonthKey()));
params.add(Integer.parseInt(entity.getDayKey()));
}
sql.append(" ON DUPLICATE KEY UPDATE ")
.append("building_id = VALUES(building_id), ")
.append("status = VALUES(status), ")
.append("receive_ts = VALUES(receive_ts), ")
.append("alert_title = VALUES(alert_title), ")
.append("alert_content = VALUES(alert_content), ")
.append("alert_cancel_title = VALUES(alert_cancel_title), ")
.append("alert_cancel_content = VALUES(alert_cancel_content), ")
.append("raw_data = VALUES(raw_data), ")
.append("upload_year = VALUES(upload_year), ")
.append("upload_month = VALUES(upload_month), ")
.append("upload_day = VALUES(upload_day)");
jdbcTemplate.update(sql.toString(), params.toArray());
} finally {
DataSourceContextHolder.clearCurrentDataSourceKey();
}

Loading…
Cancel
Save