0
0
Ruby on Railsframework~8 mins

Select and pluck in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Select and pluck
MEDIUM IMPACT
This affects how efficiently data is fetched from the database and how much data is sent to the app, impacting page load speed and server response time.
Fetching specific columns from a database table
Ruby on Rails
User.pluck(:name)
Pluck fetches only the requested column directly from the database as an array, reducing memory and processing.
📈 Performance GainReduces data transfer size and CPU usage, improving response time and LCP.
Fetching specific columns from a database table
Ruby on Rails
User.select(:id, :name).map(&:name)
Select loads full ActiveRecord objects with selected attributes, then map extracts data, causing extra memory and processing.
📉 Performance CostAdds unnecessary object instantiation and memory use, increasing server response time.
Performance Comparison
PatternData FetchedServer ProcessingNetwork PayloadVerdict
select + mapAR objects with selected columnsHigh (object creation)Large[X] Bad
pluckOnly requested columnsLow (direct array)Small[OK] Good
Rendering Pipeline
Data fetching with select loads full objects causing more server processing and larger payloads, delaying response. Pluck fetches minimal data, speeding up server response and page load.
Server Processing
Network Transfer
Rendering
⚠️ BottleneckServer Processing and Network Transfer
Core Web Vital Affected
LCP
This affects how efficiently data is fetched from the database and how much data is sent to the app, impacting page load speed and server response time.
Optimization Tips
1Use pluck to fetch only needed columns to reduce data size.
2Avoid select with map for simple column extraction to save memory and CPU.
3Smaller data payloads improve server response and page load speed.
Performance Quiz - 3 Questions
Test your performance knowledge
Which method fetches only specific columns directly from the database, reducing data load?
Aselect + map
Bpluck
Call
Dfind
DevTools: Network
How to check: Open DevTools, go to Network tab, filter for XHR or fetch requests, inspect the response payload size and content for database queries.
What to look for: Smaller payload size and faster response times indicate efficient data fetching with pluck.