发布

  • [OPIK-7076] [BE] Enable prefilter CTE for feedback score filtered queries (#7360)

    frostbyte_neo 发布于 2026-07-06 13:09:13 +00:00

    The trace_id_prefilter / span_id_prefilter CTEs narrow downstream
    feedback score scans by first finding trace/span IDs that match
    trace-column filters (name, tags, etc.) using the primary key.

    Previously, the prefilter was disabled whenever feedback score filters
    were present — an overly cautious guard introduced for a guardrails-
    specific bug (guardrails filter SQL references a CTE alias that doesn't
    exist in the prefilter). Feedback score filters use separate template
    variables and never interfere with the prefilter.

    Changes:

    • TraceDAO: Remove feedback score exclusion from shouldUseTraceIdPrefilter,
      keep guardrails guard. Add prefilter CTE to COUNT_BY_PROJECT_ID with IN
      predicates for feedback_scores, guardrails, annotation_queues, target_spans.
      Activate prefilter in countTotal().
    • SpanDAO: Same fix for shouldUseSpanIdPrefilter. Add prefilter CTE to
      COUNT_BY_PROJECT_ID inside the feedback score WITH block. Activate
      prefilter in countTotal().

    Measured on 50K traces / 240K feedback scores:

    • Trace count: 634-725ms → 57-167ms (4-12x faster)
    • Trace listing: 1.3-1.8s → 370-700ms (2-4x faster)
    • Span count: 52-257ms → 21-56ms (3-5x faster)
    • Span listing: 250-344ms → 88-120ms (~3x faster)
    下载附件