【发布时间】:2015-03-05 17:54:46
【问题描述】:
我正在使用 MySQL,但这个问题并不特定于该特定数据库。
我有posts 表和threads 表。
没什么复杂的,当然在 posts 中我有 thread_id 列,它将帖子与拥有它们的线程联系起来。
我想对该列有外键约束(posts 表中的 thread_id),类似于:
CONSTRAINT `thread_fk` FOREIGN KEY (`thread_id`) REFERENCES `threads` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
所以问题是,在该 thread_id 列上也有第二个索引有多聪明?
像这样:
KEY `thread_idx` (`thread_id`)
或:
INDEX `thread_idx` (`thread_id`)
FOREIGN KEY 索引是否会在选择时加快速度,使第二个索引变得多余,或者第二个索引对于更快的选择是必要的,或者在一列上有两个索引是个坏主意?
【问题讨论】:
标签: mysql sql database indexing