Skip to content

Commit 8520fd9

Browse files
authored
fix(drizzle): optimize count querying when no joins (#7749)
Closes #6321 To run benchmark: `git checkout b840222` - from r1tsuu/payload b840222 `pnpm dev:postgres _community` Benchmark results: (Before / After) Postgres 400 000 rows: ![image](https://github.com/user-attachments/assets/cd7c478f-2057-4c7c-adec-5dbf0b05ec7b) Postgres 2 000 000 rows: ![image](https://github.com/user-attachments/assets/04224f95-77eb-42ab-9591-887b197c597a) SQLite 400 000 rows: ![image](https://github.com/user-attachments/assets/ba7482c2-30f1-4498-892d-59710639a7b3) SQLite 2 000 000 rows: ![image](https://github.com/user-attachments/assets/c0a889f8-8e21-4b98-ac92-65ac735b8b32) ## Description <!-- Please include a summary of the pull request and any related issues it fixes. Please also include relevant motivation and context. --> - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change <!-- Please delete options that are not relevant. --> - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] Existing test suite passes locally with my changes - See #7749 (comment)
1 parent b7a0b15 commit 8520fd9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

packages/db-sqlite/src/countDistinct.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ChainedMethods } from '@payloadcms/drizzle/types'
22

33
import { chainMethods } from '@payloadcms/drizzle'
4-
import { sql } from 'drizzle-orm'
4+
import { count, sql } from 'drizzle-orm'
55

66
import type { CountDistinct, SQLiteAdapter } from './types.js'
77

@@ -22,8 +22,11 @@ export const countDistinct: CountDistinct = async function countDistinct(
2222
methods: chainedMethods,
2323
query: db
2424
.select({
25-
count: sql<number>`count
26-
(DISTINCT ${this.tables[tableName].id})`,
25+
count:
26+
joins.length > 0
27+
? sql`count
28+
(DISTINCT ${this.tables[tableName].id})`.mapWith(Number)
29+
: count(),
2730
})
2831
.from(this.tables[tableName])
2932
.where(where),

packages/drizzle/src/postgres/countDistinct.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sql } from 'drizzle-orm'
1+
import { count, sql } from 'drizzle-orm'
22

33
import type { ChainedMethods, TransactionPg } from '../types.js'
44
import type { BasePostgresAdapter, CountDistinct } from './types.js'
@@ -22,8 +22,11 @@ export const countDistinct: CountDistinct = async function countDistinct(
2222
methods: chainedMethods,
2323
query: (db as TransactionPg)
2424
.select({
25-
count: sql<string>`count
26-
(DISTINCT ${this.tables[tableName].id})`,
25+
count:
26+
joins.length > 0
27+
? sql`count
28+
(DISTINCT ${this.tables[tableName].id})`.mapWith(Number)
29+
: count(),
2730
})
2831
.from(this.tables[tableName])
2932
.where(where),

0 commit comments

Comments
 (0)