Skip to content

perf(api): batch segment list child chunks and attachments - #45050

Merged
JohnJyong merged 1 commit into
langgenius:mainfrom
cty-ut:perf/batch-segment-list-relations
Oct 29, 2026
Merged

JohnJyong merged 1 commit into
langgenius:mainfrom
cty-ut:perf/batch-segment-list-relations

Conversation

@cty-ut

@cty-ut cty-ut commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #45048
Segment lists loaded child chunks and attachments one segment at a time, so the query count grew with the page size. This PR loads them for the whole page with a fixed number of queries.

  • Added SegmentReadBatch and load_segment_read_batch in dataset_read_repository.py. For a page of segments, it loads their documents, process rules, child chunks and attachment files with one IN query each, keeping the existing tenant / dataset / document ownership conditions.
  • Console (list_segments, _detail) and Service API (load_segment_details, load_segment_detail) now read child chunks and attachments from the batch. load_segment_detail keeps its signature for the existing controller callers and builds a single-segment batch when none is passed.
  • Child chunks are still returned only for hierarchical documents whose parent_mode is not full-doc, ordered by position, the same as before.

Not changed in this PR: sign_segment_content still queries per segment when the content contains file links. That path applies file access control, so I'd prefer to batch it separately.

Screenshots

Queries per page load, hierarchical document, one child chunk and one attachment per segment:

Path Before (1 / 20 segments) After (1 / 20 segments)
Console list_segments 7 / 83 7 / 7
Service API load_segment_details 4 / 80 4 / 4

Tests

  • test_list_segments_query_count_is_bounded_for_hierarchical_documents and test_segment_page_load_is_bounded_for_hierarchical_documents: the query count for 20 segments equals the count for 1. Both fail without this change.
  • test_segment_read_batch_assigns_relations_to_their_segments: child chunks are ordered by position and assigned to their own segment, and an attachment from another tenant is excluded.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 80.31% 80.31% +0.00%
Strict coverage 80.00% 80.00% +0.00%
Typed symbols 77,394 77,404 +10
Untyped symbols 19,052 19,052 0
Modules 3990 3990 0

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.11111% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.55%. Comparing base (9e978de) to head (9e6348c).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
.../repositories/knowledge/dataset_read_repository.py 93.75% 0 Missing and 2 partials ⚠️
api/services/knowledge/dataset_read_service.py 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45050      +/-   ##
==========================================
- Coverage   89.55%   89.55%   -0.01%     
==========================================
  Files        6013     6013              
  Lines      377204   377239      +35     
  Branches    76362    76371       +9     
==========================================
+ Hits       337811   337840      +29     
- Misses      33400    33405       +5     
- Partials     5993     5994       +1     
Flag Coverage Δ
api 89.41% <91.11%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JohnJyong JohnJyong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found in 9e6348c. Reviewed the console and Service API list/detail paths, relation ownership filters, child-chunk ordering, and full-document behavior.

Validation: 173 related repository, controller, and response-schema tests passed. Seven additional local checks passed for empty input, iterator input, single-detail responses, and hierarchical/automatic/custom processing modes. Ruff lint/format checks and scoped Pyrefly checks passed. The CI checks for this revision are green.

@JohnJyong
JohnJyong added this pull request to the merge queue Oct 29, 2026
Merged via the queue into langgenius:main with commit a709c29 Oct 29, 2026
47 checks passed
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segment list issues several queries per segment (N+1) for child chunks and attachments

2 participants