0
0
Laravelframework~8 mins

Raw expressions in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Raw expressions
MEDIUM IMPACT
Using raw expressions affects how queries are built and executed, impacting database query performance and server response time.
Including complex SQL conditions in a Laravel query
Laravel
$year = 2023;
$users = DB::table('users')->whereYear('created_at', $year)->get();
Using Laravel's built-in methods allows query builder to optimize and safely bind parameters.
📈 Performance GainEnables query caching and safer execution, reducing server load
Including complex SQL conditions in a Laravel query
Laravel
$users = DB::table('users')->whereRaw('YEAR(created_at) = 2023')->get();
Using raw SQL without bindings can lead to SQL injection risks and may prevent query caching.
📉 Performance CostBlocks query optimization and can increase server processing time
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Raw SQL with whereRawN/AN/AN/A[!] Caution
Laravel query builder methodsN/AN/AN/A[OK] Good
Rendering Pipeline
Raw expressions affect the backend query generation stage before data reaches the frontend, influencing how fast data is retrieved and sent to the browser.
Server Query Execution
Network Transfer
Browser Rendering
⚠️ BottleneckServer Query Execution
Core Web Vital Affected
LCP
Using raw expressions affects how queries are built and executed, impacting database query performance and server response time.
Optimization Tips
1Avoid raw expressions without parameter binding to prevent SQL injection and improve query caching.
2Prefer Laravel query builder methods for safer and more optimized queries.
3Monitor backend query times to ensure fast data delivery and better LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance risk when using raw expressions in Laravel queries?
ASQL injection and lack of query optimization
BIncreased DOM reflows in the browser
CSlower CSS rendering
DHigher JavaScript bundle size
DevTools: Network
How to check: Open DevTools, go to Network tab, filter XHR requests, and inspect the response time of API calls that use raw expressions.
What to look for: Look for slow response times indicating inefficient queries or large payloads affecting LCP.