The Stack Was in C++. The Fix Was in the Query.
When a database query slows down, the engine is an easy suspect. This Milvus case shows how the way an application asks a question can change the work the database performs.
A correct answer at the wrong cost
During performance profiling for a user's POC, we found a query that chained many array_contains predicates with OR. We reproduced that query shape with 200 terms:
array_contains(arr, 0)
OR array_contains(arr, 1)
OR ...
OR array_contains(arr, 199)
They all checked the same ARRAY<INT64> field. The business question was whether the array contained any value from the set.
In the million-row reproduction, the query correctly returned 200,000 matches. With expression optimization off, median latency was 13,698 ms and process CPU averaged 1.334 cores.
The CPU hotspot was ARRAY membership
The unoptimized flamegraph places most CPU samples on the call path from PhyConjunctFilterExpr::Eval to PhyJsonContainsFilterExpr::ExecArrayContains<long>.
Figure 1. Blue marks OR evaluation; purple marks ARRAY membership.
In the user-space CPU sample, 97.43% of stacks passed through ExecArrayContains<long>.
One membership operation instead of 200
Milvus 3.0 can fold compatible array_contains predicates joined by OR on the same ARRAY field. With common.enabledOptimizeExpr enabled, the expression becomes:
array_contains_any(arr, [0, 1, ..., 199])
From seconds to milliseconds
All three cases used the same data and request rate, and each returned 200,000 matches.
| Query | Median latency | Mean process CPU |
|---|---|---|
| A: 200-term OR, optimization off | 13,698 ms | 1.334 cores |
| B: same OR, optimization on | 127.6 ms | 0.0360 cores |
C: explicit array_contains_any, optimization off |
125.6 ms | 0.0367 cores |
Relative to A, B cut mean process CPU by 97.30% and median latency by 99.07%. Its CPU use and latency were close to C's explicit array_contains_any.
How the question shaped the cost
Without folding, Milvus ran a 200-term OR chain; with folding, one set-membership operation. The application asked the same question. The database did not do the same work.
— MrPresent-Han
