我试图弄清楚为什么特定的 SQL 查询在 postgres 上运行缓慢。没有太多解释,这里是查询的解释分析:
explain analyze SELECT "addressbooks_address"."id", "addressbooks_address"."name" FROM "addressbooks_recipientaddress" INNER JOIN"addressbooks_address" ON ("addressbooks_recipientaddress"."address_ptr_id" = "addressbooks_address"."id") ORDER BY "addressbooks_recipientaddress"."address_ptr_id" ASC LIMIT 1000 OFFSET 378000;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=40139.55..40245.42 rows=1000 width=21) (actual time=720.444..721.958 rows=1000 loops=1)
-> Merge Join (cost=121.88..67152.43 rows=633159 width=21) (actual time=0.028..698.069 rows=379000 loops=1)
Merge Cond: (addressbooks_recipientaddress.address_ptr_id = addressbooks_address.id)
-> Index Scan using addressbooks_recipientaddress_pkey on addressbooks_recipientaddress (cost=0.00..19258.72 rows=633159 width=4) (actual time=0.012..189.480 rows=379000 loops=1)
-> Index Scan using addressbooks_address_pkey on addressbooks_address (cost=0.00..38291.65 rows=675743 width=17) (actual time=0.011..227.094 rows=388264 loops=1)
Total runtime: 722.092 ms
该查询是由 django 生成的,但在将其发布到此处之前,我已经对其进行了一些简化。但问题仍然存在。如解释所示,我有 addressbooks_address.id 和 addressbooks_recipientaddress.address_ptr_id 的索引。
有任何想法吗?