Junior — Middle
How to extend the structure of a heavily used database by adding a new column?
sobes.tech AI
Answer from AI
To add a new column to a heavily used database, you need to act carefully to avoid disrupting the application and causing downtime.
Main steps:
- Planning changes — determine the type and properties of the new column, considering the impact on performance.
- Adding a column with NULL or default value — this is a minimally blocking operation in most DBMSs.
- Gradually updating data — if you need to fill the column with values, do it in batches to avoid overloading the database.
- Updating application code — prepare to handle the new column, but initially maintain compatibility with missing data.
- Testing on a test environment — verify that everything works correctly.
Example SQL for adding a column in MySQL:
ALTER TABLE users ADD COLUMN last_login DATETIME DEFAULT NULL;
If the database is very large and the operation locks the table, you can use online migration tools, such as pt-online-schema-change for MySQL, which allow changing the structure without long locks.