hibernate Sping Boot Java应用程序与SQL Server配合使用时出现性能问题

kmynzznz  于 2023-02-04  发布在  Java
关注(0)|答案(2)|浏览(123)

我遇到了Spring Boot Java应用程序从SQL Server数据库获取数据的性能问题。一个简单的查询如下:

SELECT a, b, SUM(c)
FROM table
WHERE date = '2023-02-01' AND year = 2023
GROUP BY a, b

查询的结果是12行。当我在SSMS中执行此查询时,结果立即出现。如果通过浏览器窗口中的Spring Boot应用程序或通过Postman执行查询,则随机需要3.5到10秒才能获得结果。
该表包含大约800万行和14列:

  • 1个PK INT色谱柱
  • 1个日期列
  • 二进制小数(12,2)
  • 10台NVARCHAR(x)

Spring Boot应用程序中的查询是用带@Query注解的Native SQL编写的,我的同事们都使用ASP.NET作为后端,而且他们都是拿同一台SQL Server,性能非常好,看起来是配置问题。
到目前为止我已经尝试过:
1.我将sendParametersAsUnicode=false添加到www.example.com application.properties
1.我复制了整个表格:我对所有与字符串相关的列使用VARCHAR(x),而不是NVARCHAR(x)。
1.我用 Postman 追踪了执行时间:〉99%的执行时间落在类别转移时间内。
1.我尝试用@Nationalized注解NVARCHAR(x)列
1.我读到过这个:https://vladmihalcea.com/sql-server-jdbc-sendstringparametersasunicode/以了解NVARCHAR(x)和VARCHAR(x)在Spring Boot和SQL Server方面的区别。
有什么想法可以改变游戏规则,将执行时间从几秒减少到远低于一秒?
下面是我的项目的更多代码:
application.properties:

spring.datasource.url=jdbc:sqlserver://10.191.144.180:1433;database=Spring;encrypt=true;trustServerCertificate=true;sendStringParametersAsUnicode=false
spring.datasource.username=username
spring.datasource.password=pw123456
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.open-in-view=false

检查床分类:

@Entity
@Table(name="Fact_Snapshots_Agg")
public class FSAGG {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    Date filedate;
    String jahr;
    String a;
    String b;
    String d;
    String e;
    String f;
    String g;
    String h;
    float c;
    float i;
    String j;
    String k;
}
plus constructors and getters and setters

资源文件:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.analytics_test.model.FSAGGTInterface;
import com.analytics_test.service.FSAGGService;

@RestController
@CrossOrigin
@RequestMapping("/FSAGG")
public class FSAGGResource {
    private final FSAGGService fsaggService;
    
    @Autowired
    public FSAGGResource(FSAGGService fsaggService) { this.fsaggService = fsaggService; }
    
    @GetMapping("/Actuals/Total/{jahr}/gesamt")
    public List<FSAGGTInterface> getActualsTotalGesamt(@PathVariable("jahr") String jahr) { return fsaggService.getActualsTotalGesamt(jahr); }

}

存储库文件:

package com.analytics_test.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.analytics_test.model.FSAGG;
import com.analytics_test.model.FSAGGTInterface;

public interface FSAGGRepository extends JpaRepository<FSAGG, Long> {
    
    @Query(value = "a as a, b as b, SELECT SUM(c) as c "
            + "FROM FSAGG WHERE filedate = '2023-02-01' AND year = :year "
            + "GROUP BY a, b"
            )
    List<FSAGGTInterface> getActualsTotalGesamt(@Param("year") String year);
    
}

服务类别:

package com.analytics_test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.analytics_test.model.FSAGGTInterface;
import com.analytics_test.repository.FSAGGRepository;

@Component
@Service
public class FSAGGService {

    private final FSAGGRepository fsaggRepository;
    
    @Autowired
    public FSAGGService(FSAGGRepository fsaggRepository) { this.fsaggRepository = fsaggRepository; }
    
    public List<FSAGGTInterface> getStuff() { return fsaggRepository.getStuff(); }
    
    public List<FSAGGTInterface> getActualsTotalGesamt(String jahr) { return fsaggRepository.getActualsTotalGesamt(jahr); }

}
z3yyvxxp

z3yyvxxp1#

重写服务方法getActualsTotalGesamt,以确认问题是查询速度慢,而不是Spring Boot 中的其他问题。

public List<FSAGGTInterface> getActualsTotalGesamt(String jahr) {
    Date start = new Date();
    List<FSAGGTInterface> retVal = fsaggRepository.getActualsTotalGesamt(jahr);
    System.out.println(“repo call duration ms: “ + ((new Date()).getTime() - start.getTime()));
    return retVal;
     }
u91tlkcl

u91tlkcl2#

设置spring.jpa.show-sql=true并显示生成的请求

相关问题