Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在Postgres中,完成一个简单的索引搜索需要阅读多少页?_Postgresql_Indexing_Common Table Expression - Fatal编程技术网

Postgresql 在Postgres中,完成一个简单的索引搜索需要阅读多少页?

Postgresql 在Postgres中,完成一个简单的索引搜索需要阅读多少页?,postgresql,indexing,common-table-expression,Postgresql,Indexing,Common Table Expression,我很想了解Postgres在使用索引时如何从磁盘/缓存读取页面 考虑查询索引的单列整数表: select i into numbers from generate_series(1, 200000) s(i); create index idx on numbers(i); explain (buffers, analyse) select * from numbers where i = 456789; -- random row 此单一索引仅搜索此200k行表需要3次页面读取(Buf

我很想了解Postgres在使用索引时如何从磁盘/缓存读取页面

考虑查询索引的单列整数表:

select i 
into numbers
from generate_series(1, 200000) s(i);

create index idx on numbers(i);

explain (buffers, analyse)
select * from numbers where i = 456789; -- random row
此单一索引仅搜索此200k行表需要3次页面读取(
Buffers:shared hit=3
):

这是预期的吗?这三页与什么有关?这仅仅是为了遍历B树而必须读取的索引页的数量吗


Background:我正在尝试调整递归CTE,它遍历作为父子关系/邻接树存储在单个表中的链表结构。查询的递归部分是一个与上面类似的非常简单的索引查找。递归CTE的每个“循环”导致3次页面读取(如上所述),这是查询的大部分成本所在。可能无法提高效率,但我想知道这是否可以改进(目前10k节点链的页面读取量约为30000页,缓存时间约为25ms)。

您很幸运,autovacuum在运行查询之前就完成了,否则将是4个块

您的查询访问了根节点,即中间节点。节点和索引的叶节点。不需要访问表块(
堆获取:0
),因为

  • 索引中提供了所有信息

  • 该表的可见性地图表明该表块是全部可见的,因此没有必要查阅该表块以获取可见性信息


  • 基本上,它需要读取根、分支和叶节点:谢谢-我不知道B树中的每个节点都存储在一个页面中(根据)“这是查询的大部分成本所在”您如何确定这就是成本所在?您是在谈论成本估算还是实际的运行时成本?他的查询没有返回任何行,因为它超出了索引的范围。如果它找到了一行,它将不得不查询VM或表本身,从而导致第四个缓冲区访问。
    Index Only Scan using idx on numbers  (cost=0.42..8.44 rows=1 width=4) (actual time=0.010..0.010 rows=0 loops=1)
      Index Cond: (i = 456789)
      Heap Fetches: 0
      Buffers: shared hit=3
    Planning Time: 0.043 ms
    Execution Time: 0.022 ms