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

前端 未结 2 1733
长发绾君心
长发绾君心 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.

提交回复
热议问题