我有2个大表(每个表大约3M行),当我对每个表进行计数时,一个表返回的速度非常快,而另一个表却非常慢。
user_challenges 表很快
设计表非常慢
两个表之间的区别之一是,“设计”表的每一行都有很多数据(包括二进制)数据。这对计数行有影响吗?
picto=# explain analyze verbose select count(*) from user_challenges;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=68655.59..68655.60 rows=1 width=8) (actual time=662.859..662.859 rows=1 loops=1)
Output: count(*)
-> Seq Scan on public.user_challenges (cost=0.00..60942.07 rows=3085407 width=0) (actual time=0.028..447.922 rows=3083874 loops=1)
Output: id, challenges_id, user_id, design_uid, status, created_at, updated_at, was_watched
Planning time: 0.585 ms
Execution time: 663.148 ms
(6 rows)
picto=# explain analyze verbose select count(*) from designs;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=316148.21..316148.22 rows=1 width=8) (actual time=22173.638..22173.638 rows=1 loops=1)
Output: count(*)
-> Index Only Scan using "Design_idx_owneruid" on public.designs (cost=0.43..308686.02 rows=2984875 width=0) (actual time=0.033..21873.292 rows=2995782 loops=1)
Output: user_id
Heap Fetches: 1536177
Planning time: 1.547 ms
Execution time: 22173.667 ms
(7 rows)
关于为什么设计表上的计数需要22s而user_challenges表上的计数需要<1s的任何建议?
我想知道行内数据的数量是否会影响计数速度。我确实注意到设计计数使用的是“仅索引扫描”。
任何帮助将不胜感激。