为什么为 Jpa Repo 设置参数对于只有 2 列的简单 table 来说太慢了?
Why setting params for JpaRepo too slow for simple table with only 2 cols?
我正在使用仅包含 2 列的简单实体。在我的 table PK 中,col 是 db 中的 varchar,但实际存储的值是该 col 中的数字,而其他 col 是 int。数据库是 MS SQL 服务器。而这个table有1.65亿条记录。
Table结构:
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;
解决方案:sql server query running slow from java
我正在使用仅包含 2 列的简单实体。在我的 table PK 中,col 是 db 中的 varchar,但实际存储的值是该 col 中的数字,而其他 col 是 int。数据库是 MS SQL 服务器。而这个table有1.65亿条记录。
Table结构:
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;
解决方案:sql server query running slow from java