我们有一张约有30列的简单桌子。当运行带有表上主键的简单IN子句时,Hibernate需要很长时间才能执行查询。但是,当我们在MySQLWorkbench中运行完全相同的查询时,返回速度要快得多。我们至少看到了10倍的差别。
启用Hibernate会话度量如下
Session Metrics {
10542 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
197875 nanoseconds spent preparing 1 JDBC statements;
76234640084 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}使用Hibernate执行76.2秒。但是,在MySQL上执行的完全相同的查询(从Hibernate和MySQL慢速查询日志中检索)显示了0.071秒的执行时间。
我们试过使用hibernate.query.in_clause_parameter_padding,但没有任何区别。尝试了一些其他的东西,但似乎没有什么效果。
Hibernate代码
Transaction tx = session.beginTransaction();
String hql = "SELECT d FROM Document d WHERE d.ddmObjectId IN (:ids)";
Query query = session.createQuery(hql);
List<UUID> listOfUUIDs = List.of(UUID.fromString("0de14895-bf1e-11ec-a830-02c68fc6d6d6"),UUID.fromString("0de14db9-bf1e-11ec-a830-02c68fc6d6d6"));
query.setParameterList("ids",listOfUUIDs);
List<Document> objectList = query.list();
Iterator iterator = objectList.iterator();
while (iterator.hasNext()) {
Document document = (Document) iterator.next();
System.out.println(document.toString());
}
tx.commit();
}有什么想法吗?
发布于 2022-04-19 17:36:07
出于某种原因,MySQL没有使用来自Hibernate的这个特定查询的索引。因此,我们在查询中使用了索引提示,强制索引关键字。
https://stackoverflow.com/questions/71916694
复制相似问题