Я использую спецификации Spring для взаимодействия с базой данных, однако я вижу, что в сгенерированных SQL-запросах есть избыточные объединения, которые делают запрос очень медленным, вот код:
Subquery < Mission > usersAccessSubquery = query.subquery(Mission.class);
Root < Mission > usersAccessSubqueryRoot = usersAccessSubquery.from(Mission.class);
usersAccessSubquery.select(usersAccessSubqueryRoot);
Subquery < Mission > groupesAccessSubquery = query.subquery(Mission.class);
Root < Mission > groupesAccessSubqueryRoot = groupesAccessSubquery.from(Mission.class);
groupesAccessSubquery.select(groupesAccessSubqueryRoot);
Subquery < Mission > groupesAccessSubqueryAdmin = query.subquery(Mission.class);
Root < Mission > groupesAccessSubqueryRootAdmin = groupesAccessSubqueryAdmin.from(Mission.class);
groupesAccessSubqueryAdmin.select(groupesAccessSubqueryRootAdmin);
Predicate usersAccessPredicate = cb.equal(cb.lower(usersAccessSubqueryRoot.join("usersAccess").join("userRef").get("email")), authUserMail.toLowerCase());
Predicate groupesAccessPredicate = cb.equal(cb.lower(groupesAccessSubqueryRoot.join("groupesAccess").join("users").join("userRef").get("email")), authUserMail.toLowerCase());
Predicate groupesAccessPredicateAdmin = cb.equal(cb.lower(groupesAccessSubqueryRootAdmin.join("groupe").get("label")), groupe.getLabel().toLowerCase());
Predicate rootPredicate = cb.conjunction();
usersAccessSubquery.where(usersAccessPredicate);
groupesAccessSubquery.where(groupesAccessPredicate);
groupesAccessSubqueryAdmin.where(groupesAccessPredicateAdmin);
query.where(cb.and(
cb.or(
cb.equal(cb.lower(cb.trim(root.get("createdBy"))), authUserMail.toLowerCase().trim()),
cb.in(root.join("mission").get("id")).value(usersAccessSubquery),
cb.in(root.join("mission").get("id")).value(groupesAccessSubquery),
cb.in(root.join("mission").get("id")).value(groupesAccessSubqueryAdmin)
),
rootPredicate
));
исгенерированный SQL-запрос:
SELECT DISTINCT user0_.id AS id1_25_,
user0_.created_by AS created_2_25_,
user0_.creation_date AS creation3_25_,
user0_.groupe_id AS groupe_i6_25_,
user0_.role_id AS role_id7_25_,
user0_.update_date AS update_d4_25_,
user0_.updated_by AS updated_5_25_,
user0_.user_ref_id AS user_ref8_25_
FROM users user0_
INNER JOIN user_ref userref1_
ON user0_.user_ref_id = userref1_.id
INNER JOIN user_ref userref2_
ON user0_.user_ref_id = userref2_.id
INNER JOIN user_ref userref3_
ON user0_.user_ref_id = userref3_.id
INNER JOIN groupes groupe4_
ON user0_.groupe_id = groupe4_.id
INNER JOIN mission missions5_
ON groupe4_.id = missions5_.client_entity_id
INNER JOIN groupes groupe6_
ON user0_.groupe_id = groupe6_.id
INNER JOIN mission missions7_
ON groupe6_.id = missions7_.client_entity_id
INNER JOIN groupes groupe8_
ON user0_.groupe_id = groupe8_.id
INNER JOIN mission missions9_
ON groupe8_.id = missions9_.client_entity_id
WHERE ( Lower(userref1_.username) LIKE 'a%'
OR Lower(userref2_.first_name) LIKE 'a%'
OR Lower(userref3_.last_name) LIKE 'a%' )
AND ( missions5_.id IN (SELECT mission10_.id
FROM mission mission10_
INNER JOIN groupes groupe11_
ON mission10_.groupe_id =
groupe11_.id
WHERE Lower(groupe11_.label) =
'tls team')
OR missions7_.id IN (SELECT mission12_.id
FROM mission mission12_
INNER JOIN mission_users_access
usersacces13_
ON mission12_.id =
usersacces13_.mission_id
INNER JOIN users user14_
ON
usersacces13_.users_access_id =
user14_.id
INNER JOIN user_ref userref15_
ON user14_.user_ref_id =
userref15_.id
WHERE Lower(userref15_.departement) =
'test@test.com')
OR missions9_.id IN (SELECT mission16_.id
FROM mission mission16_
INNER JOIN mission_groupes_access
groupesacc17_
ON mission16_.id =
groupesacc17_.mission_id
INNER JOIN groupes groupe18_
ON
groupesacc17_.groupes_access_id =
groupe18_.id
INNER JOIN users users19_
ON groupe18_.id =
users19_.groupe_id
INNER JOIN user_ref userref20_
ON users19_.user_ref_id =
userref20_.id
WHERE Lower(userref20_.departement) =
'test@test.com') )
вот избыточные объединения:
INNER JOIN user_ref userref1_
ON user0_.user_ref_id = userref1_.id
INNER JOIN user_ref userref2_
ON user0_.user_ref_id = userref2_.id
INNER JOIN user_ref userref3_
ON user0_.user_ref_id = userref3_.id
INNER JOIN groupes groupe4_
ON user0_.groupe_id = groupe4_.id
INNER JOIN mission missions5_
ON groupe4_.id = missions5_.client_entity_id
INNER JOIN groupes groupe6_
ON user0_.groupe_id = groupe6_.id
INNER JOIN mission missions7_
ON groupe6_.id = missions7_.client_entity_id
INNER JOIN groupes groupe8_
ON user0_.groupe_id = groupe8_.id
INNER JOIN mission missions9_
ON groupe8_.id = missions9_.client_entity_id
, которые можно заменить на это:
INNER JOIN user_ref userref1_
ON user0_.user_ref_id = userref1_.id
INNER JOIN groupes groupe4_
ON user0_.groupe_id = groupe4_.id
INNER JOIN mission missions5_
ON groupe4_.id = missions5_.client_entity_id
, что делает время выполнения намного быстрее,Я думаю, что запрос в коде оптимизирован, и я не знаю, почему сгенерированный запрос выглядит так, кто-нибудь может объяснить, пожалуйста.