使用Spring Data JPA批量选择

时间:2018-09-17 06:34:58

标签: java spring hibernate spring-data-jpa

我正在查询一个大约有10万条记录的Spring Data JPA存储库,并且在获取记录后,我遍历了每个记录。

下面是代码:

    List<RegistrationHistograms> histogramList = 
   registrationHistoRepository.findStaticRegistrationHistograms();
    List<RegistrationHistograms> modifiedList = new ArrayList();
    histogramList.forEach(histogram -> {
        int totalOrgIdRegistrations = 
      registrationRepository.findTotalRegistrationByOrganization(histogram
                .getOrganizationId());
        List<Object[]> topDomainList = 
       userRepository.countTopDomain(histogram.getOrganizationId());
        if (CollectionUtils.isNotEmpty(topDomainList) || 
          totalOrgIdRegistrations != 0) {
            Object[] topValueArray = topDomainList.get(0);
            String topDomain = (String) topValueArray[0];
            BigInteger topDomainCount = (BigInteger) topValueArray[1];
            int totalDomainRegistration = 

          registrationRepository.countRegistrationsByEmailDomain(topDomain,
                    histogram.getOrganizationId());
            int topDomainPct = topDomainCount.intValue() * 100 / 
            totalOrgIdRegistrations;
            histogram.setOrganizationId(histogram.getOrganizationId());
            histogram.setDomain(topDomain);
            histogram.setDomainPercentage(topDomainPct);
            histogram.setTotalRegistrations(totalOrgIdRegistrations);
            histogram.setTotalDomainRegistration(totalDomainRegistration);
            modifiedList.add(histogram);
        }
    });
    batchRepository.bulkSaveOrUpdate(modifiedList);

使用的实体:

@Entity
@Table(name = "srs_registration_histogram")
@Getter
@Setter
public class RegistrationHistograms extends BaseEntity {    
@Id
@Column(name = "organization_id")
private String organizationId;

@Column(name = "total_registration")
private long totalRegistrations;

@Column(name = "domain_percentage")
private int domainPercentage;

@Column(name = "total_domain_registration")
private long totalDomainRegistration;

@Column(name = "domain")
private String domain;

@Column(name = "is_static_data")
private String isStaticData;

考虑到大量数据,此方法可能会出现性能问题。

是否可以批处理以下查询:

 List<RegistrationHistograms> histogramList = 
 registrationHistoRepository.findStaticRegistrationHistograms();

或者我们不能一一制作,而是可以一次从直方图列表中收集所有organizationsId,并查询registrationRepository数据库一次以提高性能。

0 个答案:

没有答案