简体   繁体   English

MySQL JDBC查询比JMH测试中的查询要快得多

[英]MySQL JDBC query much faster than it should be in JMH test

I am currently trying to benchmark MySQL queries using JHM. 我目前正在尝试使用JHM对MySQL查询进行基准测试。 For this, I established a connection using JDBC with a dockerized server. 为此,我使用JDBC与dockerized服务器建立了连接。

My Code: 我的代码:

@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();
}

These are the results of the tests: 这些是测试的结果:

# 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

The query contains multiple joins and the database contains data so the benchmarks should not be so low. 该查询包含多个联接,而数据库包含数据,因此基准不应太低。

The same query in DataGrip results in: DataGrip中的相同查询导致:

0 rows retrieved in 59ms (execution: 32ms, fetching: 27ms)

What I have tried: 我尝试过的

  • Running RESET QUERY CACHE and FLUSH TABLES in the @TearDown method @TearDown方法中运行RESET QUERY CACHEFLUSH TABLES

  • Setting the @TearDown to Level.Iteration @TearDown设置为Level.Iteration

  • Setting autoCommit to false and manually commiting the query (see: code) + setting the TransactionLevel to READ_COMMITTED autoCommit设置为false并手动提交查询(请参见:代码)+将TransactionLevel设置为READ_COMMITTED

The results the query returns using JDBC are correct though so it is properly executed. 但是,使用JDBC查询返回的结果是正确的,因此可以正确执行。

Any ideas? 有任何想法吗? All help is appreciated! 感谢所有帮助!

It seems like the 'issue' was related to using a PreparedStatement , once I switched over to a normal Statement , I get the same time values I get in Datagrip. 似乎“问题”与使用PreparedStatement ,一旦我切换到普通Statement ,我将获得与Datagrip中相同的时间值。

From this SO answer I found out that a PreparedStatement does "Precompilation and DB-side caching of the SQL statement" which proabably lead to the faster execution times. 这个SO答案中,我发现PreparedStatement执行“ SQL语句的预编译和数据库侧缓存”,这有可能导致更快的执行时间。

So the slower code I am now using: 所以我现在使用的代码较慢:

    @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM