[英]MySQL JDBC query much faster than it should be in JMH test
我目前正在嘗試使用JHM對MySQL查詢進行基准測試。 為此,我使用JDBC與dockerized服務器建立了連接。
我的代碼:
@Benchmark
public void testWithPreparedStatement(Blackhole bh) throws SQLException {
PreparedStatement st = connection.prepareStatement(query);
st.setString(1, "Parameter1");
st.setString(2, "Parameter2");
bh.consume(st.executeQuery());
connection.commit();
st.close();
}
這些是測試的結果:
# Warmup Iteration 1: 2.318 ms/op
Iteration 1: 2.038 ms/op
Iteration 2: 1.972 ms/op
Iteration 3: 1.908 ms/op
Iteration 4: 2.000 ms/op
Iteration 5: 1.960 ms/op
Iteration 6: 1.939 ms/op
Iteration 7: 1.968 ms/op
Iteration 8: 1.959 ms/op
該查詢包含多個聯接,而數據庫包含數據,因此基准不應太低。
DataGrip中的相同查詢導致:
0 rows retrieved in 59ms (execution: 32ms, fetching: 27ms)
我嘗試過的
在@TearDown
方法中運行RESET QUERY CACHE
和FLUSH TABLES
將@TearDown
設置為Level.Iteration
將autoCommit
設置為false
並手動提交查詢(請參見:代碼)+將TransactionLevel
設置為READ_COMMITTED
但是,使用JDBC查詢返回的結果是正確的,因此可以正確執行。
有任何想法嗎? 感謝所有幫助!
似乎“問題”與使用PreparedStatement
,一旦我切換到普通Statement
,我將獲得與Datagrip中相同的時間值。
從這個SO答案中,我發現PreparedStatement
執行“ SQL語句的預編譯和數據庫側緩存”,這有可能導致更快的執行時間。
所以我現在使用的代碼較慢:
@Benchmark
public void testWithStatement(Blackhole bh) throws SQLException{
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
bh.consume(rs.next());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.