1

我试图弄清楚为什么特定的 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 的索引。

有任何想法吗?

4

1 回答 1

2

限制 1000 偏移量 378000

快速查看您正在做的事情;您正在生成一个相当大的连接,然后将其中的绝大多数丢弃。

OFFSET如果可能,请尝试通过感兴趣行的主键进行分页,而不是使用。记住前面结果中最后一个元组中的addressbooks_address.id和键addressbooks_recipientaddress是什么,并使用 WHERE 子句,例如:

WHERE "addressbooks_recipientaddress"."id" > $1
  AND "addressbooks_address"."id" > $2

而不是OFFSET. 这样,您的索引扫描就可以直接跳到那些记录,而不是浪费所有时间来生成结果以丢弃。

如果您的框架不能做到这一点,那么这就是我不喜欢查询生成器框架的原因。

于 2013-09-06T07:06:45.687 回答