简体   繁体   中英

MySQL should I ever use USE INDEX and when?

I came across suggestion that I should never use "USE INDEX" to tell MySQL which index to use. An expert said I should leave the decision to optimizer. That probably means I should just create indexes on columns that are most needed and not worry about it anymore. It would be really good if I can do that and rely on optimizer.

What do you think about that? Any pitfalls?

Yes, you can do that, but the optimizer cant say you that the created indexe are not the best, or one missing or that is better to have a composite index.

You can only check your queries in the logs who using to much time or EXPLAIN yout queries to see which index or not the optimizer is using.

It is a bad idea to create to much index that never use.

if you have more condition in the WHERE clause create a composite index over all the field. the first field in this index is the field which reduce the result most and so on.

You can also use a composite index id you not using all fields.

Lets say you have a index over (field1, field2, field3)

this index can use from left to right

it works

WHERE field1 =...
WHERE field1 = ... AND field2 =...
WHERE field1 = ... AND field2 =... AND field3 =...

but not

WHERE field2 =...
WHERE field3 =...
WHERE field2 = ... AND field3 =...

So sometimes it it better to use a other order so you can use the index before you must create a second one

Is it intresting for you i can post some samples

You typically won't want to force the optimizer to do what you want unless you have proven, as others have said, that a) you already have the correct indexes and b) using another index is actually faster. That being said, there are absolutely great use cases USE INDEX or FORCE INDEX!

One example is warming up the mysql buffer pool with a particular set of data. If you want to load an index into the buffer pool (why you would do this is its own interesting question), you can use a FORCE INDEX clause to force mysql to read from that index.

CREATE TABLE my_table (col int, my_col int, index index_on_my_col (my_col));

SELECT my_col FROM my_table FORCE INDEX (index_on_my_col);

While database optimizers can be very good, they are not perfect. They aren't perfect for a few reasons, such as a) the decisions they make are based on statistics which are approximations, and b) the optimization algorithms themselves (such as volcano or cascades ) are based on heuristics that are not guaranteed to produce the most efficient query plan; they are engineers' (hopefully) very educated guesses that will work for the vast majority of use cases.

Similarly, the majority of people wouldn't take it upon themselves to make tweaks to their car's engine hardware or transmission. They are extremely complex systems and it's possible for a seemingly innocuous changes to have catastrophic results down the road. However, for an expert with a very specific goal in mind it's possible to get great improvements with careful changes and a lot of testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM