Share this
Important pre-checks before running pt-online-schema-change to Add/Drop an index in MySQL
by Archita Chauhan on Jul 17, 2025 1:26:41 PM
When you're working with large MySQL tables in production, making schema changes like adding or dropping indexes can be risky if not done carefully or manually using SQL statements. Fortunately, Percona Toolkit’s pt-online-schema-change (pt-osc) provides a safer, non-blocking way to make changes — but even with this powerful tool, preparation is very essential.
Today, I am sharing here some of the most essential pre-checks you should perform before using pt-online-schema-change to modify indexes on a MySQL table.
1. Check table size and row count
Understand the table size to anticipate the duration and performance impact.
- Large tables can take time and may increase load — plan accordingly
- Large tables will take longer to clone and alter, and may spike I/O and CPU
- Size of the table helps you gauge the impact and duration of the schema change
- Size of the table is also important to determine the required free disk space for a successful pt-osc execution
COMMAND TO CHECK TABLE SIZE:
select table_schema as database_name,
table_name,
round( (data_length + index_length) / 1024 / 1024 / 1024, 2) as total_size_gb, round( (data_length) / 1024 / 1024 / 1024, 2) as data_size_gb,
round( (index_length) / 1024 / 1024 / 1024, 2) as index_size_gb
from information_schema.tables
where table_schema = 'DB_NAME'
and table_name = 'TABLE_NAME';
2. Confirm that a primary key or unique index exists on the table
pt-osc needs a PRIMARY KEY or UNIQUE KEY to track and apply row changes. If no primary or unique key exists, the tool will refuse to run unless explicitly forced (--no-check-alter), which is not a recommended practice
COMMAND TO check your table structure and confirm that this exists
SHOW CREATE TABLE TABLE_NAME;
3. Check replication health
Ensure all concerned replicas are up to date and healthy before running changes to the master. Schema changes can cause significant replication lag if not managed carefully.
COMMANDS TO BE EXECUTED ON THE MASTER NODE:
SHOW SLAVE HOSTS;
COMMANDS TO BE EXECUTED ON THE ABOVE LISTED REPLICAS:
SHOW SLAVE STATUS \G
Check and confirm below values:
Seconds_Behind_Master = 0
Slave_IO_Running = YES
Slave_SQL_Running = YES
You can use --check-slave-lag with pt-osc to monitor and throttle changes based on replica lag.
4. Review the index change
Before adding or dropping an index, ensure below things are done:
- Use EXPLAIN to see the current query plans
- Ensure you're not adding duplicate or redundant indexes
- Evaluate the benefit of the change
- Ensure the index you’re adding is necessary and efficient
- Ensure the index you’re dropping is not being used by queries
- Prevent negative performance impacts
Indexes should improve read performance and not add unnecessary overhead. Before adding or dropping an index, run EXPLAIN (or EXPLAIN ANALYZE) on key queries:
1. Check Current Indexes
SHOW INDEX FROM table_name;
SHOW CREATE TABLE table_name;
2. Check query performance before and after using EXPLAIN PLAN
If you're adding or changing an index, test your most important queries with and without the index in a non-production environment:
Run the Query Without the Index
EXPLAIN SELECT * FROM test WHERE id = 1234;
This will output the execution plan, showing how MySQL is accessing the table. If you see values like type: ALL, key: NULL in the output, it means a full table scan is happening and this table needs indexing.
Now add the Index in a non-production environment first and run the same query again with EXPLAIN
EXPLAIN SELECT * FROM test WHERE id = 1234;
Now compare the output. You must see the difference as below
- type (from ALL to ref or range)
- key showing new index
- rows drastically reduced
- Extra maybe show Using index condition
Make sure you avoid these mistakes
- Over-indexing: Too many indexes slow down writes and increase storage
- Redundant indexes: Wastes resources with no performance benefit
- Wrong column order: Multi-column indexes must match query patterns
- Dropping needed indexes: Can kill performance or break query plans
5. Ensure sufficient disk space
When pt-osc is executed, it creates a copy of the table. Make sure you have enough free disk space in:
- Data directory
- Temporary storage
- Binlogs (if replication is on)
You’ll need enough free space for a table copy. The available size in the data partition should be at least twice of the table size.For e.g, if the table size is 50G, make sure there is more than 100G free space available in the data partition when pt-osc is executed.
COMMAND TO CHECK DISK SPACE:
df -h
6. Check for foreign keys
Foreign keys can block or prevent pt-osc from completing if not handled properly.If your table has foreign key constraints, pt-osc must:
- Detect them
- Rebuild the constraints pointing to the new table
- If improperly handled, the operation may fail or leave foreign key constraints broken.
COMMAND to identify any foreign key relationships:
SELECT table_name, column_name, constraint_name,referenced_table_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE referenced_table_name = 'table_name';
COMMAND to check foreign keys defined on your table:
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
FROM information_schema.referential_constraints
WHERE TABLE_NAME = 'table_name';
NOTE: Check these queries for both tables, child tables and referenced tables.
What do you do if foreign keys exist on your concerned table?
If foreign keys are existing, use the --alter-foreign-keys-method flag with one of the following options:
- auto: (default) Automatically determines the safest method
- rebuild_constraints: Drops and recreates foreign keys on child tables
- drop_swap: Rebuilds tables and drops constraints temporarily
- none: Only safe if no foreign keys are involved
What happens if you use pt-osc without handling foreign keys?
- Schema sync failures
- Orphaned child rows
- Broken referential integrity
7. Check for triggers
Since pt-osc creates triggers to keep the shadow table in sync with the original during the change, you must verify:
- The table doesn't already have a conflicting BEFORE/AFTER INSERT, UPDATE, or DELETE trigger
- pt-osc will fail and will not work on tables that already have triggers defined.
- This tool itself adds its own triggers (INSERT, UPDATE, DELETE) on the original table
MySQL does not allow multiple triggers of the same type (e.g., two AFTER INSERT triggers). - So, if your table already has any triggers, pt-osc will refuse to run
COMMANDS to check existing triggers:
SELECT TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_TABLE, ACTION_TIMING, ACTION_STATEMENT, CREATED
FROM information_schema.TRIGGERS
WHERE TRIGGER_SCHEMA = 'your_database_name'
AND EVENT_OBJECT_TABLE = 'table_name';
SHOW TRIGGERS LIKE 'table_name';
SHOW TRIGGERS FROM database_name WHERE Table = 'table_name';
8. Check for replication filters
pt-osc is very cautious when it detects that replication filters are used on the server. This is because:
- If a replica has filters like --replicate-do-db, --replicate-ignore-table, or --binlog-do-db, etc. replication can break or it misses schema changes
So by default, it aborts execution if any such filters are found
COMMAND to check replication filters
SHOW VARIABLES LIKE '%replicate%';
Check below variables:
- replicate_do_db
- replicate_ignore_table
- binlog_do_db
- replicate_wild_do_table
SHOW SLAVE STATUS\G
If any of the above values are non-empty, pt-osc will block the operation by default.
If you understand the risks, you can override this behavior by explicitly passing --no-check-replication-filters as an option in the pt-osc command. This will make pt-osc ignore the presence of replication filters and proceed anyway.
Make sure you can safely override this only if:
- You know the schema change will affect only databases/tables that are replicated
- You understand how your replication filters work
- You’ve tested it in a staging environment
- You're not using partial replication that could skip temporary or renamed tables used by pt-osc
Apart from these, there are some other important considerations to take care of while doing a pt-osc execution
1. Test the changes in test/staging environment
Always test your pt-osc command in a non-production environment before running it in a production environment
2. Set proper load throttling options
Use throttling to avoid overload, set the values as per your system values
For e.g:
--max-load Threads_running=XX
--critical-load Threads_running=XX
3. Use --max-lag in a replicated environment
Add --max-lagoption in your pt-osc command if you are working on a server which has replicas added under it. With this option, pt-online-schema-change monitors the replica lag, and pauses the data copy if any replica exceeds the specified lag in seconds. Hence this prevents the tool from overloading replicas and causing excessive lag.
It checks lag via SHOW SLAVE STATUS If any replica has replication lag > X seconds, pt-osc pauses. This helps to maintain a near-real-time data consistency and avoids breaking or delaying downstream apps that rely on timely replication
4. Take backup or snapshot first
Always take a full back up of your table before altering any production data
5. Do a dry run and check the command in a non-production environment
Double-check your full command using dry-run before doing actual execution. Always test your commands in a staging environment first, before running it directly on a production environment.
pt-online-schema-change \
--alter "ADD INDEX idx_example(created_at)" \
--dry-run \
--user=dbuser --password=dbpass --host=localhost \
--database=mydb --table=mytable
Summary
pt-online-schema-change is a fantastic tool, but it’s only as safe as your preparation. These few checks will help ensure that your schema change doesn't disrupt replication, application traffic or data integrity. It ensures your schema change is safe, non-disruptive, and production-ready.
Whether you're adding a much-needed index or cleaning up old ones, following these steps can save you hours of debugging and stress!
MySQL Database Consulting
Looking to optimize your MySQL use?
Share this
- Technical Track (837)
- Oracle (482)
- Database (208)
- MySQL (140)
- Cloud (128)
- Microsoft SQL Server (124)
- Open Source (86)
- Google Cloud (81)
- Microsoft Azure (67)
- Amazon Web Services (AWS) (60)
- Big Data (51)
- Google Cloud Platform (45)
- Cassandra (43)
- DevOps (38)
- Linux (28)
- Pythian (27)
- Podcasts (25)
- PostgreSQL (25)
- Site Reliability Engineering (23)
- Oracle E-Business Suite (22)
- Performance (22)
- Docker (21)
- DBA (18)
- Oracle Cloud Infrastructure (OCI) (18)
- Security (18)
- MongoDB (17)
- Hadoop (16)
- Amazon RDS (15)
- Automation (15)
- BigQuery (15)
- Ansible (14)
- Exadata (14)
- Oracle Database (14)
- Oracleebs (14)
- Snowflake (14)
- Artificial Intelligence (AI) (13)
- Oracle Exadata (13)
- ASM (12)
- Data (12)
- GenAI (12)
- Kubernetes (12)
- LLM (12)
- Replication (12)
- Advanced Analytics (11)
- Machine Learning (11)
- Cloud Migration (10)
- Datascape Podcast (10)
- Oracle Applications (10)
- Authentication, SSO and MFA (8)
- ChatGPT (8)
- High Availability (8)
- Infrastructure (8)
- Monitoring (8)
- Oracle EBS (8)
- Percona (8)
- Rman (8)
- Apache Cassandra (7)
- Data Governance (7)
- Microsoft Azure SQL Database (7)
- Migration (7)
- Myrocks (7)
- Python (7)
- Series (7)
- AWR (6)
- Analytics (6)
- Apache Beam (6)
- Data Enablement (6)
- Data Guard (6)
- Innodb (6)
- Oracle Enterprise Manager (OEM) (6)
- Orchestrator (6)
- RocksDB (6)
- Serverless (6)
- Airflow (5)
- Azure Synapse Analytics (5)
- Covid-19 (5)
- Data Visualization (5)
- Disaster Recovery (5)
- Generative AI (5)
- Mariadb (5)
- Microsoft (5)
- Performance Tuning (5)
- SAP (5)
- Scala (5)
- Xtrabackup (5)
- Cloud Security (4)
- Cloud Spanner (4)
- CockroachDB (4)
- Data Management (4)
- Data Pipeline (4)
- Data Security (4)
- Data Strategy (4)
- Database Migration (4)
- Databases (4)
- Dataflow (4)
- Google (4)
- Google BigQuery (4)
- Oracle Autonomous Database (Adb) (4)
- Oracle Cloud (4)
- Oracle Enterprise Manager (4)
- Prometheus (4)
- Redhat (4)
- Ssl (4)
- Windows (4)
- Amazon Relational Database Service (Rds) (3)
- Apache Kafka (3)
- Apex (3)
- Aurora (3)
- Cloud Armor (3)
- Cloud Database (3)
- Cloud FinOps (3)
- Cosmos Db (3)
- Data Analytics (3)
- Database Administrator (3)
- Database Monitoring (3)
- Database Performance (3)
- Digital Transformation (3)
- ERP (3)
- Google Chrome (3)
- Google Cloud Sql (3)
- Google Workspace (3)
- Heterogeneous Database Migration (3)
- Oracle Live Sql (3)
- Oracle Rac (3)
- Perl (3)
- Power Bi (3)
- Remote Teams (3)
- Slob (3)
- Tensorflow (3)
- Terraform (3)
- Amazon Data Migration Service (2)
- Amazon Ec2 (2)
- Anisble (2)
- Apache (2)
- Apache Flink (2)
- Apexexport (2)
- Ashdump (2)
- Azure Data Factory (2)
- Business Intelligence (2)
- Cloud Data Fusion (2)
- Cloud Hosting (2)
- Cloud Infrastructure (2)
- Cloud Shell (2)
- Cloud Sql (2)
- Conferences (2)
- Cosmosdb (2)
- Cost Management (2)
- Data Discovery (2)
- Data Integration (2)
- Data Migration (2)
- Data Quality (2)
- Data Streaming (2)
- Data Warehouse (2)
- Database Management (2)
- Database Troubleshooting (2)
- Database Upgrade (2)
- Dataguard (2)
- Dataops (2)
- Enterprise Data Platform (EDP) (2)
- Events (2)
- Fusion Middleware (2)
- Gemini (2)
- Graphite (2)
- Infrastructure As Code (2)
- Innodb Cluster (2)
- Innodb File Structure (2)
- Innodb Group Replication (2)
- Liquibase (2)
- NLP (2)
- Neo4J (2)
- Nosql (2)
- Oracle Data Guard (2)
- Oracle Datase (2)
- Oracle Flashback (2)
- Oracle Forms (2)
- Oracle Installation (2)
- Oracle Io Testing (2)
- Podcast (2)
- Rdbms (2)
- Redshift (2)
- Remote DBA (2)
- Remote Sre (2)
- S3 (2)
- SAP HANA Cloud (2)
- Single Sign-On (2)
- Webinars (2)
- X5 (2)
- AI (1)
- Actifio (1)
- Adop (1)
- Advanced Data Services (1)
- Afd (1)
- Alloydb (1)
- Amazon (1)
- Amazon Aurora Backtrack (1)
- Amazon Efs (1)
- Amazon Redshift (1)
- Amazon S3 (1)
- Amazon Sagemaker (1)
- Amazon Vpc Flow Logs (1)
- Analysis (1)
- Analytical Models (1)
- Anthos (1)
- Apache Nifi (1)
- Application Migration (1)
- Ash (1)
- Asmlib (1)
- Atlas CLI (1)
- Atp (1)
- Autonomous (1)
- Awr Data Mining (1)
- Awr Mining (1)
- Azure Data Lake (1)
- Azure Data Lake Analytics (1)
- Azure Data Lake Store (1)
- Azure Data Migration Service (1)
- Azure OpenAI (1)
- Azure Sql Data Warehouse (1)
- Batches In Cassandra (1)
- Business Insights (1)
- Chown (1)
- Chrome Security (1)
- Cloud Browser (1)
- Cloud Build (1)
- Cloud Consulting (1)
- Cloud Cost Optimization (1)
- Cloud Data Warehouse (1)
- Cloud Database Management (1)
- Cloud Dataproc (1)
- Cloud Foundry (1)
- Cloud Manager (1)
- Cloud Networking (1)
- Cloud SQL Replica (1)
- Cloud Scheduler (1)
- Cloud Services (1)
- Cloud Strategies (1)
- Compliance (1)
- Conversational AI (1)
- Cyber Security (1)
- Data Analysis (1)
- Data Analytics Platform (1)
- Data Box (1)
- Data Classification (1)
- Data Cleansing (1)
- Data Encryption (1)
- Data Engineering (1)
- Data Estate (1)
- Data Insights (1)
- Data Integrity (1)
- Data Leader (1)
- Data Lifecycle Management (1)
- Data Lineage (1)
- Data Masking (1)
- Data Mesh (1)
- Data Migration Assistant (1)
- Data Migration Service (1)
- Data Mining (1)
- Data Modeling (1)
- Data Monetization (1)
- Data Policy (1)
- Data Profiling (1)
- Data Protection (1)
- Data Retention (1)
- Data Safe (1)
- Data Sheets (1)
- Data Summit (1)
- Data Vault (1)
- Data Warehouse Modernization (1)
- Database Consultant (1)
- Database Consulting (1)
- Database Link (1)
- Database Migrations (1)
- Database Modernization (1)
- Database Provisioning (1)
- Database Provisioning Failed (1)
- Database Replication (1)
- Database Schemas (1)
- Databricks (1)
- Datascape 59 (1)
- DeepSeek (1)
- Docker-Composer (1)
- Duet AI (1)
- Edp (1)
- Etl (1)
- Gcp Compute (1)
- Gcp-Spanner (1)
- Global Analytics (1)
- Google Analytics (1)
- Google Cloud Architecture Framework (1)
- Google Cloud Data Services (1)
- Google Cloud Partner (1)
- Google Cloud Spanner (1)
- Google Cloud VMware Engine (1)
- Google Compute Engine (1)
- Google Dataflow (1)
- Google Datalab (1)
- Google Grab And Go (1)
- Graph Algorithms (1)
- Graph Databases (1)
- Graph Inferences (1)
- Graph Theory (1)
- GraphQL (1)
- Health Check (1)
- Healthcheck (1)
- Information (1)
- Infrastructure As A Code (1)
- Innobackupex (1)
- Innodb Concurrency (1)
- Innodb Flush Method (1)
- It Industry (1)
- Kubeflow (1)
- LMSYS Chatbot Arena (1)
- Linux Host Monitoring (1)
- Linux Storage Appliance (1)
- Looker (1)
- MMLU (1)
- Managed Services (1)
- Migrate (1)
- Migration Checklist (1)
- MongoDB Atlas (1)
- MongoDB Compass (1)
- Newsroom (1)
- Nifi (1)
- OPEX (1)
- Odbcs (1)
- Odbs (1)
- On-Premises (1)
- Open Source Database (1)
- Ora-01852 (1)
- Ora-7445 (1)
- Oracle Cursor (1)
- Oracle Database@Google Cloud (1)
- Oracle Exadata Smart Scan (1)
- Oracle Licensing (1)
- Oracle Linux Virtualization Manager (1)
- Oracle Oda (1)
- Oracle Openworld (1)
- Oracle Parallelism (1)
- Oracle RMAN (1)
- Oracle Rdbms (1)
- Oracle Real Application Clusters (1)
- Oracle Reports (1)
- Oracle Security (1)
- Perfomrance (1)
- Performance Schema (1)
- Policy (1)
- Prompt Engineering (1)
- Public Cloud (1)
- Pythian News (1)
- Rdb (1)
- Replication Error (1)
- Retail (1)
- Securing Sql Server (1)
- Serverless Computing (1)
- Sso (1)
- Tenserflow (1)
- Teradata (1)
- Vertex AI (1)
- Videos (1)
- Workspace Security (1)
- Xbstream (1)
- August 2025 (1)
- July 2025 (3)
- June 2025 (1)
- May 2025 (3)
- March 2025 (2)
- February 2025 (1)
- January 2025 (2)
- December 2024 (1)
- October 2024 (2)
- September 2024 (7)
- August 2024 (4)
- July 2024 (2)
- June 2024 (6)
- May 2024 (3)
- April 2024 (2)
- February 2024 (1)
- January 2024 (11)
- December 2023 (10)
- November 2023 (10)
- October 2023 (11)
- September 2023 (8)
- August 2023 (6)
- July 2023 (2)
- June 2023 (13)
- May 2023 (4)
- April 2023 (6)
- March 2023 (10)
- February 2023 (6)
- January 2023 (5)
- December 2022 (10)
- November 2022 (10)
- October 2022 (10)
- September 2022 (13)
- August 2022 (16)
- July 2022 (12)
- June 2022 (13)
- May 2022 (11)
- April 2022 (4)
- March 2022 (5)
- February 2022 (4)
- January 2022 (14)
- December 2021 (16)
- November 2021 (11)
- October 2021 (6)
- September 2021 (11)
- August 2021 (6)
- July 2021 (9)
- June 2021 (4)
- May 2021 (8)
- April 2021 (16)
- March 2021 (16)
- February 2021 (6)
- January 2021 (12)
- December 2020 (12)
- November 2020 (17)
- October 2020 (11)
- September 2020 (10)
- August 2020 (11)
- July 2020 (13)
- June 2020 (6)
- May 2020 (9)
- April 2020 (18)
- March 2020 (21)
- February 2020 (13)
- January 2020 (15)
- December 2019 (10)
- November 2019 (11)
- October 2019 (12)
- September 2019 (16)
- August 2019 (15)
- July 2019 (10)
- June 2019 (16)
- May 2019 (20)
- April 2019 (21)
- March 2019 (14)
- February 2019 (18)
- January 2019 (18)
- December 2018 (5)
- November 2018 (16)
- October 2018 (12)
- September 2018 (20)
- August 2018 (27)
- July 2018 (31)
- June 2018 (34)
- May 2018 (28)
- April 2018 (27)
- March 2018 (17)
- February 2018 (8)
- January 2018 (20)
- December 2017 (14)
- November 2017 (4)
- October 2017 (1)
- September 2017 (3)
- August 2017 (5)
- July 2017 (4)
- June 2017 (2)
- May 2017 (7)
- April 2017 (7)
- March 2017 (8)
- February 2017 (8)
- January 2017 (5)
- December 2016 (3)
- November 2016 (4)
- October 2016 (8)
- September 2016 (9)
- August 2016 (10)
- July 2016 (9)
- June 2016 (8)
- May 2016 (13)
- April 2016 (16)
- March 2016 (13)
- February 2016 (11)
- January 2016 (6)
- December 2015 (11)
- November 2015 (11)
- October 2015 (5)
- September 2015 (16)
- August 2015 (4)
- July 2015 (1)
- June 2015 (3)
- May 2015 (6)
- April 2015 (5)
- March 2015 (5)
- February 2015 (4)
- January 2015 (3)
- December 2014 (7)
- October 2014 (4)
- September 2014 (6)
- August 2014 (6)
- July 2014 (16)
- June 2014 (7)
- May 2014 (6)
- April 2014 (5)
- March 2014 (4)
- February 2014 (10)
- January 2014 (6)
- December 2013 (8)
- November 2013 (12)
- October 2013 (9)
- September 2013 (6)
- August 2013 (7)
- July 2013 (9)
- June 2013 (7)
- May 2013 (7)
- April 2013 (4)
- March 2013 (7)
- February 2013 (4)
- January 2013 (4)
- December 2012 (6)
- November 2012 (8)
- October 2012 (9)
- September 2012 (3)
- August 2012 (5)
- July 2012 (5)
- June 2012 (7)
- May 2012 (11)
- April 2012 (1)
- March 2012 (8)
- February 2012 (1)
- January 2012 (6)
- December 2011 (8)
- November 2011 (5)
- October 2011 (9)
- September 2011 (6)
- August 2011 (4)
- July 2011 (1)
- June 2011 (1)
- May 2011 (5)
- April 2011 (2)
- February 2011 (2)
- January 2011 (2)
- December 2010 (1)
- November 2010 (7)
- October 2010 (3)
- September 2010 (8)
- August 2010 (2)
- July 2010 (4)
- June 2010 (7)
- May 2010 (2)
- April 2010 (1)
- March 2010 (3)
- February 2010 (3)
- January 2010 (2)
- November 2009 (6)
- October 2009 (6)
- August 2009 (3)
- July 2009 (3)
- June 2009 (3)
- May 2009 (2)
- April 2009 (8)
- March 2009 (6)
- February 2009 (4)
- January 2009 (3)
- November 2008 (3)
- October 2008 (7)
- September 2008 (6)
- August 2008 (9)
- July 2008 (9)
- June 2008 (9)
- May 2008 (9)
- April 2008 (8)
- March 2008 (4)
- February 2008 (3)
- January 2008 (3)
- December 2007 (2)
- November 2007 (7)
- October 2007 (1)
- August 2007 (4)
- July 2007 (3)
- June 2007 (8)
- May 2007 (4)
- April 2007 (2)
- March 2007 (2)
- February 2007 (5)
- January 2007 (8)
- December 2006 (1)
- November 2006 (3)
- October 2006 (4)
- September 2006 (3)
- July 2006 (1)
- May 2006 (2)
- April 2006 (1)
- July 2005 (1)
No Comments Yet
Let us know what you think