简体   繁体   English

使用主键作为索引

[英]Using Primary Keys as Index

In my application I usually use my primary keys as a way to access data. 在我的应用程序中,我通常使用主键作为访问数据的方式。 however I've been told in order to increase performance, I should index columns in my table, However I have no idea what columns should be indexed. 但是我被告知为了提高性能,我应该在我的表中索引列,但是我不知道应该索引哪些列。

Now the Question: 现在问题:

1) Is it a good idea to make your primary key as an Index?(assuming the primary key is unique,an id 1)将主键作为索引是否是个好主意?(假设主键是唯一的,一个id

2) how would I know what columns to index ? 2)我怎么知道要索引的列?

1) Primary keys are implemented using a unique index automatically in Postgres. 1) 主键在Postgres中自动使用唯一索引实现
The same is true for MySQL. MySQL也是如此。

You are done here. 你在这里完成了。

2) For advice on additional indices, consider this closely related answer: 2)关于其他指数的建议,请考虑这个密切相关的答案:
Optimize PostgreSQL read-only tables 优化PostgreSQL只读表

Again, the basics are the same for MySQL and Postgres. 同样,MySQL和Postgres的基础是相同的。 However, Postgres has more advanced features like partial or functional indices if you need them. 但是,如果您需要,Postgres具有更多高级功能,如部分或功能索引 Start with the basics, though. 但是,从基础知识开始。

Your primary key will already have an index that is created for you automatically by PostgreSQL. 您的主键已经有一个PostgreSQL自动为您创建的索引。 You do not need to index the column again. 您无需再次索引该列。

As far as the rest of the fields go, take a look at the article here on figuring out cardinality: http://kirk.webfinish.com/2013/08/some-help-to-find-uniqueness-in-a-large-table-with-many-fields/ 至于其他领域,请看看这里关于计算基数的文章: http//kirk.webfinish.com/2013/08/some-help-to-find-uniqueness-in-a-大表有一对多场/

Fields that are completely unique are candidates, fields that have no uniqueness at all are useless to index. 完全唯一的字段是候选字段,没有唯一性的字段对索引无用。 The sweet spot is the cardinality in the middle (.5). 甜蜜点是中间的基数(.5)。

And of course you should take a look at which columns you are using in the WHERE clause. 当然,您应该查看WHERE子句中使用的列。 It is useless to index columns that are not a part of your quals. 索引不属于您的quals的列是没用的。

Primary keys will have an idex only if you formally define them as primary keys. 只有在正式将主键定义为主键时,主键才会具有idex。 Where most people forget to make indexes are Foriegn keys which are not generally automatically indexed and almost always will be involved in joins and thus indexed. 大多数人忘记制作索引的地方是Foriegn键,它们通常不会自动编入索引,并且几乎总是会参与连接并因此编入索引。 Other candidates for indexes are things you frequently filter data on that have a large number fo possible values, things like names, part numbers, start Dates, etc. 索引的其他候选者是您经常过滤数据的东西,其中包含大量可能的值,例如名称,部件号,开始日期等。

1) Is it a good idea to make your primary key as an Index?(assuming the primary key is unique,an id 1)将主键作为索引是否是个好主意?(假设主键是唯一的,一个id

All DBMSes I know of will automatically create an index underneath the PK. 我所知道的所有DBMS都会自动在PK下创建一个索引。

In case of MySQL/InnoDB, PK will not just be indexed, but that index will be clustered index . 在MySQL / InnoDB的情况下,PK不仅会被索引,而且该索引将成为聚簇索引

(BTW, just saying "primary key" implies it is unique, so there is no need to explicitly state "assuming the primary key is unique".) (顺便说一下,只是说“主键”意味着它是唯一的,所以没有必要明确说明“假设主键是唯一的”。)

2) how would I know what columns to index ? 2)我怎么知道要索引的列?

That depends on which queries need to be supported. 这取决于需要支持哪些查询。

But beware that adding indexes is not free and is a matter of engineering tradeoff - while some queries might benefit from an index, some may actually suffer from it. 但请注意,添加索引不是免费的,而是工程权衡的问题 - 虽然某些查询可能会从索引中受益,但实际上有些查询可能会受到影响。 For example: 例如:

  • An index on FOO would significantly speed-up the SELECT * FROM T WHERE FOO = ... . FOO上的索引将显着加快SELECT * FROM T WHERE FOO = ...
  • However, the same index would somewhat slow-down the INSERT INTO T VALUES (...) . 但是,相同的索引会稍微减慢INSERT INTO T VALUES (...)

In most situations you'd favor large speedup in SELECT over small slowdown in INSERT, but that may not always be the case. 在大多数情况下,你喜欢SELECT中的大加速而不是INSERT中的小减速,但情况可能并非总是如此。

Indexing and the database performance in general are a complex topic beyond the scope of a humble StackOverflow post, but if you are interested I warmly recommend reading Use The Index, Luke! 索引和数据库性能通常是一个复杂的主题,超出了简单的StackOverflow帖子的范围,但如果你感兴趣我热烈推荐阅读使用索引,卢克! .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM