如果ID列是主键,是否需要在mySQL表上添加索引?

Ten*_*kha 8 mysql indexing join outer-join

我在mySQL中有一个表,其中'id'列是PRIMARY KEY:

CREATE TABLE `USERS` (
  `ID` mediumint(9) NOT NULL auto_increment,
  .....
  PRIMARY KEY  (`ID`),
  KEY `id_index` (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=267 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

我还添加了一个索引如下:

CREATE INDEX id_index ON USERS (id);
Run Code Online (Sandbox Code Playgroud)

我需要这样做吗?或者主键是否自动编入索引?

最终目的是加速在表USERS的id列上加入的查询.

谢谢

vel*_*ije 12

不,你不需要这样做.

主键自动编入索引.您需要的是索引其他表中的外键列.

  • MySQL将自动创建索引[即使是外键](http://stackoverflow.com/questions/3529161/indexes-on-primary-and-foreign-keys). (3认同)