1. 程式人生 > 其它 >postgresql 執行計劃 - 巢狀查詢

postgresql 執行計劃 - 巢狀查詢

1.nested loop join:

對左表中找到的每一行都要掃描右表一次。

這種策略最容易實現但是可能非常耗時

(不過,如果右表可以通過索引掃描,這將是一個不錯的策略。因為可以用左表當前行的值來作為右表上索引掃描的鍵)。

參考:(2條訊息) PostgreSQL表連線 nestloop/hash/merge join詳解_Focus on PostgreSQL-CSDN部落格

2. 例子:–nested loop join:

nl連線大致過程為:1、t2表進行掃描, 過濾條件info = ‘bill’;

2、t2表根據過濾條件輸出的中間結果, 每一條中間結果, t1表都根據索引idx_t1_id掃一遍, 過濾條件id= t2.id。
bill@bill=>explain select * from t2 join t1 on (t1.id=t2.id) where t2.info='bill';
QUERY PLAN
--------------------------------------------------------------------------- Nested Loop (cost=0.29..1793.92 rows=1 width=26) -> Seq Scan on t2 (cost=0.00
..1791.00 rows=1 width=13) Filter: (info = 'bill'::text) -> Index Scan using idx_t1_id on t1 (cost=0.29..2.91 rows=1 width=13) Index Cond: (id = t2.id) (5 rows)
用一個例子來演示會更加清晰