После миграции базы данных из автономного экземпляра PostgreSQL в Amazon RDS мы сталкиваемся с некоторыми странными проблемами с запросами к таблице 12M строк.
Запросы, которые работали раньше, теперь блокируются с помощью wait_event_type=LWLockTranche
и wait_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: загрузка ЦП стабильна на уровне 20-30%, подключения к БД 2-40, свободная память 50% (3 ГБ), запись IOPS 1-10, чтение IOPS 650-750, свободное хранилище 100 ГБ.
Чем может быть вызвана эта разница? Что мы можем проверить больше?