我正在调查一些缓慢的查询,我需要一些帮助来读取我得到的数据。
我们有一个特定的查询,它使用索引并且大多数时候运行得相当快,但是有时它运行得很慢(700 毫秒以上),不知道为什么。
Limit (cost=8.59..8.60 rows=1 width=619) (actual time=5.653..5.654 rows=1 loops=1)
-> Sort (cost=8.59..8.60 rows=1 width=619) (actual time=5.652..5.652 rows=1 loops=1)
Sort Key: is_main DESC, id
Sort Method: quicksort Memory: 25kB
-> Index Scan using index_pictures_on_imageable_id_and_imageable_type on pictures (cost=0.56..8.58
rows=1 width=619) (actual time=3.644..5.587 rows=1 loops=1)
Index Cond: ((imageable_id = 12345) AND ((imageable_type)::text = 'Product'::text))
Filter: (tag = 30)
Rows Removed by Filter: 2
Planning Time: 1.699 ms
Execution Time: 5.764 ms
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,我会说查询的几乎全部成本都在索引扫描上,对吗?这对我来说听起来不错,那么为什么同一个查询有时运行得相当慢呢?
我开始认为我们的实例可能无法将整个索引保留在内存中,因此它时不时地使用磁盘。这可以解释缓慢的查询。然而,这超出了我的想象。那有意义吗?
该表有大约 1500 万行,5156 MB大小也大约是 1500 万行。索引为 …