0
0
Ruby on Railsframework~8 mins

Changing column types in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Changing column types
MEDIUM IMPACT
This affects page load speed indirectly by impacting database query performance and server response time, which can delay rendering of data-driven pages.
Changing a database column type in a Rails migration
Ruby on Rails
class ChangeColumnTypeGood < ActiveRecord::Migration[7.0]
  def up
    add_column :users, :age_new, :string
    execute('UPDATE users SET age_new = age::text')
    remove_column :users, :age
    rename_column :users, :age_new, :age
  end

  def down
    # reverse steps
  end
end
This approach avoids locking the entire table by adding a new column and migrating data, reducing downtime.
📈 Performance Gainreduces blocking time to seconds, allowing faster page responses and better LCP
Changing a database column type in a Rails migration
Ruby on Rails
class ChangeColumnTypeBad < ActiveRecord::Migration[7.0]
  def change
    change_column :users, :age, :string
  end
end
This runs a blocking operation that locks the entire table during migration, causing downtime and slow queries until complete.
📉 Performance Costblocks database writes for several seconds to minutes depending on table size, delaying page responses
Performance Comparison
PatternDatabase LockingQuery SpeedUser ImpactVerdict
Direct change_columnFull table lockSlow during migrationPage load delays, downtime[X] Bad
Add new column + backfillMinimal lockingFast queries after migrationMinimal downtime, faster page loads[OK] Good
Rendering Pipeline
Changing column types affects the backend database layer, which impacts how fast data queries return results. Slow queries delay server response, which delays browser receiving HTML and rendering content.
Server Processing
Network Transfer
Rendering
⚠️ BottleneckServer Processing due to database locks and slow queries
Core Web Vital Affected
LCP
This affects page load speed indirectly by impacting database query performance and server response time, which can delay rendering of data-driven pages.
Optimization Tips
1Avoid direct column type changes on large tables to prevent long database locks.
2Use add-column and backfill strategy to minimize downtime and speed up queries.
3Monitor server response times during migrations to ensure good user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk when changing a column type directly in a large Rails database table?
AThe browser will re-render the page multiple times
BThe table gets locked, causing slow queries and delayed page loads
CCSS styles will be recalculated unnecessarily
DJavaScript bundle size increases significantly
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page and observe server response times for data requests.
What to look for: Look for long waiting (TTFB) times indicating slow server response due to database migration impact.