简体   繁体   English

如果表上有索引,如何处理表更新/插入

[英]How handle table updates/inserts if there is an index on table

Hi i am a bit confused about the handling of indexes in postgres. 嗨,我对postgres中的索引处理有些困惑。 I use the 9.6 version. 我使用9.6版本。 From my understanding after reading postgres docs and answers from stackoverflow i want to verify the following: 从阅读postgres文档和stackoverflow的答案后的理解来看,我想验证以下内容:

  • postgres does not support indexes with the classic notion postgres不支持经典概念的索引
  • all indexes in postgres are non-clustered indexes Postgres中的所有索引都是非聚集索引
  • indexes does not allocate any new space but apply a sort on the table thats way after create index a CLUSTER command shall follow. 索引不会分配任何新空间,而是在创建索引之后对表应用排序,因此必须遵循CLUSTER命令。
  • in the docs it is stated that after updates/inserts on table the index is updated automatically 在文档中指出,在对表进行更新/插入之后,索引会自动更新

Show i created a table with col1,col2,col3,col4 and the an index based on col2, col3. 显示我用col1,col2,col3,col4创建了一个表,并基于col2,col3创建了一个索引。 Selects that have to do with col2, col3 became 15 times faster. 与col2有关的选择,col3的速度提高了15倍。 When i execute select * from table then results are displayed first sorted based on col2 and then based on col3. 当我执行select * from table时,结果将首先显示基于col2,然后基于col3排序。

When i add a new row in the table (with a col2 value (test_value) that already existed), this row went at the end of the table (this was checked with : select * from table). 当我在表中添加新行(具有col2值(test_value)已经存在)时,该行位于表的末尾(已通过:select * from table进行检查)。

1) Did the index got updated with this new entry automatically even if the select all showed the row at the end? 1)即使全选最后显示了该行,索引是否也已使用此新条目自动更新?

2) If a execute a query will all the rows that have the test_value on col2 what will happen? 2)如果执行查询,col2上所有具有test_value的行将发生什么? Will i get all the results through the index? 我可以通过索引获得所有结果吗?

There are some wrong assumptions here. 这里有一些错误的假设。

The most important is: The order of the rows in a select is indeterminate unless you include ORDER BY . 最重要的是:select中的行顺序是不确定的,除非您包括ORDER BY So you can get any result db engine decide is the faster way to get the data. 因此,您可以获得数据库引擎决定的任何结果,这是获取数据的更快方法。 So if select * from table return the last inserted element at the end, that doesn't tell you anything regarding the index. 因此,如果select * from table则最后返回最后插入的元素,这不会告诉您有关索引的任何信息。

How Rows are stored and Index information are separate things 行的存储方式和索引信息是分开的

1) Yes, index was updated after insert. 1)是,插入后索引已更新。

2) Yes, because index was already update. 2)是,因为索引已经更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM