0

I have tables

dn_table ~ 10_000 rows

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

sr_table ~ 1m rows

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

And I stuck with a lateral join query for them. It has extremely bad performance some queries are running for hours depending on dataset size while selecting with the limit.

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"

I've tried to use correlated subquery for them but it brings me results that I don't expect.

Thanks in advance!

cudouny
  • 124
  • 7

1 Answers1

1

A correlated subquery and lateral join should return the same results.

But for your lateral join, you want an index on sr_table(SR). You might also want an order by, but that is a semantic issue, not a performance issue.

If you do add an order by, you'll want to include those columns in the index as well.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786