1. 程式人生 > >Lambda 在實際工作中應用

Lambda 在實際工作中應用

最近的專案裡要用到上傳Excel,並且上傳的Excel檔案也很大,足足有27Mb,有大概27 w 的資料量,不過解析是放在前端了,雖然這樣使用者也需要比較久的時間用來上傳。

但是短時間內,我能夠想到的就是這樣對於資料庫的壓力會比較大,所以我想到的就是對於資料庫的垂直拆分(但是技術老大說是類微服務,我不是很懂,因為感覺這不是一會兒事唉),將上傳的資料單獨儲存在一張表裡,並且將這一張表放在另外一個數據庫。(說到現在我還是感覺是資料庫的垂直拆分。。)

問題來了,而我就是要寫這樣的一個方法:

@Transactional
public void importer(String client, Transporter transporter, List<ShipmentAmendVM> reconciliations) {
    
if (client.equals("client")) { List<Reconciliation> reconciliationList = new ArrayList<>(); for (ShipmentAmendVM shipmentAmendVM : reconciliations) { shipmentAmendVM.setId(StringUtils.trimAllWhitespace(shipmentAmendVM.getId())); Reconciliation reconciliation =
new Reconciliation(); reconciliation.setId(shipmentAmendVM.getId()); reconciliation.setExtraFee(shipmentAmendVM.getExtraFee()); reconciliation.setWeight(shipmentAmendVM.getWeight()); reconciliation.setCost(shipmentAmendVM.getCost()); reconciliation.setReason(shipmentAmendVM.getReason()); reconciliation.setClient(client); reconciliation.setTransporter(transporter); reconciliation.setAmount(shipmentAmendVM.getAmount());
shipmentRepository.findById(shipmentAmendVM.getId()).ifPresent(shipment -> {
    reconciliation.setDeclaredPrice(shipment.getPrice());
    reconciliation.setDeclaredWeight(shipment.getWeight());
    shipmentRepository.updateStatus(ShipmentStatus.CHECKED, shipmentAmendVM.getId());
});
// 下面的是我之前寫的,效能真的爛的一逼。。。
/*if (shipmentRepository.findById(shipmentAmendVM.getId()).isPresent()) { Shipment shipment = shipmentRepository.findById(shipmentAmendVM.getId()).get(); reconciliation.setDeclaredPrice(shipment.getPrice()); reconciliation.setDeclaredWeight(shipment.getWeight()); shipmentRepository.updateStatus(ShipmentStatus.CHECKED, shipmentAmendVM.getId()); }*/ reconciliationList.add(reconciliation); } reconciliationRepository.saveAll(reconciliationList); }}
因為我會重複查兩次資料庫,而且是在27W的資料量下。老大幫我改了一下,這個時候感覺還是lambda 很好用,以後應該要多學習一下了。