Skip to content

⚡ Optimize getProductsByCollection with secondary index#50

Merged
AJFrio merged 1 commit intomainfrom
perf-optimize-kv-collection-index-17260775420947353121
Feb 9, 2026
Merged

⚡ Optimize getProductsByCollection with secondary index#50
AJFrio merged 1 commit intomainfrom
perf-optimize-kv-collection-index-17260775420947353121

Conversation

@AJFrio
Copy link
Copy Markdown
Owner

@AJFrio AJFrio commented Feb 9, 2026

Implemented a secondary index for products by collection ID in the KV store. This optimization significantly reduces the number of KV read operations and improves the performance of the getProductsByCollection method, avoiding a full scan of all products. All lifecycle operations (create, update, delete) for products and collections are updated to maintain the index's integrity. Verified with a benchmark script showing a 90% reduction in KV calls for a typical scenario.


PR created automatically by Jules for task 17260775420947353121 started by @AJFrio

Replaces the inefficient full-scan in getProductsByCollection with a
secondary index mapping collection IDs to product IDs.

- Implemented index updates in createProduct, updateProduct, and deleteProduct.
- Added index cleanup in deleteCollection.
- Re-implemented getProductsByCollection to use the new index.
- Updated test helpers to maintain the index in tests.
- Added a benchmark script showing a 90% reduction in KV calls.

Performance impact (100 products in target collection out of 1000 total):
- KV Get calls: 1001 -> 101 (90% reduction)
- Execution time: 5.13ms -> 0.44ms (on mock KV)

Co-authored-by: AJFrio <20246916+AJFrio@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24cbd68af4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 177 to +182
async getProductsByCollection(collectionId) {
const allProducts = await this.getAllProducts()
return allProducts.filter(product => product.collectionId === collectionId)
const collKey = `collection:products:${collectionId}`
const productIds = await this.namespace.get(collKey)
if (!productIds) return []

const ids = JSON.parse(productIds)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Handle legacy data when collection index missing

If a collection’s index key doesn’t exist (e.g., products created before this change or after a partial restore), getProductsByCollection now returns [] instead of the matching products. This is a behavior regression from the previous full-scan implementation and will surface in any environment with pre-existing KV data that hasn’t been reindexed. Consider falling back to a scan or rebuilding the index when collection:products:${collectionId} is absent to preserve correctness.

Useful? React with 👍 / 👎.

@AJFrio AJFrio merged commit c48ab82 into main Feb 9, 2026
1 check passed
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.

1 participant