Skip to content

Commit

Permalink
hotfix: 쿼리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
wugawuga committed Jun 8, 2024
1 parent 2a2bdd7 commit d8fad12
Showing 1 changed file with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ public List<Product> searchProductsByTopTagsFirst(final Long tagId, final Pageab
final String jpql = """
SELECT DISTINCT p
FROM Product p
WHERE p.id IN (
SELECT p2.id
FROM Product p2
JOIN p2.reviews r2
JOIN r2.reviewTags rt2
WHERE rt2.tag.id = :tagId
AND rt2.tag.id IN (
SELECT rt3.tag.id
FROM Review r3
JOIN r3.reviewTags rt3
WHERE r3.product.id = p2.id
GROUP BY rt3.tag.id
ORDER BY COUNT(rt3.tag.id) DESC
LIMIT 3
)
)
JOIN p.reviews r
JOIN r.reviewTags rt
JOIN (
SELECT rt2.tag.id AS tagId
FROM ReviewTag rt2
GROUP BY rt2.tag.id
ORDER BY COUNT(rt2.tag.id) DESC
LIMIT 3
) AS topTags
ON rt.tag.id = topTags.tagId
WHERE rt.tag.id = :tagId
ORDER BY p.id DESC
""";

Expand All @@ -52,27 +47,20 @@ ORDER BY COUNT(rt3.tag.id) DESC
public List<Product> searchProductsByTopTags(final Long tagId, final Long lastProductId, final Pageable pageable) {
final String jpql = """
SELECT DISTINCT p
FROM Product p
WHERE p.id < :lastProductId
AND p.id IN (
SELECT p2.id
FROM Product p2
JOIN p2.reviews r2
JOIN r2.reviewTags rt2
WHERE rt2.tag.id = :tagId
AND rt2.tag.id IN (
SELECT rt3.tag.id
FROM Review r3
JOIN r3.reviewTags rt3
WHERE r3.product.id = p2.id
GROUP BY rt3.tag.id
ORDER BY COUNT(rt3.tag.id) DESC
LIMIT 3
)
GROUP BY p2.id
HAVING COUNT(DISTINCT rt2.tag.id) <= 3
)
ORDER BY p.id DESC
FROM Product p
JOIN p.reviews r
JOIN r.reviewTags rt
JOIN (
SELECT rt2.tag.id AS tagId
FROM ReviewTag rt2
GROUP BY rt2.tag.id
ORDER BY COUNT(rt2.tag.id) DESC
LIMIT 3
) AS topTags
ON rt.tag.id = topTags.tagId
WHERE rt.tag.id = :tagId
AND p.id < :lastProductId
ORDER BY p.id DESC
""";

final TypedQuery<Product> query = entityManager.createQuery(jpql, Product.class);
Expand Down

0 comments on commit d8fad12

Please sign in to comment.