drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Support MySQL STRAIGHT_JOIN (force join order)

Open
#6,019 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

MySQL supports `STRAIGHT_JOIN`, which is identical to `INNER JOIN` except that it forces the optimizer to read the tables in the order they appear in the `FROM`/join clause ([docs](https://dev.mysql.com/doc/refman/8.0/en/join.html)). There is currently no way to emit it from the query builder — `.innerJoin()` / `.leftJoin()` always produce plain `JOIN` / `LEFT JOIN`, and there is no hook for join-order optimizer hints.

Proposed API, mirroring the existing join methods:

```ts
db.select()
.from(userActivity)
.straightJoin(user, eq(user.id, userActivity.userId))
.innerJoin(profile, eq(profile.userId, user.id))
// ...
```

### Use case

For some queries the MySQL optimizer picks a poor driving table and no amount of indexing convinces it otherwise. A common case is a feed/listing query where the filter and the sort come from different tables: without a forced order the optimizer scans the wrong table first and does a large filesort, while starting from the correct table lets an index provide the order and short-circuit at `LIMIT`.

Today the only workaround is to drop the whole statement to raw `sql`, which loses the builder's typed result inference and forces re-declaring the join graph and projection by hand.

### Additional notes

- `STRAIGHT_JOIN` is MySQL-only, so it would live on the MySQL dialect builder alongside the other join methods.
- It is part of the MySQL join grammar (`table_reference {[INNER | CROSS] JOIN | STRAIGHT_JOIN} table_factor`), so semantics beyond join order match `INNER JOIN` exactly.

Contributor guide

Open the contributing guide

Research direction

Start with the MySQL dialect builder and its existing innerJoin and leftJoin methods to understand their API and SQL output. Add the proposed straightJoin entry point alongside them, then verify that it emits MySQL STRAIGHT_JOIN, preserves typed result inference, and matches INNER JOIN semantics apart from join order.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, typescript
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.