我正在使用 Ubuntu 16.04 以及 PostgreSQL 9.5 和 Django 1.11
我的网站一直遭受超长 ajax 调用的困扰(在某些情况下长达 30 秒)。相同的 ajax 调用大约需要 500 毫秒的开发时间。
该问题与磁盘读取 I/O 相关。在生产中执行单个查询会驱动磁盘读取 I/O高达 25MB/秒 https://i.gyazo.com/e212d736db195831b5dd0e422db62a14.png; an 完全相同的开发中的查询导致磁盘读取 I/O 低于 0.01 MB/s。代码和查询在生产/开发中是相同的。
因此,生产中的 postgres 的某些问题会导致磁盘读取 I/O 异常高。会是什么呢?
下面是一个示例查询,在生产中大约需要 25 秒,而在开发中只需要 500 毫秒:
EXPLAIN (ANALYZE, BUFFERS)
SELECT COUNT(*) AS "__count" FROM "map_listing"
WHERE ("map_listing"."lo" < -79.32516245458987 AND "map_listing"."la" > 43.640279060122346
AND "map_listing"."lo" > -79.60531382177737 AND "map_listing"."transaction_type" = 'Sale'
AND "map_listing"."la" < 43.774544561921296
AND NOT ("map_listing"."status" = 'Sld' AND "map_listing"."sold_date" < '2018-01-21'::date
AND "map_listing"."sold_date" IS NOT NULL)
AND NOT (("map_listing"."status" = 'Ter' OR "map_listing"."status" = 'Exp'))
AND NOT (("map_listing"."property_type" = 'Parking Space' OR "map_listing"."property_type" = 'Locker')));
做的结果EXPLAIN (ANALYZE, BUFFERS)
关于上述声明(生产):
Aggregate (cost=89924.55..89924.56 rows=1 width=0) (actual time=27318.859..27318.860 rows=1 loops=1)
Buffers: shared read=73424
-> Bitmap Heap Scan on map_listing (cost=4873.96..89836.85 rows=35079 width=0) (actual time=6061.214..27315.183 rows=3228 loops=1)
Recheck Cond: ((la > 43.640279060122346) AND (la < 43.774544561921296))
Rows Removed by Index Recheck: 86733
Filter: ((lo < '-79.32516245458987'::numeric) AND (lo > '-79.60531382177737'::numeric) AND ((status)::text <> 'Ter'::text) AND ((status)::text <> 'Exp'::text) AND ((property_type)::text <> 'Parking Space'::text) AND ((property_type)::text <> 'Locker'::text) AND ((transaction_type)::text = 'Sale'::text) AND (((status)::text <> 'Sld'::text) OR (sold_date >= '2018-01-21'::date) OR (sold_date IS NULL)))
Rows Removed by Filter: 190108
Heap Blocks: exact=46091 lossy=26592
Buffers: shared read=73424
-> Bitmap Index Scan on map_listing_la_88ca396c (cost=0.00..4865.19 rows=192477 width=0) (actual time=156.964..156.964 rows=194434 loops=1)
Index Cond: ((la > 43.640279060122346) AND (la < 43.774544561921296))
Buffers: shared read=741
Planning time: 0.546 ms
Execution time: 27318.926 ms
(14 rows)
的结果EXPLAIN (ANALYZE, BUFFERS)
(发展):
Aggregate (cost=95326.23..95326.24 rows=1 width=8) (actual time=495.373..495.373 rows=1 loops=1)
Buffers: shared read=77281
-> Bitmap Heap Scan on map_listing (cost=5211.98..95225.57 rows=40265 width=0) (actual time=80.929..495.140 rows=4565 loops=1)
Recheck Cond: ((la > 43.640279060122346) AND (la < 43.774544561921296))
Rows Removed by Index Recheck: 85958
Filter: ((lo < '-79.32516245458987'::numeric) AND (lo > '-79.60531382177737'::numeric) AND ((status)::text <> 'Ter'::text) AND ((status)::text <> 'Exp'::text) AND ((property_type)::text <> 'P
arking Space'::text) AND ((property_type)::text <> 'Locker'::text) AND ((transaction_type)::text = 'Sale'::text) AND (((status)::text <> 'Sld'::text) OR (sold_date >= '2018-01-21'::date) OR (sold_date
IS NULL)))
Rows Removed by Filter: 198033
Heap Blocks: exact=49858 lossy=26639
Buffers: shared read=77281
-> Bitmap Index Scan on map_listing_la_88ca396c (cost=0.00..5201.91 rows=205749 width=0) (actual time=73.070..73.070 rows=205569 loops=1)
Index Cond: ((la > 43.640279060122346) AND (la < 43.774544561921296))
Buffers: shared read=784
Planning time: 0.962 ms
Execution time: 495.822 ms
(14 rows)