How to determine what type of index to use in Postgres?

前端 未结 2 1737
长发绾君心
长发绾君心 2021-02-05 03:09

I have a Postgres database that has 2 columns that are not primary keys (nor can be), but are searched on a lot and are compared for equality to 2 columns in other tables.

相关标签:
2条回答
  • 2021-02-05 03:14

    Postgres support B-tree, R-tree, Hash, GiST and GIN indexing types. B-tree indexing is the most common and fits most common scenarios. This is the syntax:

    CREATE INDEX idex_name ON table_name USING btree(column1, column2);
    

    Here is the createindex documentation and here is more info on different indextypes in postgres.

    What type of index you should use depends on what types of operations you want to perform. If you simply want equality checking then hash index is the best. For most common operations(e.g. comparison, pattern matching) B-tree should be used. I have personally never used GiST or GIN indexing. ANY Guru out there?

    The documentation describes all these types. They can help you better than me :)

    Hope this helps.

    0 讨论(0)
  • 2021-02-05 03:17

    Try to understand the queryplanner as well, because this part of PostgreSQL has to work with your indexes. EXPLAIN ANALYZE will be essential to optimise your queries.

    0 讨论(0)
提交回复
热议问题