簡體   English   中英

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

[英]Do I need to add an index on a mySQL table if the ID column is the primary key?

I have a table in mySQL where the 'id' column is the 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;

I've also added an index as follows:

CREATE INDEX id_index ON USERS (id);

Did I need to do this? Or is the primary key automatically indexed?

The end aim is here is to speed up queries which join on the id column of table USERS.

Thanks

不,您不需要這樣做。

主鍵會自動建立索引。 您需要索引其他表中作為外鍵的列。

您無需向PK添加其他索引。

使用主鍵

默認情況下,主鍵的工作方式類似於INDEX KEY。 您無需創建此索引索引鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM