dotnet / dotnet/efcore

Consider lifting up correlated scalar subqueries to JOINs where possible, for better query performance

Open
#34,398 0 comments 1 reaction 0 assignees View on GitHub
area-perf area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

For example, this would transform the following:

```sql
SELECT a.col1, (SELECT b.col2 FROM b WHERE b.x = a.x)
FROM a;
```

... to the following:

```sql
SELECT a.col1, b.col2
FROM a LEFT JOIN b ON b.x = a.x;
```

Notes:
* Check that this makes sense for other databases as well (likely)
* There's a slight semantic difference between the two: the former errors when the scalar subquery returns more than one row, but the latter returns multiple rows (JOIN). We can perform this transformation e.g. only in cases where we're sure that the subquery returns one row (e.g. there's LIMIT 1 inside, or there's a predicate over a uniquely-constrained column).

See https://www.cybertec-postgresql.com/en/subqueries-and-performance-in-postgresql for more details

/cc @laurenz (post author)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.