Commit 37809f6
authored
fix: optimize translation/key count queries for branching (#3485)
## Summary
- Rewrites `getTranslationCount`, `getProjectKeyCount`, and
`getKeyCount` in `OrganizationStatsService` to use native SQL, correctly
deduplicating logical keys across branches (same key name + namespace
can exist in multiple branch rows)
- `getTranslationCount` now uses `SELECT DISTINCT` with an `IN` subquery
for projects and `EXISTS` for deleted-language filtering — avoids 2 386
individual translation index scans in favour of a single sequential scan
(13× fewer buffer accesses)
- Adds a Liquibase migration that creates
`idx_key_project_name_namespace` on `key(project_id, name,
namespace_id)` and runs `ANALYZE`, enabling the planner to use
incremental sort instead of a full 3.7 MB quicksort (reduces DB
execution time from ~54 ms → ~25 ms)
- Adds `OrganizationStatsServiceTest` covering unique key/translation
counting across branches, shared keys, namespaces, and empty
translations
## Test plan
- [ ] `OrganizationStatsServiceTest` — all 4 tests pass
- [ ] Hit `GET /v2/organizations/:id/usage` on a local instance with
branching data and verify response time improvement
- [ ] Verify migration applies cleanly (`CREATE INDEX CONCURRENTLY` is
non-blocking)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Chores**
* Improved organization statistics retrieval for more efficient and
reliable counting of keys and translations.
* Added a concurrent database index and post-index analyze to speed
related queries.
* Adjusted result handling to ensure accurate zero/number returns when
no data is present.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 11d45fa commit 37809f6
2 files changed
Lines changed: 44 additions & 16 deletions
File tree
- backend/data/src/main
- kotlin/io/tolgee/service/organization
- resources/db/changelog
Lines changed: 34 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
52 | 64 | | |
53 | 65 | | |
54 | | - | |
| 66 | + | |
| 67 | + | |
55 | 68 | | |
56 | 69 | | |
57 | 70 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
67 | 85 | | |
68 | 86 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5078 | 5078 | | |
5079 | 5079 | | |
5080 | 5080 | | |
| 5081 | + | |
| 5082 | + | |
| 5083 | + | |
| 5084 | + | |
| 5085 | + | |
| 5086 | + | |
| 5087 | + | |
| 5088 | + | |
| 5089 | + | |
| 5090 | + | |
5081 | 5091 | | |
0 commit comments