如何确定在 Postgres 中使用什么类型的索引?
我有一个 Postgres 数据库,其中有 2 列不是主键(也不能是主键),但进行了大量搜索,并与其他表中的 2 列进行比较以确保相等。
我相信这是向我的表添加索引的完美案例。我以前从未在数据库上使用过索引,所以我正在尝试学习执行此操作的正确方法。
我了解到可以选择多种类型的索引。如何确定哪种方法对我的数据库最有效?另外,正确的方法是创建一个覆盖两列的索引吗?
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.
I believe this is a perfect case for adding an index to my tables. I have never used indexing on a database before so I am trying to learn the proper way of doing this.
I have learned that there are multiple types of indexing I can pick from. How do I determine what method will be the most efficient for my database? Also would the proper method be to create a single index that covers both columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Postgres 支持 B 树、R 树、哈希、GiST 和 GIN 索引类型。 B 树索引是最常见的,适合最常见的场景。这是语法:
这是 createindex 文档,这里是有关 postgres 中不同索引类型的更多信息。
您应该使用什么类型的索引取决于您要执行什么类型的操作。如果您只想进行相等性检查,那么哈希索引是最好的。对于大多数常见操作(例如比较、模式匹配),应使用 B 树。我个人从未使用过 GiST 或 GIN 索引。那里有大师吗?
该文档描述了所有这些类型。他们可以比我更好地帮助您:)
希望这会有所帮助。
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:
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.
尝试理解 queryplanner ,因为这部分PostgreSQL 必须与您的索引一起使用。 EXPLAIN ANALYZE 对于优化查询至关重要。
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.