0
0
Ruby on Railsframework~8 mins

Adding and removing columns in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Adding and removing columns
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by changing database schema, which can impact server response time and frontend rendering if migrations block requests.
Updating database schema by adding or removing columns
Ruby on Rails
class AddBioToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :bio, :text
  end
end

class RemoveNicknameFromUsers < ActiveRecord::Migration[7.0]
  def change
    remove_column :users, :nickname
  end
end
Splitting add and remove operations into separate migrations reduces lock time and allows safer, incremental schema changes.
📈 Performance Gainreduces table lock duration, improving database availability and lowering page load delays
Updating database schema by adding or removing columns
Ruby on Rails
class AddDetailsToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :bio, :text
    remove_column :users, :nickname
  end
end
This runs add_column and remove_column in a single migration which can lock the table and block queries during migration, causing slow page loads or downtime.
📉 Performance Costblocks database writes during migration, increasing page load time and causing potential request timeouts
Performance Comparison
PatternDatabase LockingMigration DurationPage Load ImpactVerdict
Single migration with add and remove columnsLong table lockLonger migration timeBlocks page load, increases LCP[X] Bad
Separate migrations for add and remove columnsShorter locks per migrationShorter individual migrationsImproves server responsiveness, lowers LCP[OK] Good
Rendering Pipeline
Schema changes affect server response time which impacts how fast the browser receives data to render. Long migrations can delay server responses, slowing Largest Contentful Paint (LCP).
Server Processing
Network Transfer
Rendering
⚠️ BottleneckServer Processing during migration locks
Core Web Vital Affected
LCP
This affects page load speed and interaction responsiveness by changing database schema, which can impact server response time and frontend rendering if migrations block requests.
Optimization Tips
1Avoid combining add and remove column operations in one migration to reduce table lock time.
2Run migrations during low traffic periods to minimize user impact.
3Split large schema changes into smaller, incremental migrations for better server responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a performance risk when adding and removing columns in a single migration?
AReduced bundle size
BFaster page rendering in browser
CLong database table locks blocking queries
DImproved CSS selector performance
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page during migration, and observe server response times and delays.
What to look for: Look for slow or stalled server responses indicating backend migration blocking requests.