具有横向连接的PostgreSQL计划器(大小表)

时间:2019-09-06 02:13:24

标签: postgresql query-performance lateral-join

我相信这两个查询具有相同的输出

我的问题是,计划者为什么不同时测试两个变体并选择最有效的变体?

select n.id, n.n <-> hc.n hdist_n, hc.*
from n 
join lateral (select * from hotelscombined hc order by n.n <-> hc.n limit 5)hc on (hc.cc = n.u) 
order by 1;
select n.id, n.n <-> hc.n hdist_n, hc.*
from hotelscombined hc                     
join lateral (select * from n order by n.n <-> hc.n limit 5)n on (hc.cc = n.u) 
order by 1;
                                                        QUERY PLAN                                                         
---------------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.71..2466952.10 rows=515552 width=83)
   ->  Index Scan using n_pkey on n  (cost=0.43..1199539.45 rows=2181374 width=17)
   ->  Subquery Scan on hc  (cost=0.28..0.57 rows=1 width=75)
         Filter: (n.u = hc.cc)
         ->  Limit  (cost=0.28..0.51 rows=5 width=79)
               ->  Index Scan using hotelscombined_n_idx1 on hotelscombined hc_1  (cost=0.28..3906.32 rows=85687 width=79)
                     Order By: (n <-> n.n)
(7 rows)

Time: 4.814 ms
                                                 QUERY PLAN                                                  
-------------------------------------------------------------------------------------------------------------
 Sort  (cost=287016.55..287067.18 rows=20252 width=83)
   Sort Key: n.id
   ->  Nested Loop  (cost=0.41..285567.95 rows=20252 width=83)
         ->  Seq Scan on hotelscombined hc  (cost=0.00..2027.87 rows=85687 width=75)
         ->  Subquery Scan on n  (cost=0.41..3.30 rows=1 width=17)
               Filter: (hc.cc = n.u)
               ->  Limit  (cost=0.41..3.24 rows=5 width=3749)
                     ->  Index Scan using n_n_idx1 on n n_1  (cost=0.41..1231791.79 rows=2181374 width=3749)
                           Order By: (n <-> hc.n)
(9 rows)

Time: 7.817 ms

0 个答案:

没有答案