在主键上创建和索引有什么好处吗? [英] Is there any benefit to creating and index on a primary key?

查看:478
本文介绍了在主键上创建和索引有什么好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了此SO信息:

MySQL中的主键,唯一键和索引有什么区别

并发现语句:


另请注意,定义为主键或唯一键的列会自动在MySQL中索引。

Also note that columns defined as primary keys or unique keys are automatically indexed in MySQL.

根据这个,我有两个问题:

Based on this, I have two questions:


  • 安全地假设在主键本身上创建索引没有性能优势,因为按设计,主键是索引?

也许更重要的问题:


  • 如果您正在做典型的例子,人们引用,基于lastName和firstName执行SELECT,由于主键已经是索引,所以您创建的索引可以是(primary_key,lastName,firstName)还是只是(lastName,firstName)?

推荐答案

对你的第一个问题,你可以假设。

To your first question, yets, you're safe to assume that.

对于第二个问题:

索引有助于加快搜索速度 - 这就像书中的索引。他们可以帮助数据库引擎跳转到正确的记录,正如一个索引可以帮助你跳转到一本书的正确的页面。

Indexes help to speed up searching - it's like an index in a book. They can help the DB engine jump to the correct record, just as an index can help you jump to the right page in a book.

创建自己取决于你打算如何搜索数据。

The benefit to indexes that you might create youself depends on how you intend to search the data.

在你的例子中,如果你打算在名字字段上创建一个INDEX在您的应用程式。

In your example, I'd create an INDEX on the name fields if you're going to search on them in your app.

这篇关于在主键上创建和索引有什么好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆