0
0
Expressframework~8 mins

Raw queries when needed in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Raw queries when needed
MEDIUM IMPACT
Using raw queries affects server response time and database query execution speed, impacting how fast data is sent to the client.
Fetching complex data efficiently from the database
Express
await sequelize.query("SELECT users.*, orders.* FROM users JOIN orders ON users.id = orders.user_id WHERE orders.status = 'pending'");
Raw query executes a single optimized SQL statement reducing overhead and speeding up data retrieval.
📈 Performance Gainreduces query time by 30-50% and server blocking time
Fetching complex data efficiently from the database
Express
await User.findAll({ include: [{ model: Order, where: { status: 'pending' } }] });
ORM generates multiple queries or inefficient joins causing slower response and higher CPU usage.
📉 Performance Costblocks server response for 100-200ms on complex joins
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
ORM complex queriesN/AN/AHigher server response delay[X] Bad
Raw SQL queriesN/AN/ALower server response delay[OK] Good
Rendering Pipeline
Raw queries reduce server processing time, allowing faster data delivery to the browser, improving initial content paint.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing due to ORM overhead
Core Web Vital Affected
LCP
Using raw queries affects server response time and database query execution speed, impacting how fast data is sent to the client.
Optimization Tips
1Use raw queries for complex or performance-critical database operations.
2Always sanitize inputs in raw queries to prevent SQL injection.
3Avoid raw queries for simple CRUD operations where ORM is efficient.
Performance Quiz - 3 Questions
Test your performance knowledge
Why might raw SQL queries improve performance over ORM methods?
AThey reduce server CPU usage by avoiding ORM overhead
BThey increase the number of database queries
CThey add extra parsing steps on the server
DThey always cache data automatically
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and inspect the time taken for API calls fetching data.
What to look for: Look for shorter server response times and faster data transfer indicating efficient queries.