Skip to content

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

Closed

Description

@cty-ut

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • Please do not modify this template :) and fill in all the required fields.

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Loading a document's segment list runs a fixed number of queries per segment, so the query count grows linearly with the page size.

For every segment, the list serializer calls get_segment_child_chunks (which loads the segment's Document, then its DatasetProcessRule, then its ChildChunk rows) and get_segment_attachments. All segments on the page belong to the same document, so the document and process rule queries return the same rows every time.

Measured on the unit-test SQLite database with a hierarchical document where every segment has one child chunk and one attachment:

Path 1 segment 20 segments
Console list_segments 7 queries 83 queries
Service API load_segment_details 4 queries 80 queries

At the maximum page size of 100, a hierarchical document issues about 400 extra queries per page.

2. Additional context or comments

Affected paths:

  • Console: SQLAlchemySegmentRepository.list_segments → _segment_data
  • Service API: load_segment_details → load_segment_detail

Proposal: load a page's child chunks and attachments with a bounded number of queries, the same way #42279 did for the dataset list and load_document_read_batch does for the document list. I have a PR ready for this.

3. Can you help us with this feature?

  • I am interested in contributing to this feature.

Activity

  1. added
    💪 enhancement
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    💪 enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions