Database indexes play an important role in improving query performance. They help MySQL find records faster, reduce unnecessary table scans, and improve the execution of frequently used queries. However, every additional index also requires storage and increases the work performed during INSERT, UPDATE, and DELETE operations.
As an application grows, developers often create new indexes to improve query performance. Over time, some indexes may become unnecessary because query patterns change, new composite indexes are introduced, or application features are removed.
Removing an index directly from a production database can be risky. An index that appears unused during normal testing might still support an important report, background job, or rarely executed query. Dropping it could cause unexpected performance problems.
MySQL provides a feature called invisible indexes to address this challenge. It allows developers to hide an index from the query optimizer without physically dropping it. Developers can then observe how the database behaves without that index and restore its visibility if performance becomes worse.
This article explains how invisible indexes work, how to test index changes safely, how to compare query execution plans, and how to use invisible indexes as part of a production database optimization strategy.
What Are Invisible Indexes in MySQL?
An invisible index is an index that exists in the database but is ignored by the MySQL query optimizer when it selects an execution plan.
Normally, MySQL considers available visible indexes when optimizing queries. When an index becomes invisible, the optimizer stops using it by default.
However, the index is not physically deleted. MySQL continues maintaining the index whenever the underlying table data changes.
For example, consider an orders table with the following indexes:
CREATE TABLE Orders
(
OrderId BIGINT PRIMARY KEY,
CustomerId BIGINT NOT NULL,
OrderDate DATETIME NOT NULL,
Status VARCHAR(30) NOT NULL,
TotalAmount DECIMAL(12, 2) NOT NULL,
INDEX idx_customer_id (CustomerId),
INDEX idx_order_date (OrderDate),
INDEX idx_status (Status)
);
All three secondary indexes are visible by default.
If developers suspect that idx_status is no longer useful, they can make it invisible:
ALTER TABLE Orders
ALTER INDEX idx_status INVISIBLE;
The index remains in the database, but the optimizer no longer considers it for normal query execution plans.
If testing shows that the index is still required, it can be made visible again:
ALTER TABLE Orders
ALTER INDEX idx_status VISIBLE;
This makes invisible indexes useful for evaluating index removal without immediately performing a destructive operation.
How Invisible Indexes Work
Invisible indexes change the optimizer's behavior, not the physical existence of an index.
When an index is visible, MySQL can consider it when optimizing queries. When it is invisible, the optimizer ignores it unless invisible-index usage is explicitly enabled.
The following table explains the difference:
Feature | Visible index | Invisible index |
|---|
Stored in the database | Yes | Yes |
Maintained during INSERT | Yes | Yes |
Maintained during UPDATE | Yes | Yes |
Maintained during DELETE | Yes | Yes |
Considered by optimizer by default | Yes | No |
Can be restored without rebuilding the index | Not applicable | Yes |
Uses storage space | Yes | Yes |
Enforces unique constraints | Yes | Yes |
One important detail is that making an index invisible does not eliminate its maintenance overhead. MySQL still updates the index when table records change.
Therefore, invisible indexes are useful for testing query performance and optimizer behavior, but they do not immediately reduce index storage or write overhead.
Check Existing Indexes Before Testing
Before changing index visibility, inspect the indexes available on the target table.
Use the SHOW INDEX statement:
SHOW INDEX FROM Orders;
The output includes information such as:
You can also retrieve visibility information through the Information Schema:
SELECT
INDEX_NAME,
COLUMN_NAME,
NON_UNIQUE,
IS_VISIBLE
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'Orders';
The IS_VISIBLE column indicates whether the index is available to the optimizer.
A result may look like this:
INDEX_NAME | COLUMN_NAME | NON_UNIQUE | IS_VISIBLE |
|---|
PRIMARY | OrderId | 0 | YES |
idx_customer_id | CustomerId | 1 | YES |
idx_order_date | OrderDate | 1 | YES |
idx_status | Status | 1 | NO |
This confirms that idx_status still exists but is currently invisible.
Checking existing indexes before making changes helps prevent accidental modifications to important database structures.
Create an Invisible Index in MySQL
MySQL allows developers to create an index as invisible from the beginning.
For example, suppose a reporting requirement introduces a new index on the order status and order date.
Instead of making the index immediately available to the optimizer, create it as invisible:
CREATE INDEX idx_status_order_date
ON Orders (Status, OrderDate)
INVISIBLE;
The index is created and maintained, but the optimizer ignores it by default.
This approach is useful when a team wants to prepare an index before testing it with selected queries.
You can also create an invisible index while defining a table:
CREATE TABLE OrderReports
(
ReportId BIGINT PRIMARY KEY,
CustomerId BIGINT NOT NULL,
ReportDate DATE NOT NULL,
INDEX idx_customer_report_date
(
CustomerId,
ReportDate
) INVISIBLE
);
By default, indexes are visible unless the INVISIBLE keyword is specified.
Test the Impact of Removing an Existing Index
The most common use of invisible indexes is testing whether an existing index can be removed safely.
Let's use the Orders table to understand this process.
Assume the application frequently executes the following query:
SELECT
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
Before changing the index, check the execution plan:
EXPLAIN
SELECT
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
If idx_status is useful for this query, the execution plan may show that MySQL considers or uses it.
The actual plan depends on table size, data distribution, statistics, and the estimated cost of using the index.
Make the index invisible
Now hide the index:
ALTER TABLE Orders
ALTER INDEX idx_status INVISIBLE;
Execute the same query again:
EXPLAIN
SELECT
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
Because the index is invisible, the optimizer no longer considers it by default.
The execution plan may now show a full table scan or use another available index.
Compare the two execution plans to understand how the optimizer's decision changed.
For example:
Measurement | Before | After |
|---|
Index visibility | Visible | Invisible |
Optimizer can consider idx_status | Yes | No |
Possible access method | Index lookup | Table scan or another index |
Query execution cost | Depends on workload | May increase or decrease |
Do not assume that a full table scan is always worse. For queries that return a large percentage of table rows, scanning the table can sometimes be more efficient than using a secondary index.
The objective is to evaluate actual workload performance rather than simply count how many indexes a query uses.
Compare Execution Plans With Invisible Indexes
MySQL provides the use_invisible_indexes optimizer switch.
By default, this setting is disabled:
SELECT @@optimizer_switch;
The output contains a setting similar to:
use_invisible_indexes=off
When it is disabled, invisible indexes are ignored by the optimizer.
Developers can temporarily enable invisible indexes for a single query using the SET_VAR optimizer hint.
For example:
EXPLAIN
SELECT /*+ SET_VAR(
optimizer_switch = 'use_invisible_indexes=on'
) */
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
This allows MySQL to consider invisible indexes when constructing the execution plan for that query.
You can compare two plans:
-- Normal optimizer behavior
EXPLAIN
SELECT *
FROM Orders
WHERE Status = 'Completed';
-- Include invisible indexes
EXPLAIN
SELECT /*+ SET_VAR(
optimizer_switch = 'use_invisible_indexes=on'
) */
*
FROM Orders
WHERE Status = 'Completed';
The first query ignores invisible indexes. The second allows the optimizer to consider them.
This is useful when testing a newly created invisible index before making it visible to normal application queries.
The optimizer switch changes the indexes considered during plan construction. It does not change the index's visibility status.
Measure Query Performance With EXPLAIN ANALYZE
An execution plan shows how MySQL intends to execute a query. However, estimated execution costs do not always represent actual workload performance.
MySQL supports EXPLAIN ANALYZE, which executes the query and reports actual execution information.
For example:
EXPLAIN ANALYZE
SELECT
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
The output includes information such as:
Run the query before and after making the index invisible, using the same data and comparable conditions.
You can also use EXPLAIN ANALYZE with the invisible-index optimizer hint to test a candidate index:
EXPLAIN ANALYZE
SELECT /*+ SET_VAR(
optimizer_switch = 'use_invisible_indexes=on'
) */
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
Remember that EXPLAIN ANALYZE executes the statement. Use it carefully with expensive queries, production data, and statements that modify records.
For production testing, begin with representative read-only queries and a controlled test environment.
Test a New Composite Index Before Making It Visible
Invisible indexes are also useful when evaluating new index designs.
Suppose an application frequently retrieves completed orders for a specific customer within a date range:
SELECT
OrderId,
OrderDate,
TotalAmount
FROM Orders
WHERE CustomerId = 101
AND Status = 'Completed'
AND OrderDate >= '2026-01-01'
ORDER BY OrderDate DESC;
The current database may have separate indexes on CustomerId, Status, and OrderDate.
A composite index may improve this query:
CREATE INDEX idx_customer_status_date
ON Orders
(
CustomerId,
Status,
OrderDate
)
INVISIBLE;
Now test the query using the invisible-index optimizer hint:
EXPLAIN
SELECT /*+ SET_VAR(
optimizer_switch = 'use_invisible_indexes=on'
) */
OrderId,
OrderDate,
TotalAmount
FROM Orders
WHERE CustomerId = 101
AND Status = 'Completed'
AND OrderDate >= '2026-01-01'
ORDER BY OrderDate DESC;
Compare the plan with the normal optimizer behavior.
If the new index provides a better execution plan, evaluate its actual performance with representative data and queries before making it visible.
ALTER TABLE Orders
ALTER INDEX idx_customer_status_date VISIBLE;
The column order in a composite index matters. MySQL can use the leading columns of an index for different query patterns, but the effectiveness depends on filtering conditions, sorting requirements, selectivity, and the optimizer's cost estimates.
An index that improves one query may not improve every query that uses the same table.
Monitor Workload Changes After Hiding an Index
Testing a single query is not enough to determine whether an index is safe to remove.
Production applications usually execute many queries through APIs, background services, scheduled jobs, and reporting systems.
An index that appears unnecessary in one endpoint may still be important for another part of the application.
After making an index invisible, monitor the database for changes in workload behavior.
Useful monitoring sources include:
Performance Schema statement statistics
Slow query logs
Query execution plans
Database CPU utilization
Disk I/O activity
Application response times
Query latency and error rates
For example, you can inspect statement performance summaries:
SELECT
DIGEST_TEXT,
COUNT_STAR,
SUM_TIMER_WAIT,
AVG_TIMER_WAIT,
SUM_ROWS_EXAMINED
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;
This query helps identify statements consuming significant execution time.
Compare the results before and after changing index visibility. Look for queries that begin examining more rows or taking longer to execute.
MySQL documentation also identifies changes in execution plans, slow query log entries, and Performance Schema workload information as useful ways to detect the impact of invisible indexes.
For a production system, monitor through a representative business cycle. A short test may not include month-end reports, scheduled processing, or less frequently used application features.
Restore an Index When Performance Degrades
If testing shows that hiding an index causes a performance problem, restore its visibility.
ALTER TABLE Orders
ALTER INDEX idx_status VISIBLE;
The index becomes available to the optimizer again.
You do not need to recreate it because the index was never physically removed.
After restoring visibility, check the execution plan again:
EXPLAIN
SELECT
OrderId,
CustomerId,
OrderDate,
Status,
TotalAmount
FROM Orders
WHERE Status = 'Completed';
This provides a quick recovery path when an index turns out to be necessary.
Once the index has been tested thoroughly and the team is confident it is no longer required, it can be dropped:
DROP INDEX idx_status ON Orders;
Dropping the index permanently removes it and its associated storage.
Before doing so, review the full workload, confirm that the index is not required by other queries, and follow the organization's database change approval process.
Important Limitations of Invisible Indexes
Invisible indexes are useful, but they have some important limitations.
Primary key indexes cannot be invisible
MySQL does not allow primary key indexes to be made invisible.
For example:
ALTER TABLE Orders
ALTER INDEX PRIMARY INVISIBLE;
This operation fails because a primary key index cannot be invisible.
There is also a related limitation involving unique indexes. If a unique index on NOT NULL columns acts as the table's implicit primary key because no explicit primary key exists, MySQL does not allow that index to become invisible.
If the table has an explicit primary key, other unique indexes may be eligible for visibility changes.
Unique constraints remain enforced
Making a unique index invisible does not disable its uniqueness constraint.
For example:
CREATE UNIQUE INDEX ux_customer_email
ON Customers (Email);
If this index is made invisible:
ALTER TABLE Customers
ALTER INDEX ux_customer_email INVISIBLE;
MySQL still prevents duplicate values from being inserted.
This is important because invisible indexes affect optimizer visibility, not data integrity rules.
Invisible indexes still consume storage
An invisible index remains physically present.
It continues consuming disk space and requires maintenance during data modifications.
Therefore, hiding an index does not immediately reduce storage consumption or improve write performance.
Visibility changes are not instant DDL
Changing an index from visible to invisible or back is generally a fast, in-place operation, but it should not be treated as an instant DDL operation.
Do not assume that specifying ALGORITHM=INSTANT will work for index visibility changes. Check the supported DDL behavior for your MySQL version and storage engine.
Index hints can be affected
Queries that explicitly reference an invisible index through index hints can encounter errors because the hinted index is unavailable to normal optimizer processing.
Review application SQL that uses USE INDEX, FORCE INDEX, or IGNORE INDEX before changing index visibility.
Common Mistakes to Avoid
Mistake | Why it causes problems | Recommended approach |
|---|
Dropping an index without testing | Important queries may become slower | Make it invisible first |
Testing only one query | Other application queries may depend on the index | Monitor representative workloads |
Assuming invisible means deleted | The index still consumes storage | Drop it after testing if removal is approved |
Ignoring write operations | The invisible index is still maintained | Evaluate the actual storage and write requirements |
Comparing plans with different data | Results may not represent the same workload | Use consistent test data and conditions |
Assuming a full table scan is always bad | Some queries are more efficient without an index | Compare actual execution metrics |
Forgetting index hints | Explicit references may fail when the index is invisible | Review application SQL before changing visibility |
Ignoring unique constraints | Invisible unique indexes still enforce uniqueness | Preserve data integrity expectations |
Testing for too short a period | Infrequent queries may be missed | Monitor through a representative workload cycle |
Best Practices for Testing MySQL Index Changes
Review existing indexes and their visibility before making changes.
Identify the queries that may depend on the index.
Capture baseline execution plans and performance measurements.
Make one index invisible at a time to isolate its impact.
Use EXPLAIN to compare optimizer decisions.
Use EXPLAIN ANALYZE carefully to measure actual query execution.
Test new indexes as invisible before enabling them for normal optimizer use.
Monitor Performance Schema, slow query logs, CPU usage, and application response times.
Include scheduled jobs and reporting queries in the test workload.
Restore visibility immediately if important queries become slower.
Drop the index only after sufficient testing and approval.
Test index changes in staging before applying them to production.
For larger systems, consider using a documented index change process. Record the index name, affected queries, baseline metrics, test duration, observed results, and final decision.
This makes database optimization more predictable and easier to review.
Advantages and Disadvantages of Invisible Indexes
Advantages | Disadvantages |
|---|
Test index removal without dropping the index | Does not reduce index storage |
Restore visibility without rebuilding the index | Invisible indexes still have write-maintenance costs |
Compare optimizer execution plans | Does not automatically identify every query that depends on an index |
Test new indexes before enabling them | Requires workload monitoring and careful analysis |
Helps reduce risk during index cleanup | Primary key indexes cannot be invisible |
Supports controlled database optimization | Explicit index hints may require changes |
Conclusion
MySQL invisible indexes provide a practical way to test index changes without immediately removing database structures.
Developers can make an existing index invisible, observe how the query optimizer behaves, compare execution plans, and monitor application performance. If the index is still required, it can be made visible again without rebuilding it.
The feature is also useful when evaluating new composite indexes. Developers can create an index as invisible, test it with the use_invisible_indexes optimizer setting, and make it visible only after validating the results.
However, invisible indexes are not the same as dropped indexes. They continue consuming storage, participating in data maintenance, and enforcing unique constraints.
For production database optimization, use invisible indexes as part of a controlled testing process. Combine execution-plan analysis with real workload monitoring before permanently removing an index. This helps reduce unnecessary indexes while protecting application performance.
Join the conversation! Your thoughts help the community grow.