简体   繁体   English

Maria DB - 索引帮助

[英]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在 main.another_column 和 lookup.lookup_column 上创建索引是否足够

or index on main.main_column is also required to increase the performance?或 main.main_column 上的索引也需要提高性能?

No, an index on main.main_column isn't needed and wouldn't be used.不, main.main_column 上的索引不是必需的,也不会被使用。

Set all three indexes and then do explain select rest-of-your-query ;设置所有三个索引,然后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.优化器可能会选择反转连接并使用 main.main_column 上的索引,以使记录的顺序正确,但在您的简单情况下,这不应该发生。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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