[英]mysql unique key and index
in MySQL, If I create a unique key on two columns, say 在MySQL中,如果我在两列上创建一个唯一键,比如说
UNIQUE KEY my_key
( column1
, column2
) UNIQUE KEY
my_key
( column1
, column2
)
is it necessary to build another key on column1
? 是否有必要在
column1
上构建另一个键? My goal is to have column1 and column2 to be unique, and I'll do lots of selects keyed by column1. 我的目标是让column1和column2是唯一的,我会做很多由column1键入的选择。
No, it is not necessary because an index on (column1, column2) can be used instead of an index of column1 alone. 不,这不是必需的,因为可以使用(column1,column2)上的索引而不是column1的索引。
You might want to make a new index on just column two though as the index (column1, column2) is not good when searching only for (column2). 您可能希望仅在第二列上创建一个新索引,因为当仅搜索(column2)时索引(column1,column2)不好。
No, your my_key
index takes care of any queries on column1
or conditions on column1
AND column2
. 不,您的
my_key
索引负责处理column1
上的任何查询或column1
和column2
上的条件。 However, if you make queries on only column2
, then you should add an additional index for column2
to be able to query it efficiently. 但是,如果仅对
column2
进行查询,则应为column2
添加其他索引,以便能够有效地进行查询。
Furthermore, if both column1
and column2
are unique, then you might want to consider using something like 此外,如果
column1
和column2
都是唯一的,那么您可能需要考虑使用类似的东西
[...]
UNIQUE(column1),
UNIQUE(column2),
PRIMARY KEY (column1, column2);
This ensures that both column1
and column2
are unique, and any query selecting only column1
and column2
can be retrieved using index-only access. 这可确保
column1
和column2
都是唯一的,并且可以使用仅索引访问来检索仅选择column1
和column2
任何查询。
A UNIQUE-index is a btree-index . UNIQUE-index是btree-index 。 This index is sorted and that's why you don't need a second index on the first colummn.
这个索引是排序的,这就是你不需要第一个colummn的第二个索引的原因。 The sort order of the combination will also work for only the first column, but not for only the second column.
组合的排序顺序也仅适用于第一列,但不适用于第二列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.