Ruslan Mukhamadiarov’s Post

🔥 𝗗𝗜𝗦𝗧𝗜𝗡𝗖𝗧 𝗱𝗶𝗱𝗻’𝘁 𝗳𝗶𝘅 𝘆𝗼𝘂𝗿 𝗾𝘂𝗲𝗿𝘆. 𝗜𝘁 𝗷𝘂𝘀𝘁 𝗵𝗶𝗱 𝘁𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺. You saw duplicates → added DISTINCT → result “looks correct”. But the database still did the wrong work. Here are 4 real cases where DISTINCT lies to you 👇 1️⃣ 𝗢𝗻𝗲-𝘁𝗼-𝗺𝗮𝗻𝘆 𝗝𝗢𝗜𝗡 (𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗘𝗫𝗜𝗦𝗧𝗦) SELECT o.* FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE oi.status = 'ACTIVE'; 🤕 duplicates 🚑 DISTINCT 🔍 Problem: You multiplied rows. One order → many items → many rows. ✅ Fix: WHERE EXISTS (...) 👉 If you don’t need child data — don’t JOIN it. 2️⃣ 𝗖𝗮𝗿𝘁𝗲𝘀𝗶𝗮𝗻 𝗺𝘂𝗹𝘁𝗶𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻 (𝗺𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝗝𝗢𝗜𝗡𝘀) FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN payments p ON p.order_id = o.id 3 items × 4 payments = 12 rows 🤕 data explosion 🚑 DISTINCT 🔍 Problem: You multiplied relationships. ✅ Fix: WHERE EXISTS (...) AND EXISTS (...) or split queries. 👉 Multiple one-to-many JOINs = red flag. 3️⃣ 𝗝𝗢𝗜𝗡 𝗙𝗘𝗧𝗖𝗛 𝗲𝘅𝗽𝗹𝗼𝘀𝗶𝗼𝗻 (𝗝𝗣𝗔) SELECT o FROM Order o JOIN FETCH o.items JOIN FETCH o.payments 🤕 looks fine in Java 🚑 DISTINCT 🔍 Problem: SQL still does 3×4 = 12 rows. Hibernate deduplicates objects, not SQL work. ✅ Fix: fetch one collection only load others separately / batch 👉 JOIN FETCH hides the explosion, not removes it. 4️⃣ 𝗪𝗿𝗼𝗻𝗴 𝗝𝗢𝗜𝗡 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻 JOIN payments p ON p.user_id = u.id (should be order_id) 🤕 random duplicates 🚑 DISTINCT 🔍 Problem: Wrong relationship → quasi-cartesian result. ✅ Fix: verify FK verify cardinality 👉 DISTINCT can’t fix wrong logic. 🧠 𝗧𝗵𝗲 𝗽𝗮𝘁𝘁𝗲𝗿𝗻 In all cases: you created extra rows then removed them DISTINCT = post-processing, not a fix 💬 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 If DISTINCT is fixing your result, your query is already wrong. Or simpler: You didn’t remove duplicates. You paid the database to create them and clean them up. #backend #java #sql #databases #performance #systemdesign #softwareengineering #jpa

  • No alternative text description for this image

Thank you so much for the information I actually face one of these problems I joined a child table and that lead to many rows instead of one row and when I removed that table the problem was solved

Great post. I’d add that a lot of these issues become far less likely when schema design is treated as a first-class concern from day one: correct PK/FK constraints, uniqueness rules, cardinality modeled properly, and indexes aligned to access patterns. Good constraints won’t replace query review though, and this is a great example of what to look out for.

See more comments

To view or add a comment, sign in

Explore content categories