mysql唯一索引和普通索引那个效率高。我指的是效率,区别我明白。
答案:2 悬赏:30 手机版
解决时间 2021-03-01 15:43
- 提问者网友:姑娘长的好罪过
- 2021-02-28 21:18
是这样,我在表主键开始建立的是普通索引,后面我想改成唯一索引来,主键本来就是自动增长的,所以不存在重复的情况。而发现之前那个表是主键是普通索引,我现在想改成唯一索引,请问有没有影响的情况?那个更好?
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-02-28 22:34
1.主键必须是唯一的索引,唯一索引不一定是主键;
2.一个表可以有多个唯一索引,但只能有一个主键;
3.主键列不允许空值,而唯一索引列允许空值。
2.一个表可以有多个唯一索引,但只能有一个主键;
3.主键列不允许空值,而唯一索引列允许空值。
全部回答
- 1楼网友:持酒劝斜阳
- 2021-02-28 23:19
二级索引??
mysql中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅助索引(secondary indexes)。
以innodb来说,每个innodb表具有一个特殊的索引称为聚集索引。如果您的表上定义有主键,该主键索引是聚集索引。如果你不定义为您的表的主键时,mysql取第一个唯一索引(unique)而且只含非空列(not null)作为主键,innodb使用它作为聚集索引。如果没有这样的列,innodb就自己产生一个这样的id值,它有六个字节,而且是隐藏的,使其作为聚簇索引。
聚簇索引主要是为了方便存储。。所以二级索引应该都是对聚簇索引的索引。
下面是mysql manual上的原话,也可能我理解有误。
every innodb table has a special index called the clustered index where the data for the rows is stored. if you define a primary key on your table, the index of the primary key is the clustered index.
if you do not define a primary key for your table, mysql picks the first unique index that has only not null columns as the primary key and innodb uses it as the clustered index. if there is no such index in the table, innodb internally generates a hidden clustered index on a synthetic column containing row id values. the rows are ordered by the id that innodb assigns to the rows in such a table. the row id is a 6-byte field that increases monotonically as new rows are inserted. thus, the rows ordered by the row id are physically in insertion order.
accessing a row through the clustered index is fast because the row data is on the same page where the index search leads. if a table is large, the clustered index architecture often saves a disk i/o operation when compared to storage organizations that store row data using a different page from the index record. (for example, myisam uses one file for data rows and another for index records.)
in innodb, the records in non-clustered indexes (also called secondary indexes) contain the primary key value for the row. innodb uses this primary key value to search for the row in the clustered index. if the primary key is long, the secondary indexes use more space, so it is advantageous to have a short primary key.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯