The query was slow, and the logs showed why: every scan across the table touched more data than it needed. The fix was straightforward—add a new column.
Creating a new column in a production database is simple at a glance, but the details matter. Schema changes can lock tables, block writes, or cause migrations to fail if not planned. Choose the right data type from the start to avoid rework. Use defaults only when necessary, and beware of backfilling large datasets without batching.
In PostgreSQL, ALTER TABLE table_name ADD COLUMN column_name data_type; is direct and reliable. In MySQL, it’s similar: ALTER TABLE table_name ADD COLUMN column_name data_type;. For large datasets, schedule downtime or use tools like pt-online-schema-change for zero-downtime execution.
If the new column will store computed data, consider whether it should be generated on write or on read. Generated columns can save compute time for repetitive queries but may increase write cost. Indexed columns speed lookups but can slow inserts, so measure tradeoffs before committing.