Course contentsShow
← Databases & SQL
Lesson 1043 of 1,9539. MySQL and MariaDB EcosystemsFree lesson

Forcing Index Usage

Use optimizer hints to override query plans for specific performance tuning tasks.

In this lesson, you will learn how to use optimizer hints to manually override the query planner’s decisions and force the database to use a specific index.

The Optimizer's Dilemma

Under normal circumstances, the query optimizer is remarkably smart. It calculates the "cost" of various access methods (like scanning the entire table versus seeking through an index) and picks the one that should be fastest. However, sometimes the optimizer makes a mistake—perhaps due to outdated statistics or a complex query structure—and chooses a slow full-table scan when a perfectly good index exists.

Forcing the Hand of the Engine

When you have identified a query that is underperforming, you can use Optimizer Hints to tell the engine, "Ignore your calculation; use this specific index instead." In MySQL and MariaDB, we achieve this using the USE INDEX or FORCE INDEX clauses.

While USE INDEX acts as a recommendation to the optimizer, FORCE INDEX acts as a strict instruction. If the index you specify in a FORCE INDEX hint is unusable for the query (for example, if the columns don't match), the database will return an error, highlighting exactly why the index isn't viable.

Consider this example where we have an index named idx_customer_status:

SELECT * 
FROM orders 
FORCE INDEX (idx_customer_status) 
WHERE status = 'pending' AND created_at > '2023-01-01';

When to Use This

You should treat index forcing as a "last resort" diagnostic or performance tuning tool. Because database data distributions change over time, an index that is efficient today might become inefficient tomorrow if the data density shifts.

By forcing an index, you lock the query plan in place. If you upgrade your hardware or your data grows significantly, your hardcoded hint might prevent the optimizer from switching to a more efficient strategy later on. Always test the impact of these hints in a staging environment before pushing them to production.

Key Takeaway: Use FORCE INDEX to override the query optimizer when you have proven that a specific index provides superior performance, but exercise caution as this prevents the optimizer from dynamically adapting to future changes in your data distribution.