Skip to content

BACKPORT [IMP] orm: exists for one2many searches#649

Open
moylop260 wants to merge 1 commit intoVauxoo:18.0from
vauxoo-dev:18.0-field-o2m-exists-moy
Open

BACKPORT [IMP] orm: exists for one2many searches#649
moylop260 wants to merge 1 commit intoVauxoo:18.0from
vauxoo-dev:18.0-field-o2m-exists-moy

Conversation

@moylop260
Copy link

Use the EXISTS clause of SQL instead of IN for one2many fields.

```
WHERE tab."id" IN (SELECT "inverse" FROM cotab WHERE ...)
-- becomes
WHERE EXISTS(SELECT FROM (
SELECT "inverse" AS __inverse FROM cotab WHERE ...
) WHERE __inverse = tab."id")
```

The subselect is here to avoid alias name clashes between the main query
and the subquery. It is trivial and predicates will be pushed down.
Usually postgres can plan better queries for such queries than for IN
queries where it often chooses to materialize the subquery before
performing the IN operation, especially for NOT EXISTS queries.

Backport from odoo@988d59d

Related to issue odoo#211586

@moylop260 moylop260 self-assigned this Feb 4, 2026
    Use the EXISTS clause of SQL instead of IN for one2many fields.

    ```
    WHERE tab."id" IN (SELECT "inverse" FROM cotab WHERE ...)
    -- becomes
    WHERE EXISTS(SELECT FROM (
    SELECT "inverse" AS __inverse FROM cotab WHERE ...
    ) WHERE __inverse = tab."id")
    ```

    The subselect is here to avoid alias name clashes between the main query
    and the subquery. It is trivial and predicates will be pushed down.
    Usually postgres can plan better queries for such queries than for IN
    queries where it often chooses to materialize the subquery before
    performing the IN operation, especially for NOT EXISTS queries.

Backport from odoo@988d59d
Related to issue odoo#211586
@moylop260 moylop260 force-pushed the 18.0-field-o2m-exists-moy branch from efe4fe5 to 9eb1bd0 Compare February 4, 2026 20:40
@moylop260 moylop260 added the PATCH label Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant