spring-boot - 为什么对于只有 2 列的简单表来说,为 JpaRepo 设置参数太慢?
问题描述
我只使用带有 2 个列的简单实体。在我的表中,PK col 是 db 中的 varchar,但实际存储的值是该 col 中的数字,而其他 col 是 int。DB 是 MS SQL Server。而这张表有1.65亿条记录。
表结构:
SECURITY_TAB
varchar(30) SECID PK;
int VERSION;
实体
@Entity
@Table(name = "SECURITY_TAB")
public class Security {
@Id
@Column
private String secid;
@Column
private int version;
//... getter n setter n toString
}
存储库
public interface SecurityRepository extends JpaRepository<Security, String> {
@Query("SELECT s from Security s where secid= :secid")
Security findSecurityBySecid(@Param("secid") String secid)); //this is slow
//below is fine.
//@Query("SELECT s from Security s where secid='1111'")
//Security findSecurityBySecid();
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SecurityTests {
@Autowired
private SecurityRepository securityRepository;
@Test
public void testSecurityBySecid() {
Instant start = Instant.now();
Security security = securityRepository.findSecurityBySecid("1111");
System.out.println(Duration.between(start, Instant.now()));
System.out.println(security);
System.out.println(Duration.between(start, Instant.now()));
}
}
这个简单的查询需要 20 多秒,而当我运行类似的查询 MS SQL Server Mgmt Studio 或硬编码时,查询结果中的值会以毫秒为单位返回。那么出了什么问题呢?
解决方案
能够解决:解决方案是在 jdbc url 中设置此属性:sendStringParametersAsUnicode=false
如果您使用的是 MS SQL 官方驱动程序,则完整示例:jdbc:sqlserver://yourserver;instanceName=yourInstance;databaseName=yourDBName;sendStringParametersAsUnicode=false;
推荐阅读
- visual-studio - 是否有任何自动化方法可以在 TFS 2018 中创建集合和项目
- angular - 带有 AOT 编译器的 NGRX angular 5 减速器
- javascript - 如何使用没有任何参数的函数在 selenium 中执行 Javascript?
- spring-boot - 集成测试spring boot
- java - 使用jsoup删除外部标签和内部标签的最佳方法是什么
- scala - PlayFramework - 上传文件的 FakeRequest
- extjs - ExtJs-> Ext.Store.proxy.writer 错误:没有为 writer.create 指定类型
- reactjs - 如何通过 npm 删除警告?
- linux - 两台 linux 服务器之间交换的数据
- java - 如何使用 JGIT 从 GIT Hub 中仅获取新更改