Sasha
513ba636af
fix(db-postgres): ensure countDistinct works correctly and achieve better performance when the query has table joins ( #11208 )
...
The fix, added in https://github.com/payloadcms/payload/pull/11096
wasn't sufficient enough. It did handle the case when the same query
path / table was joined twice and caused incorrect `totalDocs`, but it
didn't handle the case when `JOIN` returns more than 1 rows, which 2
added new assertions here check.
Now, we use `COUNT(*)` only if we don't have any joined tables. If we
do, instead of using `SELECT (COUNT DISTINCT id)` which as described in
the previous PR is _very slow_ for large tables, we use the following
query:
```sql
SELECT COUNT(1) OVER() as count -- window function, executes for each row only once
FROM users
LEFT JOIN -- ... here additional rows are added
WHERE -- ...
GROUP BY users.id -- this ensures we're counting only users without additional rows from joins.
LIMIT 1 -- Since COUNT(1) OVER() executes and resolves before doing LIMIT, we can safely apply LIMIT 1.
```
2025-02-16 14:08:08 +02:00
..
2025-02-14 00:08:20 +00:00
2024-12-28 05:28:37 +00:00
2025-02-14 07:47:00 -05:00
2024-11-27 20:36:37 +00:00
2024-11-16 15:30:05 -05:00
2024-11-27 20:36:37 +00:00
2024-12-28 05:28:37 +00:00
2024-12-28 05:28:37 +00:00
2025-02-14 00:08:20 +00:00
2025-01-17 09:42:46 -05:00
2025-01-21 02:18:13 +02:00
2024-11-27 20:36:37 +00:00
2024-09-20 22:46:40 -04:00
2024-12-18 22:44:04 -05:00
2025-02-10 16:29:08 -05:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-12-28 05:28:37 +00:00
2024-11-27 20:36:37 +00:00
2025-02-14 14:45:55 -05:00
2025-02-11 13:20:55 -05:00
2024-11-27 20:36:37 +00:00
2024-12-13 15:03:57 -05:00
2024-12-16 17:22:17 +02:00
2025-02-14 00:08:20 +00:00
2025-01-29 13:27:00 -05:00
2024-11-27 20:36:37 +00:00
2024-10-30 17:56:50 +00:00
2025-02-14 12:23:49 -05:00
2025-01-13 14:51:26 +00:00
2025-02-14 00:08:20 +00:00
2024-08-13 12:54:33 -04:00
2025-02-12 14:48:12 -05:00
2024-12-19 22:22:43 -05:00
2025-01-14 21:17:25 +00:00
2024-12-10 08:42:23 -05:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-12-31 09:14:56 -05:00
2024-12-28 05:28:37 +00:00
2025-01-24 10:10:49 -05:00
2025-01-27 13:28:15 +00:00
2024-11-27 20:36:37 +00:00
2025-02-05 10:14:17 -05:00
2024-11-27 20:36:37 +00:00
2024-12-19 22:22:43 -05:00
2024-12-19 22:22:43 -05:00
2024-11-27 20:36:37 +00:00
2025-01-14 22:38:31 -05:00
2025-02-16 14:08:08 +02:00
2024-07-23 13:44:44 -04:00
2024-12-21 07:42:44 -05:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-11-27 20:36:37 +00:00
2024-12-16 21:40:22 +00:00
2025-02-13 18:11:31 +02:00
2025-02-13 21:42:52 +00:00
2025-02-10 19:48:52 -05:00
2024-07-11 15:59:38 -04:00
2024-05-25 15:45:05 +00:00
2025-01-24 01:58:45 +00:00
2024-03-07 11:33:46 -05:00
2025-02-14 00:08:20 +00:00
2024-12-31 09:14:56 -05:00
2025-02-07 03:54:26 +00:00
2024-12-20 20:13:28 +00:00
2024-12-19 11:08:17 -05:00
2024-08-13 12:54:33 -04:00
2024-11-11 19:28:55 -07:00
2024-09-16 17:02:08 +00:00
2025-01-17 09:16:29 -05:00
2024-09-16 17:02:08 +00:00
2024-11-11 13:59:05 -05:00
2024-10-11 18:54:39 +00:00
2025-02-08 23:25:00 +00:00
2024-10-11 18:54:39 +00:00
2024-12-17 14:49:29 -05:00
2024-11-11 13:59:05 -05:00
2025-02-10 15:02:53 -05:00
2024-05-01 17:35:41 -04:00
2025-02-07 03:24:49 +00:00
2025-02-14 00:08:20 +00:00
2024-08-14 08:57:04 -04:00
2024-08-14 08:57:04 -04:00
2024-10-14 20:02:26 +00:00
2025-01-15 14:47:46 -05:00
2024-04-06 15:06:04 -04:00
2024-06-17 14:25:36 -04:00
2025-02-14 00:08:20 +00:00
2024-03-14 23:53:47 -04:00
2025-01-14 20:00:00 -03:00
2025-01-14 20:00:00 -03:00