Mysql:唯一字段需要索引吗?

时间:2022-09-16 08:34:23

I have one unique field in my table, and I need to search over it quickly. Do I need to index it?

我的表中有一个独特的字段,我需要快速搜索它。我需要索引吗?

Do searches over unique fields and indexed fields vary in speed or resource usage?

对唯一字段和索引字段的搜索在速度或资源使用方面有所不同吗?

3 个解决方案

#1


40  

No, you dont have to index it again. When you specify UNIQUE KEY, the column is indexed. So it has no difference in performance with other indexed column (e.g. PRIMARY KEY) of same type.

不,你不必再次索引它。指定UNIQUE KEY时,将对列进行索引。因此,它与相同类型的其他索引列(例如PRIMARY KEY)的性能没有差别。

However if the type is different, there will be little performance difference.

但是,如果类型不同,则性能差异很小。

#2


7  

Every UNIQUE field is by definition indexed with a UNIQUE INDEX - this also happens to be the fastest searchable access path.

根据定义,每个UNIQUE字段都使用UNIQUE INDEX索引 - 这也恰好是最快的可搜索访问路径。

#3


6  

If the field needs to be UNIQUE then it should either be the PRIMARY KEY or a UNIQUE INDEX.

如果字段需要是UNIQUE,那么它应该是PRIMARY KEY或UNIQUE INDEX。

As for performance between UNIQUE INDEX and INDEX, there is no difference when selecting as both will use the same algorithm i.e. hashing or b-tree. It's just that with a UNIQUE index, especially a numeric i.e. INT one, it will be faster than an index which contains duplicates as algorithms such as b-tree are able to more efficiently get to the requested row(s)

至于UNIQUE INDEX和INDEX之间的性能,选择时没有区别,因为两者都使用相同的算法,即散列或b树。只是使用UNIQUE索引,特别是数字,即INT,它将比包含重复的索引更快,因为诸如b-tree之类的算法能够更有效地到达请求的行。

#1


40  

No, you dont have to index it again. When you specify UNIQUE KEY, the column is indexed. So it has no difference in performance with other indexed column (e.g. PRIMARY KEY) of same type.

不,你不必再次索引它。指定UNIQUE KEY时,将对列进行索引。因此,它与相同类型的其他索引列(例如PRIMARY KEY)的性能没有差别。

However if the type is different, there will be little performance difference.

但是,如果类型不同,则性能差异很小。

#2


7  

Every UNIQUE field is by definition indexed with a UNIQUE INDEX - this also happens to be the fastest searchable access path.

根据定义,每个UNIQUE字段都使用UNIQUE INDEX索引 - 这也恰好是最快的可搜索访问路径。

#3


6  

If the field needs to be UNIQUE then it should either be the PRIMARY KEY or a UNIQUE INDEX.

如果字段需要是UNIQUE,那么它应该是PRIMARY KEY或UNIQUE INDEX。

As for performance between UNIQUE INDEX and INDEX, there is no difference when selecting as both will use the same algorithm i.e. hashing or b-tree. It's just that with a UNIQUE index, especially a numeric i.e. INT one, it will be faster than an index which contains duplicates as algorithms such as b-tree are able to more efficiently get to the requested row(s)

至于UNIQUE INDEX和INDEX之间的性能,选择时没有区别,因为两者都使用相同的算法,即散列或b树。只是使用UNIQUE索引,特别是数字,即INT,它将比包含重复的索引更快,因为诸如b-tree之类的算法能够更有效地到达请求的行。