[英]How to optimize a jpa query
I have a query which is taking a bit more time to fetch data from mysql db. 我有一个查询,要花更多时间从mysql db中获取数据。 There is only few records in database 数据库中只有很少的记录
@Query(" SELECT COUNT(*)
FROM Message n
WHERE n.deleted = false
AND n.sent = true
AND n.senderEnvelope.user = ?1
AND n.replyToMessage IN (
SELECT DISTINCT m.replyToMessage
FROM Message m , Relationship r
WHERE m.deleted = false
AND m.sent = true
AND m.senderEnvelope.user = ?1
AND ?2 MEMBER OF r.categories
)
")
Long countUniqueRepliesSentByUser(User currentUser ,
RecommendationCategory recommendationCategory);
Is there is a way by which i can optimize this query or right it in another way. 有没有一种方法可以优化此查询或以其他方式解决它。 Please help. 请帮忙。 Thanks in advance 提前致谢
The biggest problem is that there is no relationship specified for these two tables: 最大的问题是,没有为这两个表指定关系:
FROM Message m , Relationship r
That leads to a costly "cross join". 这导致代价高昂的“交叉连接”。
This seems like a syntax error; 这似乎是语法错误; what was it supposed to mean??: 它应该是什么意思?
AND ?2 MEMBER OF r.categories
After fixing those, then deal with IN
. 修复这些问题后,再处理IN
。 IN ( SELECT DISTINCT ... )
is poorly optimized. IN ( SELECT DISTINCT ... )
的优化不佳。 Furthermore the DISTINCT
is, as some level, redundant. 此外, DISTINCT
在某种程度上是多余的。 Consider changing it to 考虑将其更改为
EXISTS ( SELECT 1 FROM ...
AND n.replyToMessage = m.replyToMessage )
Please provide EXPLAIN SELECT ...
for any query you are asking about. 如果您有任何EXPLAIN SELECT ...
请提供EXPLAIN SELECT ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.