多线程如何在Java中更新大型表时提高性能?
我正在使用Azure Table Storage存储大约一百万条映射信息记录,例如<;trackId>;。应该通过向API端点发送查询来每月更新这些映射信息。我编写了以下代码来对每个记录进行简单检查,并更新存储在存储器中的记录
public void updateTrackProfileIdMappings() {
List<TrackProfileIdMapping> mappings = trackProfileIdMappingRepository.getAllMappings();
logger.info("Start updating TrackProfileIdMappings. {} mappings will be checked.", mappings.size());
Stopwatch stopwatch = Stopwatch.createStarted();
int count = 0;
for (TrackProfileIdMapping mapping : mappings) {
String trackId = mapping.getTrackId();
count = updateTrackProfileIdMapping(trackId, count);
if(countTrackProfileIdMappingUpdate % 10000 == 0) {
logger.info("Updating TrackProfileIdMappings: {} out of {} mappings have been checked and updated.", countTrackProfileIdMappingUpdate, mappings.size());
}
}
stopwatch.stop();
logger.info("Finished updating TrackProfileIdMappings. {} mappings have been checked and updated.", mappings.size());
}
private int updateTrackProfileIdMapping(String trackId, int count) {
try {
String profileId = ApiService.getProfileId(trackId);
trackProfileIdMappingRepository.insertOrUpdateMapping(trackId, profileId);
count++;
} catch (.ApiCallException e) {
logger.error("Failed to retrieve profileId for trackId {}", trackId);
}
}
而更新10k记录几乎需要30分钟。有没有一种方法可以使用多线程来提高Java的性能
共 (0) 个答案