0
0
Spring Bootframework~8 mins

Native SQL queries in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Native SQL queries
MEDIUM IMPACT
Native SQL queries impact server response time and data fetching speed, which affects how fast the page can start rendering.
Fetching data from the database for a web page
Spring Boot
String sql = "SELECT id, name, email FROM users WHERE active = true LIMIT 100";
List<User> users = entityManager.createNativeQuery(sql, User.class).getResultList();
Limits data to needed columns and rows, reducing data size and query time.
📈 Performance GainReduces server response time by 50% or more, improving LCP
Fetching data from the database for a web page
Spring Boot
String sql = "SELECT * FROM users";
List<User> users = entityManager.createNativeQuery(sql, User.class).getResultList();
Fetching all columns and rows without filtering causes large data transfer and slow response.
📉 Performance CostBlocks server response for longer, increasing LCP by hundreds of milliseconds on large tables
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Fetching all data without filtersN/A (server-side)N/ADelays initial paint[X] Bad
Fetching filtered, limited dataN/A (server-side)N/ASpeeds up initial paint[OK] Good
Rendering Pipeline
Native SQL queries run on the server before the browser rendering starts. Faster queries mean faster data delivery, enabling quicker HTML generation and browser painting.
Server Data Fetching
HTML Generation
Browser Rendering
⚠️ BottleneckServer Data Fetching
Core Web Vital Affected
LCP
Native SQL queries impact server response time and data fetching speed, which affects how fast the page can start rendering.
Optimization Tips
1Always filter and limit data in native SQL queries to reduce server response time.
2Avoid selecting unnecessary columns or rows to minimize data transfer.
3Use database indexes to speed up query execution and reduce delays.
Performance Quiz - 3 Questions
Test your performance knowledge
How does limiting columns and rows in a native SQL query affect page load?
AIt causes more reflows in the browser.
BIt increases the number of DOM nodes.
CIt reduces server response time, improving LCP.
DIt has no effect on performance.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and inspect the API or page request timing.
What to look for: Look at the Time to First Byte (TTFB) and total response time to see if server queries delay loading.