简体   繁体   中英

Maria DB - Help on Index

I have below query

Select main.display_column, lookup.lookup_column from
main
inner join lookup on main.main_column = lookup.lookup_column
where main.another_column = 123;

Is it enough to create index on main.another_column and lookup.lookup_column

or index on main.main_column is also required to increase the performance?

No, an index on main.main_column isn't needed and wouldn't be used.

Set all three indexes and then do explain select rest-of-your-query ; it will show only the two indexes being used.

If you had something like this:

select main.display_column, lookup.lookup_column
from main
inner join lookup on main.main_column = lookup.lookup_column
where main.another_column >= 123
order by lookup.primarykey

the optimizer might choose to reverse the join and use the index on main.main_column in order to have the records already in correct order, but in your straightforward case that shouldn't happen.

As you asked, these are the optimal Indexes:

main:    INDEX(another_column)   -- takes care of the WHERE
lookup:  INDEX(lookup_column)    -- takes care of the JOIN

Optionally (for possibly a little more speed):

main:  INDEX(another_column, main_column, display_column)  -- "covering"

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