gpt4 book ai didi

postgresql - 简单查询,迁移后加锁

转载 作者:行者123 更新时间:2023-11-29 11:48:59 25 4
gpt4 key购买 nike

在数据库从自托管 PostgreSQL 实例迁移到 Amazon RDS 后,我们在查询 1200 万行表时遇到了一些奇怪的问题。以前有效的查询现在使用 wait_event_type=LWLockTranchewait_event=buffer_io 锁定(即使是没有任何 JOIN 的最简单的查询)。所有索引和执行计划似乎都很好。除了查询执行时间外,Explain Analyze 没有显示任何奇怪的东西。

示例查询:

explain (verbose, buffers, analyze) SELECT * FROM "products_product" WHERE ("products_product"."category_id" = 43);

自托管 PostgreSQL 的结果:

Index Scan using products_product_b583a629 on public.products_product  (cost=0.43..5256.40 rows=5667 width=1758) (actual time=24.372..298.822 rows=29342 loops=1)
Output: id, title, description, image_path, image_source_url, website_source, date_created, date_updated, afi_url, afi_price_currency, afi_recognize_id, afi_price, afi_old_price_currency, meta_link, afi_old_price, meta_published, meta_admin_note, afi_id, brand_id, category_id, retailer_id, afi_promotion, afi_stock, search_vector, original_category_id, search_vector_pl, title
_pl, description_pl, owner_id
Index Cond: (products_product.category_id = 43)
Buffers: shared hit=71 read=22261
I/O Timings: read=233.266
Planning time: 0.271 ms
Execution time: 310.205 ms

以及来自 Amazon RDS 的同一查询的结果:

Index Scan using products_product_b583a629 on public.products_product  (cost=0.43..27905.30 rows=30563 width=1753) (actual time=26.084..179652.029 rows=29342 loops=1)
Output: id, title, description, image_path, image_source_url, website_source, date_created, date_updated, afi_url, afi_price_currency, afi_recognize_id, afi_price, afi_old_price_currency, meta_link, afi_old_price, meta_published, meta_admin_note, afi_id, brand_id, category_id, retailer_id, afi_promotion, afi_stock, search_vector, original_category_id, search_vector_pl, title_pl, description_pl, owner_id
Index Cond: (products_product.category_id = 43)
Buffers: shared hit=2532 read=19856
Planning time: 0.093 ms
Execution time: 179665.121 ms

RDS:CPU 使用率稳定在 20-30% 水平,数据库连接数 2-40,可用内存 50% (3GB),写入 IOPS 1-10,读取 IOPS 650-750,可用存储空间 100GB。

是什么导致了这种差异?我们还能检查什么?

最佳答案

引用这个,https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html

AWS 的 iops 上限取决于您的存储类型。如果是 gp2,则每 GB 存储可获得 3 个 iops。如果您的索引是 int 类型,有 12M 条记录,则索引大小可能为 150MB。在 700 iops 下,即使没有其他 session 运行也需要一段时间。如果你有其他 session 占用了 iops,你会得到 buffer_io 等待。

关于postgresql - 简单查询,迁移后加锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53125750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com