Spring Boot + JdbcTemplate有无法解释的查询缓慢?

时间:2016-09-16 23:09:34

标签: java mysql spring-mvc connection-pooling

我正在使用带有JdbcTemplate的Spring Boot和默认的Tomcat连接池。

当我在MVC应用程序中点击路径时,它使用JdbcTemplate运行查询。这是日志:

Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.441 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL update
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.443 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [UPDATE tokens SET last_accessed = NOW() where token = ?]
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.444 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.451 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate               : SQL update affected 1 rows
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.452 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.457  INFO 24249 --- [io-8080-exec-10] e.u.d.d.w.e.u.d.d.w.c.CensusController   : Before census query:Fri Sep 16 16:04:34 PDT 2016
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.458 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL query
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.459 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT o.acad_term_code, o.crse_num, o.acad_subject_code, sum(s.current_enrol_cnt) as current_enrol_cnt, sum(o.max_enrol_cnt) as max_enrol_cnt, s.snapshot_code, sum(s.wait_cnt) as wait_cnt FROM course_sect_snapshot s INNER JOIN course_sect_offering o ON s.crn_num = o.crn_num AND s.acad_term_code = o.acad_term_code AND s.vers = o.vers WHERE s.vers = (SELECT max(vers) FROM cdw_vers WHERE import_finished IS NOT NULL) AND o.acad_subject_code = ? AND o.crse_num = ? AND s.snapshot_code IN ('INSTR_BEG', 'CURRENT', 'DAY5', 'DAY10', 'DAY15') GROUP BY o.acad_term_code, o.acad_subject_code, o.crse_num, s.snapshot_code ORDER BY o.acad_term_code, o.crse_num]
Sep 16 16:04:34 beta.dw java[24249]: 2016-09-16 16:04:34.460 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
Sep 16 16:04:42 beta.dw java[24249]: 2016-09-16 16:04:42.243 DEBUG 24249 --- [io-8080-exec-10] o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
Sep 16 16:04:42 beta.dw java[24249]: 2016-09-16 16:04:42.244  INFO 24249 --- [io-8080-exec-10] e.u.d.d.w.e.u.d.d.w.c.CensusController   : After census query:Fri Sep 16 16:04:42 PDT 2016

如您所见,“获取JDBC连接”和“返回JDBC连接”之间大约需要8秒。

但是,查询不需要8秒。通过'mysql'命令在同一服务器上运行相同的查询,显示确切的查询(填入相同的参数)大约需要1.8秒。

缺少的6.2来自哪里?我怀疑是Tomcat连接池 - 例如,当我在启动Spring Boot后执行我的第一个查询时,只有那时 - 不是在启动但是在页面加载时 - Tomcat连接池是否会收集连接。

有任何想法如何调试这个?

1 个答案:

答案 0 :(得分:0)

这结果是与在VM中运行的MySQL相关的性能问题。