Skip to content

Joinload - an alternate to preload for large databases that encounter the postgres 65,535 limit in the "IN" clause #7532

@vacaramin

Description

@vacaramin

Describe the feature

Add a JoinLoad method as an alternative to Preload that uses SQL joins instead of IN (...) queries. This would allow GORM to load related records using JOIN clauses directly, which avoids hitting PostgreSQL’s 65,535 parameter limit and improves performance for large datasets.

Example usage:

db.JoinLoad("Orders").Find(&users)

This should load users and their related Orders using a single LEFT JOIN, and hydrate the result into the nested struct just like Preload does.

It should also support nested associations like:

db.JoinLoad("Orders.Items").Find(&users)

Motivation

When working with large databases, Preload can generate queries that include thousands of IDs in an IN clause. PostgreSQL has a hard limit of 65,535 parameters in a query. This makes Preload unusable or unstable at scale.

A join-based alternative solves this problem by:

  • Using SQL joins instead of IN clauses
  • Reducing the number of queries
  • Preventing query crashes due to parameter limits
  • Performing better on larger datasets

It also aligns with how other ORMs like SQLAlchemy (joinedload) and Ent (WithX) handle related data loading efficiently.

Related Issues

#7497

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions