发布

  • [OPIK-4334] [BE] Optimize SpanDAO and TraceDAO queries with target project_ids extraction (#5151)

    frostbyte_neo 发布于 2026-02-11 08:39:29 +00:00

    • [OPIK-4334] [BE] Optimize SpanDAO and TraceDAO queries with target project_ids extraction

    Extract target_projects CTE to separate queries to reduce ClickHouse table scans.
    Instead of using CTEs that execute on each reference, we now:

    1. Execute a separate query to get target project_ids
    2. Pass project_ids as parameters with conditional binding
    3. Use StringTemplate conditionals for query flexibility

    This optimization pattern reduces multiple table scans in complex queries.

    • [OPIK-4334] [BE] Fix SELECT_BY_IDS query referencing undefined target_projects CTE

    The SELECT_BY_IDS query referenced a non-existent 'target_projects' CTE
    in the spans and comments subqueries. This caused ClickHouse to fail
    when trying to retrieve spans by IDs.

    Changed lines 674 and 690 from:
    AND project_id IN (SELECT project_id FROM target_projects)
    to:
    <if(has_target_projects)>AND project_id IN :target_project_ids

    This makes the query consistent with the optimization pattern used in
    the feedback_scores CTEs and works correctly with the getByIds method.

    下载附件