简体   繁体   English

为什么仅索引扫描需要这么长时间?

[英]Why Index Only Scan is taking so long?

Why does executing my simple query 为什么执行我的简单查询

select count(this_.Id) as y0_ from Activity this_

take so long (more than 10 minutes this time)? 花这么长时间(这次超过10分钟)?

Here is the query plan (output of the EXPLAIN ANALYZE): 这是查询计划(EXPLAIN ANALYZE的输出):

QUERY PLAN  
 Aggregate (cost=854047.36..854047.37 rows=1 width=4)
> (actual time=728525.277..728525.277 rows=1 loops=1)   
->  Index Only
> Scan using activity_pkey on activity this_  (cost=0.56..805401.87
> rows=19458196 width=4) (actual time=36.961..725381.557 rows=19517989
> loops=1)  
> Heap Fetches: 10351403  
Total runtime: 728533.529 ms

And PostgreSql version: 和PostgreSql版本:

PostgreSQL 9.3.5 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.7.2-5) 4.7.2, 64-bit

The index is here too, on the Id field: 索引也位于ID字段上:

ALTER TABLE public.activity
ADD CONSTRAINT activity_pkey 
PRIMARY KEY (id);

Index Only Scan in PostgreSQL has to look sometimes into the table (heap), because index pages don't contain information about tuple visibility. PostgreSQL中的仅索引扫描有时必须查看表(堆),因为索引页不包含有关元组可见性的信息。 That's how many rows were fetched from the index: 那就是从索引中获取了多少行:

(actual ... rows=19517989 (实际...行= 19517989

And that's how many rows were re-checked in the heap: 这就是在堆中重新检查多少行:

Heap Fetches: 10351403 堆获取:10351403

To speed it up, you should run vacuum on you table: vacuum Activity 为了加快速度,您应该在工作台上进行vacuum Activityvacuum Activity

Vacuum will update visibility map , and after that Index Only Scan will be able to perform using (almost) only index pages. Vacuum将更新可见性图 ,然后,“仅索引扫描”将只能使用(几乎)仅索引页执行。

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

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