MySQL:唯一字段需要是索引吗?
我的表中有一个 UNIQUE
字段,我需要快速搜索它。我需要索引它吗?
对唯一字段和索引字段的搜索速度或资源使用情况是否有所不同?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不必再次为其编制索引。当您指定
UNIQUE KEY
时,该列将被索引。因此它与相同类型的其他索引列(例如PRIMARY KEY)在性能上没有差异。但如果类型不同,性能差异很小。
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.However if the type is different, there will be little performance difference.
根据定义,每个
UNIQUE
字段都使用UNIQUE INDEX
进行索引 - 这也恰好是最快的可搜索访问路径。Every
UNIQUE
field is by definition indexed with aUNIQUE INDEX
- this also happens to be the fastest searchable access path.如果字段需要是唯一的,那么它应该是
PRIMARY KEY
或UNIQUE INDEX
。至于
UNIQUE INDEX
和INDEX
之间的性能,选择时没有区别,因为两者都使用相同的算法,即哈希或b树。只是使用UNIQUE
索引,尤其是数字索引,即INT
索引,它会比包含重复项的索引更快,因为 b 树等算法能够有效地到达请求的行If the field needs to be UNIQUE then it should either be the
PRIMARY KEY
or aUNIQUE INDEX
.As for performance between
UNIQUE INDEX
andINDEX
, there is no difference when selecting as both will use the same algorithm i.e. hashing or b-tree. It's just that with aUNIQUE
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)