在 Varchar 上创建 MySQL 索引总是会产生 2 个索引?
我的表中有一个 email
列,它是 VARCHAR(255)
。大多数电子邮件的长度只有 40-50 个字符,其余字符仅在极少数情况下存在。因此,我只想索引电子邮件列的前 50 个字符。
我尝试使用此查询来执行此操作:
ALTER TABLE `users` ADD INDEX (email(50) )
但是,这会创建 2 个索引,一个名为 email
,另一个名为 email_2
。第一个索引没有长度限制,第二个索引有 50 个字符的限制。
这是为什么呢?有没有解决方法,或者我别无选择,只能索引整个列?
I have an email
column in my table which is a VARCHAR(255)
. Most emails would only be 40-50 characters long, and the rest of the characters are there just there for a rare case. Therefore, I want to only index the first 50 characters of the email column.
I've tried to do it with this query:
ALTER TABLE `users` ADD INDEX (email(50) )
However, that creates 2 indexes, one named email
and the other email_2
. The first index has no length limits, the second one has got the 50 character limit.
Why is this? Is there any workaround or do I have no option but to index the full column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么只对前 50 个字符建立索引? MySQL 将足够聪明,只将索引中的空间用于实际数据。即,它不会为每个条目分配 255 个字符的空间。因此,没有理由不简单地按原样索引整个列。
Why bother indexing only the first 50 characters? MySQL will be smart enough to only use space in the index for the actual data. I.e., it isn't going to allocate 255 characters of space for each entry. Thus, there is no reason not to simply index the entire column as it is.