Postgres RDS - 相同数据和索引的直方图性能大幅降低

时间:2016-04-09 13:59:24

标签: postgresql amazon-rds

在我们开发一个系统时,我们正在开发一个由Amazon RDS(postgres)支持的登台服务器,我们发现一个运行在相同数据和相同索引上的查询在RDS上的运行速度比本地运行慢近1000倍。

SELECT cast(min(price_latest) as bigint) as minprice,cast(max(price_latest) as bigint) as maxprice, COUNT(*), histogram(cast(price_latest as bigint), 0, 10000000, 1000) FROM "locations" WHERE (ST_Contains(ST_GeomFromText('MULTIPOLYGON (((1.734671 52.591183, 1.744188 52.573059, 1.729994 52.562677, 1.742489 52.528631, .... , 1.734671 52.591183)))'),lonlat))

本地它在100-200ms范围内运行(这是可以接受的),在RDS上它需要大约80秒才能返回相同的结果......

查询上的本地EXPLAIN ANALYZE给出:

Aggregate  (cost=67403.05..67403.07 rows=1 width=8) (actual time=110.657..110.657 rows=1 loops=1)
->  Bitmap Heap Scan on locations  (cost=686.61..66488.76 rows=3483 width=8) (actual time=6.329..60.381 rows=8180 loops=1)
    Recheck Cond: ('010600000001 (...)
    Filter: (is_primary AND (live = 1) AND ((buy_or_rent)::text = 'buy'::text) AND _st_contains('010600000001 (...)
    Rows Removed by Filter: 12232
    ->  Bitmap Index Scan on index_lonlat  (cost=0.00..685.74 rows=21005 width=0) (actual time=5.600..5.600 rows=21646 loops=1)
          Index Cond: ('010600000001 (...)
Total runtime: 111.027 ms

RDS远程版本提供:

Aggregate  (cost=67462.71..67462.72 rows=1 width=8) (actual time=107304.702..107304.702 rows=1 loops=1)
->  Bitmap Heap Scan on locations  (cost=826.47..66578.87 rows=3367 width=8) (actual time=5000.747..107178.185 rows=8180 loops=1)
    Recheck Cond: ('010600000001 (...)
    Filter: (is_primary AND (live = 1) AND ((buy_or_rent)::text = 'buy'::text) AND _st_contains('010600000001 (...)
    Rows Removed by Filter: 13646
    ->  Bitmap Index Scan on index_lonlat  (cost=0.00..825.62 rows=23095 width=0) (actual time=4912.543..4912.543 rows=21826 loops=1)
          Index Cond: ('010600000001 (...)
Total runtime: 107326.975 ms

这是我们使用的直方图函数:postgres function

似乎他们(本地macbook机器和RDS)都以同样的方式攻击查询 - 那么为什么会出现差异呢? - 结果在一行返回,所以我不认为这是网络延迟。

0 个答案:

没有答案