Skip to content

Conversation

@carlesarnal
Copy link
Member

@carlesarnal carlesarnal commented Jan 2, 2026

Summary

  • Optimizes search queries by using window functions (COUNT(*) OVER()) to combine result and count queries into a single database query
  • Keeps EXISTS subqueries for label filtering (compatible with all database backends)
  • Adds composite indexes on label tables for PostgreSQL, SQL Server, and H2 to improve query performance

Root Cause

Search operations were executing two separate queries: one for fetching results and one for counting total matches. This doubled the database round-trips for every search operation.

Changes

  • Created new mapper classes for handling window function results (SearchedArtifactWithCountMapper, SearchedVersionWithCountMapper, SearchedGroupWithCountMapper)
  • Extended SqlStatements interface with selectWithCountWindowTemplate() method
  • Added database-specific SQL statement overrides for PostgreSQL, SQL Server, MySQL, and H2
  • Refactored searchArtifacts(), searchVersions(), and searchGroups() methods to use single combined queries
  • DDL upgrade scripts for v102 schema with composite indexes (PostgreSQL, SQL Server, H2)
  • Note: MySQL does not include composite indexes due to the 3072-byte key length limit

Test plan

  • Existing unit tests pass
  • Integration tests pass on all storage variants (SQL, SQL Server, KafkaSQL)
  • Build completes successfully

Closes #7010

@carlesarnal carlesarnal force-pushed the issue-7010-search-query-optimization branch 3 times, most recently from 6902f30 to 234bf56 Compare January 7, 2026 11:42
This commit implements Phase 1 of the search query optimization work for issue Apicurio#7010.

Key optimizations:
- Use window functions (COUNT(*) OVER()) to combine result and count queries into a single query
- Keep EXISTS subqueries for label filtering (compatible with all database backends)
- Add composite indexes on label tables for PostgreSQL, SQL Server, and H2

Changes include:
- New mapper classes for handling window function results (SearchedArtifactWithCountMapper,
  SearchedVersionWithCountMapper, SearchedGroupWithCountMapper)
- Extended SqlStatements interface with window function template methods
- Refactored searchArtifacts(), searchVersions(), and searchGroups() methods
- DDL upgrade scripts for v102 schema with composite indexes
- Note: MySQL does not include composite indexes due to 3072-byte key length limit

Closes Apicurio#7010
@carlesarnal carlesarnal force-pushed the issue-7010-search-query-optimization branch from 234bf56 to d72b745 Compare January 7, 2026 13:10
@carlesarnal carlesarnal merged commit f6070f2 into Apicurio:main Jan 8, 2026
30 checks passed
@carlesarnal carlesarnal deleted the issue-7010-search-query-optimization branch January 8, 2026 07:23
carlesarnal added a commit that referenced this pull request Jan 8, 2026
carlesarnal added a commit that referenced this pull request Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Search Improvements] Phase 1: Query Optimization

1 participant