在主键上创建多个索引是否有性能优势?外键?
如果我有一个包含主键和外键的表,并且经常使用包含 (...WHEREprimary=n ANDforeign=x)
的查询来完成搜索,那么是否有任何性能使用两个键在 MySQL 中创建多重索引有什么好处?
我知道它们都已经是索引了,但是我不确定外键包含在另一个表中时是否仍然被视为索引。例如,MySQL 会查找主键,然后比较外键的所有值,直到找到正确的值,或者它是否已经知道它在哪里,因为外键也是一个索引?
更新:我正在使用 InnoDB 表。
If I have a table that has a primary key and a foreign key, and searches are frequently done with queries that include both (...WHERE primary=n AND foreign=x)
, is there any performance benefit to making a multiple index in MySQL using the two keys?
I understand that they are both indexes already, but I am uncertain if the foreign key is still seen as an index when included in another table. For example, would MySQL go to the primary key, and then compare all values of the foreign key until the right one is found, or does it already know where it is because the foreign key is also an index?
Update: I am using InnoDB tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于相等比较,您无法获得对主键索引的改进(因为此时最多只有一行可以匹配)。
访问路径是:
如果在主数据库上进行范围扫描,复合索引可能会有意义键并希望通过另一列缩小范围。
For equality comparisons, you cannot get an improvement over the primary key index (because at that point, there is at most just one row that can match).
The access path would be:
A composite index might make some sense if you have a range scan on the primary key and want to narrow that down by the other column.