首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IN子句时Hibernate缓慢- MySQL。从MySQLWorkbench运行时,完全相同的查询运行速度要快得多

使用IN子句时Hibernate缓慢- MySQL。从MySQLWorkbench运行时,完全相同的查询运行速度要快得多
EN

Stack Overflow用户
提问于 2022-04-18 20:23:31
回答 1查看 187关注 0票数 0

我们有一张约有30列的简单桌子。当运行带有表上主键的简单IN子句时,Hibernate需要很长时间才能执行查询。但是,当我们在MySQLWorkbench中运行完全相同的查询时,返回速度要快得多。我们至少看到了10倍的差别。

启用Hibernate会话度量如下

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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();
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-04-19 17:36:07

出于某种原因,MySQL没有使用来自Hibernate的这个特定查询的索引。因此,我们在查询中使用了索引提示,强制索引关键字。

https://dev.mysql.com/doc/refman/8.0/en/index-hints.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71916694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档