【发布时间】:2021-03-02 05:40:07
【问题描述】:
我有 JPA 语句,在执行时执行简单查询。
我检查了日志, 它在可接受的时间内执行,但为了执行下一条语句,准备下一条语句需要时间
这里是日志
2021-03-01T12:35:42.008614Z 84 Prepare SELECT * from table where id=?
2021-03-01T12:35:42.008810Z 84 Execute SELECT * from table where id=4
2021-03-01T12:35:42.012826Z 84 Close stmt
2021-03-01T12:35:42.033090Z 84 Prepare SELECT * from table where id=?
2021-03-01T12:35:42.033279Z 84 Execute SELECT * from table where id=5
2021-03-01T12:35:42.033860Z 84 Close stmt
2021-03-01T12:35:42.054576Z 84 Prepare SELECT * from table where id=?
2021-03-01T12:35:42.054792Z 84 Execute SELECT * from table where id=6
2021-03-01T12:35:42.055372Z 84 Close stmt
时间差可能看起来不大但是这条语句要执行1200+次左右,所以最后的延迟实在是太大了
有什么办法可以减少转换时间(关闭语句和下一条准备语句之间的时间)?
中间没有语句或代码行导致任何时间延迟,它是循环中的单个语句
:更新->
我通过在 application.yml 文件中启用 CachePrepStmt 属性来减少转换时间
所以新的转换时间大约是 0.1 毫秒,但现在执行时间是 20 毫秒
怎么会这样,有什么想法吗?
这是新的日志语句
2021-03-02T06:20:51.249367Z 59 Execute SELECT * from table where id=5
2021-03-02T06:20:51.269273Z 59 Reset stmt
2021-03-02T06:20:51.269385Z 59 Execute SELECT * from table where id=6
2021-03-02T06:20:51.289372Z 59 Reset stmt
2021-03-02T06:20:51.289512Z 59 Execute SELECT * from table where id=7
2021-03-02T06:20:51.308678Z 59 Reset stmt
2021-03-02T06:20:51.308812Z 59 Execute SELECT * from table where id=8
2021-03-02T06:20:51.328953Z 59 Reset stmt
2021-03-02T06:20:51.329123Z 59 Execute SELECT * from table where id=9
2021-03-02T06:20:51.348447Z 59 Reset stmt
【问题讨论】:
-
可以分享代码吗?
标签: java mysql hibernate jpa prepared-statement