【发布时间】:2021-06-21 15:19:27
【问题描述】:
我有一个 Spring5 应用程序使用 NamedParameterJdbcTemplate 从 OracleDB 获取数据。
当我从 intellij DB 控制台执行相同的查询时,我会在 1.5 秒内得到结果,但是当我使用 Java 应用程序执行相同的查询时
jdbcTemplate.queryForList(query, params); 需要 90 秒
这是 DBconfigration 类:
@Configuration
@EnableJpaRepositories("com.xxxx.xxx.relational.repositories")
@PropertySource(value = "classpath:jdbc.properties")
@ComponentScan
@EnableTransactionManagement
@Import(RestTemplateConfig.class)
public class RelationalConfig {
@Bean(destroyMethod = "close")
public BasicDataSource dataSource(@Value("${jdbc.driverClassName}") String driverClassName,
@Value("${jdbc.url}") String url, @Value("${jdbc.username}") String username,
@Value("${jdbc.password}") String password) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
@Bean
HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
@Autowired
LocalContainerEntityManagerFactoryBean entityManagerFactory(BasicDataSource dataSource,
HibernateJpaVendorAdapter vendor, @Value("${hibernate.dialect}") String dialect,
@Value("${jdbc.schema}") String schema) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(vendor);
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", dialect);
jpaProperties.put("hibernate.default_schema", schema);
// jpaProperties.put("hibernate.jdbc.fetch_size", 200);
// jpaProperties.put("hibernate.connection.pool_size", 10);
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.setPackagesToScan("com.xxx.xxx.relational.entities");
return entityManagerFactoryBean;
}
@Bean
@Autowired
JpaTransactionManager transactionManager(EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
}
@Bean
@Autowired
NamedParameterJdbcTemplate jdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setFetchSize(200);
return new NamedParameterJdbcTemplate(jdbcTemplate);
}
@Bean
@Autowired
SiblingsRepo<LegDTO> legSiblingsRepo(LegRepository legRepo) {
return new SiblingsRepo<LegDTO>() {
@Override
public Optional<LegDTO> byNext(LegDTO next) {
return legRepo.findByNextLegId(next.getId());
}
@Override
public Optional<LegDTO> byId(Integer id) {
return legRepo.findById(id);
}
@Override
public LegDTO save(LegDTO sibling) {
return legRepo.save(sibling);
}
};
}
@Bean
@Autowired
SiblingsRepo<JourneyDTO> journeySiblingsRepo(JourneyRepository journeyRepo) {
return new SiblingsRepo<JourneyDTO>() {
@Override
public Optional<JourneyDTO> byNext(JourneyDTO next) {
return journeyRepo.findByNextJourneyId(next.getId());
}
@Override
public Optional<JourneyDTO> byId(Integer id) {
return journeyRepo.findById(id);
}
@Override
public JourneyDTO save(JourneyDTO sibling) {
return journeyRepo.save(sibling);
}
};
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
return new PropertySourcesPlaceholderConfigurer();
}
}
这里是查询:
String q = "select journeyid, extid, extccuid
from ACTIVE_JOURNEY_VIEW
WHERE coalesce(ata, nextJourneyEtd, sysdate + 1) > sysdate
AND ((organizationid = :orgId AND deleted = 0)
OR organizationid = :orgId
OR enclosingHire_organizationId = :orgId
OR gln_orig_organizationid = :orgId
OR originFacilityId in (
select facilityid
from v_FacilityAndBbrOrg
where bbrorgid = :orgId
and (nvl(atd, etd), activeEndDate) overlaps (fromDate, toDate))
OR gln_dest_organizationid = :orgId
OR destinationFacilityId in (
select facilityid
from v_FacilityAndBbrOrg
where bbrorgid = :orgId
and (nvl(atd, etd), activeEndDate) overlaps (fromDate, toDate))
)";
protected <T> List<T> query(String key, RowMapper<T> mapper, Object... kvs) {
Map<String, Object> params = map(kvs);
return jdbcTemplate.queryForList(q, params);
}
【问题讨论】:
-
您是否从 intellij 和您的应用程序连接到同一个数据库服务器?
-
是的,都连接到同一个 dB 服务器
-
您是否在控制台中获取完整的结果?
-
是的,在 java 应用程序和控制台上执行相同的查询,我得到相同的结果,但响应时间非常不同
-
比较苹果和橙子。发生的情况是您还有一个将结果转换为对象的行映射器。根据数据量,这需要很多时间。还有你测量的时间是什么时候?只有 db 执行或 HTTP 页面的呈现。
标签: java sql oracle spring-data-jpa jdbctemplate