Mysql very slow for alter table query
⚡TLDR
Supercharge MySQL ALTER TABLE operations with:
ALGORITHM=INPLACE
: Decreases locking, sidesteps full table copy.LOCK=NONE
: Grants other sessions table access during alteration.- Index Management: Kick out obsolete indexes before, bring in essential ones after.
- Chunky Operations: For behemoth tables, tweak in chunks to cut downtime.
Always trial run to ensure snappy execution and data safety!
Scrutinizing slow ALTER TABLEs
MySQL's ALTER TABLE
can sometimes be slower than a turtle wading in molasses due to reasons such as:
- Staggering table size: With a sea of rows, the database must scribble tons of data.
- Resource scarcity: Limited memory or I/O power can throttle performance.
- Index infestation: Extra indexes balloon the quantity of data to process.
- Simultaneous operations: Other operations on the same table can add to locking woes and time taken.
Enhancer hacks for ALTER TABLE
Remove the clutter, streamline, optimize
Before hitting a large alteration, it's prudent to tidy up:
- Trash Unused Indexes: Lighten the load by purging unused indexes.
- Merge Indexes: Combine similar indexes when possible to trim numbers.
High tech tools for low disruption
pt-online-schema-change
: A lifesaver from the Percona Toolkit that lets you alter tables with negligible production impact.gh-ost
: A handy tool from GitHub's desk that allows for online schema changes with minimal load, even during high traffic.lhm
: The Large Hadron Migrator provides a virtual stage for large-scale database alterations, allowing changes on the fly.
Crafty tactics during alteration
- Normalize Your Data: Simplify your data model to shrink table complexity and size.
- Foreign Key Gymnastics: Temporarily disable
foreign_key_checks
to speed up foreign key maneuvers. - Unique Index Trickery: Similar to above, turning off
unique_checks
can hasten the rebuild of unique indexes.
Quick fix tips: reducing downtime
A dose of phased approach
A step-wise methodology with pt-online-schema-change
:
- Create: Forge a new table with the desired structure.
- Sync: Gradually copy data in sync with live table.
- Copy: Complete data transfer to the new structure.
- Swap: Juggle table names in a flash to cut downtime.
Smooth migration sans locks
When you can't afford even the slightest downtime:
- Bundle alterations into a single operation.
- mysqldump: Resort to create a new table and import data after alterations.
- Filesystem copy: Practice runs can save the day by revealing time expectations.
Watch out for potential drawbacks
Certain data types and table engines:
- Text/BLOB columns: Their storage nature can create a roadblock in alters process.
- MyISAM Engine: Unlike InnoDB, MyISAM is "allergic" to concurrent alterations due to absence of row-level locking.
Scheduled maintenance windows - best practices
During off-peak hours, remember to:
- Schedule: Arrange operations for off-peak hours.
- Announce: Alert users for possible (yet minimized) disruptions.
- Monitor: Keep an eye on the process and system load using monitoring tools.
Linked
Linked
Was this article helpful?