PostgreSQL中通常如何定义索引(来自MySQL)
和其他东西一样,PostgreSQL 似乎有更强大/更复杂的表索引。也许有人可以帮助我了解索引列的默认方式。
默认情况下,我指的是整数/布尔列,90% 的时间都使用它们来过滤表结果。
在 MySQL 中,我只需在与该列同名的列上创建一个索引。我不确定使用了什么类型(btree?)或者将索引命名为与列相同的含义是什么 - 但它有效。
现在转向 PostgreSQL,我想知道用相同的名称命名索引是否有任何问题(或任何原因)。另外,我想知道 int/bool 值应该使用哪种类型的索引。
Like everything else, it seems PostgreSQL has much more powerful/complex indexing for tables. Perhaps someone can help me to know the default way to index columns.
By default I mean integer/boolean columns which are used 90% of the time to filter table results.
In MySQL I would simply create an index on a column with the same name as that column. I'm not sure what type was used (btree?) or what the implications of naming the index the same thing as the column were - but it worked.
Now moving to PostgreSQL I'm wondering if there is any problem naming the index with the same name (or any reason not too). Also, I'm wondering which type of index should be used for int/bool values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数情况下,默认索引类型(btree)就可以了。
默认索引名称(如果您未在 CREATE INDEX 语句中指定)基于表和列名称,通常类似于 table_column_idx。 IIRC 索引名称在模式中必须是唯一的,因此,如果您使用与列相同的名称来命名索引,则在模式中的多个表中使用相同的列名称时,您可能会遇到麻烦。
For the most part the default index type (btree) will be fine.
The default index name (if you don't specify one in the CREATE INDEX statement) is based on the table and column name, typically something like table_column_idx. IIRC index names must be unique within a schema, so if you name your indexes with the same name as the columns you may run into trouble if the same column name is used in more than one table in the schema.
如果要对布尔值建立索引,请使用条件索引。大多数布尔值要么是均匀分布的,在这种情况下,仅布尔值的索引几乎不会给你带来任何好处,因为你会以一种或另一种方式阅读。但是,如果您有一个布尔值,其中 99.999% 的行是一个值,另外 0.001% 是另一个值,则仅在 0.001% 上创建索引是有意义的:
请注意,这也适用于多列索引。
If you want to index booleans use a conditional index. Most boolean values are either evenly distruted, in which case an index on just boole gains you almost nothing, since you'll be reading half one way or the other. But, in instances where you have a boole where 99.999% of the rows are one value and the other 0.001% are the other value, creating an index on JUST that 0.001% makes sense:
Note that this also works for multicolumn indexes.