I am using simple Entity with 2 cols only. In my table PK col is varchar in db but in actual values stored is numerical in that col and other col is int. DB is MS SQL Server. And this table has 165 million records.
Table structure:
SECURITY_TAB
varchar(30) SECID PK;
int VERSION;
Entity
@Entity
@Table(name = "SECURITY_TAB")
public class Security {
@Id
@Column
private String secid;
@Column
private int version;
//... getter n setter n toString
}
Repository
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();
}
TestClass
@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()));
}
}
This simple query is taking more than 20 secs, While when I run similar query MS SQL Server Mgmt Studio or hard code the value in the query result is returned in mill seconds. So what is going wrong?