Postgres and Indexes on Foreign Keys and Primary Keys

前端 未结 7 1670
青春惊慌失措
青春惊慌失措 2020-12-02 03:34

Does Postgres automatically put indexes on Foreign Keys and Primary Keys? How can I tell? Is there a command that will return all indexes on a table?

7条回答
  •  [愿得一人]
    2020-12-02 04:20

    I love how this is explained in the article Cool performance features of EclipseLink 2.5

    Indexing Foreign Keys

    The first feature is auto indexing of foreign keys. Most people incorrectly assume that databases index foreign keys by default. Well, they don't. Primary keys are auto indexed, but foreign keys are not. This means any query based on the foreign key will be doing full table scans. This is any OneToMany, ManyToMany or ElementCollection relationship, as well as many OneToOne relationships, and most queries on any relationship involving joins or object comparisons. This can be a major perform issue, and you should always index your foreign keys fields.

提交回复
热议问题