Tuning Oracle with Hibernate: 12.5s to 362ms

I just wrapped up a performance tuning marathon where we took a routine preview of 68 loan installments from a painful 12.5 seconds down to just 362ms (random test case). The "villain" wasn't a missing index or a lack of RAM—it was the way Hibernate and the Oracle Optimizer were talking to each other. Here’s the breakdown for my fellow devs: 1️⃣ The "Lazy" N+1 Killer: We found that a simple Java .filter() on a list was triggering 68 individual hidden queries. Even if each query takes 100ms, your users are waiting 7 seconds before they see a single byte. Lesson: Always use JOIN FETCH or DTOs for metadata lookups. 2️⃣ When Indexes are ignored: We had the right index, but Oracle stayed stubbornly slow. Why? Complex Window Functions (ROW_NUMBER()) inside Views can sometimes prevent "Predicate Pushdown," forcing the DB to scan millions of rows before filtering for your 68 IDs. 3️⃣ The "Merge" Strategy: The breakthrough came when we stopped asking for the "Penalty Data" separately. By merging the logic into the main fetch as a Correlated Scalar Subquery, we forced the database into a "Nested Loop" lookup. 4️⃣ The Results: ❌ Before: 12,551ms (User walks away for coffee) ✅ After: 362ms (Instant response) Performance tuning isn't just about adding indexes—it's about understanding the "conversation" between your application and your data. #Java #SpringBoot #Oracle #SQL #DatabasePerformance #BackendDevelopment #SoftwareEngineering #Hibernate #developer #Damascus

That 12.5s to 362ms improvement is impressive. The execution plan analysis is usually where the biggest wins hide — a missing index or an implicit type conversion in a JOIN predicate can multiply execution time by orders of magnitude without any obvious clue in the query text itself. Curious: did the bulk of the improvement come from indexing changes, or was there a query rewrite involved (like replacing correlated subqueries with JOINs)? Disclosure: I work on ai2sql.io — a natural language to SQL tool. Performance tuning like this is a great reminder that generating correct SQL is only half the problem; generating efficient SQL is the harder half.

Classic story!🔥 The N+1 problem with Hibernate is a real headache, and many people overlook how Oracle Optimizer can't do Predicate Pushdown when Window Functions are nested inside a View. The Correlated Scalar Subquery is a smart move — it forces the DB into a Nested Loop instead of scanning everything first then filtering. 12.5s → 362ms is a serious win! 💪

Like
Reply

Way to go Batool 🥳

Like
Reply

برافو عليكن .. ولهيك دايما بروح للآلية يلي عم يتم تنفيذ الكويري فيها بالإضافة للـ execution plan لأن هية كتاب أسرار الداتابيس 👏

القروض بأيدي أمينة 👍 و شكراً على المشاركة

See more comments

To view or add a comment, sign in

Explore content categories