簡體   English   中英

PostgreSQL 橫向連接性能不佳

[英]PostgreSQL poor lateral join performance

我有桌子

dn_table ~ 10_000 rows

|  DN  |
--------
| 1234 |
| 1235 |
| .... |

sr_table ~ 1m rows

|  SR  |
--------
| 2345 |
| 2346 |
| .... |

我堅持對他們進行橫向連接查詢。 它具有極其糟糕的性能,某些查詢在選擇限制時根據數據集大小運行數小時。

select
       *
from (
     select
            "alias1"."DN",
            "alias2"."SR"
     from (
          select "alias1"."DN"
          from "dn_table" as "alias1"
          ) as "alias1" left outer join lateral (
             select *
             from "sr_table" as "alias2"
             where "alias1"."DN" = "alias2"."SR"
             limit 1
         ) as "alias2" on true
     ) as "alias"

我嘗試為它們使用相關子查詢,但它給我帶來了我不期望的結果。

提前致謝!

相關子查詢和橫向連接應該返回相同的結果。

但是對於橫向連接,您需要sr_table(SR)上的索引。 您可能還需要order by ,但這是語義問題,而不是性能問題。

如果確實添加了order by ,則還需要將這些列包含在索引中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM