Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL : FORCE INDEX vs USE INDEX

Tags:

I'm recently working on index optimization for MySQL table, i noticed that the
FORCE INDEX and USE INDEX almost serve same functionality, I would like to ask what is their different?

like image 533
Teddybugs Avatar asked Dec 27 '13 08:12

Teddybugs


People also ask

What is force index in MySQL?

The FORCE INDEX hint acts like USE INDEX ( index_list ) , with the addition that a table scan is assumed to be very expensive. In other words, a table scan is used only if there is no way to use one of the named indexes to find rows in the table.

Does MySQL like use index?

MySQL also uses indexes for LIKE comparisons if the argument to LIKE is a constant string that doesn't start with a wildcard character.

How do you force an index?

How the Force Index Works. The force index is calculated by subtracting yesterday's close from today's close and multiplying the result by today's volume. If closing prices are higher today than yesterday, the force is positive. If closing prices are lower than yesterday's, the force is negative.

How do I force an index in Mariadb?

FORCE INDEX works by only considering the given indexes (like with USE_INDEX) but in addition it tells the optimizer to regard a table scan as something very expensive. However if none of the 'forced' indexes can be used, then a table scan will be used anyway.


1 Answers

Post my above comments as an answer:

If you use USE INDEX then you RECOMMEND optimizer to use this index, but it can use a table scan if optimizer thinks it will be faster. If you use FORCE INDEX then you MAKE optimizer to use this index even if it thinks a table scan is more efficient. Optimizer will use a table scan only if there is no way to use index to find rows.

Index Hint Syntax:

You can also use FORCE INDEX, which acts like USE INDEX (index_list) but with the addition that a table scan is assumed to be very expensive. In other words, a table scan is used only if there is no way to use one of the given indexes to find rows in the table.

like image 62
valex Avatar answered Sep 20 '22 13:09

valex