Skip to content

Ensure ordinal builder emit ordinal blocks #127949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2025

Conversation

dnhatn
Copy link
Member

@dnhatn dnhatn commented May 9, 2025

Currently, if a field has high cardinality, we may mistakenly disable emitting ordinal blocks. For example, with 10,000 tsid values, we never emit ordinal blocks during reads, even though we could emit blocks for 10 tsid values across 1,000 positions. This bug disables optimizations for value aggregation and block hashing.

This change tracks the minimum and maximum seen ordinals and uses them as an estimate for the number of ordinals. However, if a page contains ord=1 and ord=9999, ordinal blocks still won't be emitted. Allocating a bitset or an array for value_count could track this more accurately but would require additional memory. I need to think about this trade off more before opening another PR to fix this issue completely.

This is a quick, contained fix that significantly speeds up time-series aggregation (and other queries too).

The execution time of this query is reduced from 3.4s to 1.9s with 11M documents.

POST /_query
{
    "profile": true,
    "query": "TS metrics-hostmetricsreceiver.otel-default | STATS cpu = avg(avg_over_time(`metrics.system.cpu.load_average.1m`)) BY host.name, BUCKET(@timestamp, 5 minute)"
}
"took": 3475,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167
"took": 1965,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167

@elasticsearchmachine
Copy link
Collaborator

Hi @dnhatn, I've created a changelog YAML for you.

@dnhatn dnhatn marked this pull request as ready for review May 9, 2025 04:04
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label May 9, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Copy link
Member

@martijnvg martijnvg left a comment

Choose a reason for hiding this comment

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

LGTM - I think keeping track of min and max ord is an improved heuristic whether to build ordinals block.

@@ -55,7 +59,7 @@ public SingletonOrdinalsBuilder endPositionEntry() {
}

BytesRefBlock buildOrdinal() {
int valueCount = docValues.getValueCount();
int valueCount = maxOrd - minOrd + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be changed? The values are not guaranteed to be dense, iiuc?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is the estimate. I will work on on a follow-up to resolve this completely.

@dnhatn dnhatn mentioned this pull request May 9, 2025
16 tasks
@dnhatn dnhatn added the auto-backport Automatically create backport pull requests when merged label May 9, 2025
@dnhatn dnhatn merged commit 2ea23da into elastic:main May 9, 2025
17 checks passed
@dnhatn dnhatn deleted the fix-ordinal-reader branch May 9, 2025 16:03
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.19
8.18

dnhatn added a commit to dnhatn/elasticsearch that referenced this pull request May 9, 2025
Currently, if a field has high cardinality, we may mistakenly disable 
emitting ordinal blocks. For example, with 10,000 `tsid` values, we
never emit ordinal blocks during reads, even though we could emit blocks
for 10 `tsid` values across 1,000 positions. This bug disables
optimizations for value aggregation and block hashing.

This change tracks the minimum and maximum seen ordinals and uses them 
as an estimate for the number of ordinals. However, if a page contains
`ord=1` and `ord=9999`, ordinal blocks still won't be emitted.
Allocating a bitset or an array for `value_count` could track this more
accurately but would require additional memory. I need to think about
this trade off more before opening another PR to fix this issue
completely.

This is a quick, contained fix that significantly speeds up time-series 
aggregation (and other queries too).

The execution time of this query is reduced from 3.4s to 1.9s with 11M documents.

```
POST /_query
{
    "profile": true,
    "query": "TS metrics-hostmetricsreceiver.otel-default
            | STATS cpu = avg(avg_over_time(`metrics.system.cpu.load_average.1m`)) BY host.name, BUCKET(@timestamp, 5 minute)"
}
```

```
"took": 3475,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167
```

```
"took": 1965,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167
```
dnhatn added a commit to dnhatn/elasticsearch that referenced this pull request May 9, 2025
Currently, if a field has high cardinality, we may mistakenly disable 
emitting ordinal blocks. For example, with 10,000 `tsid` values, we
never emit ordinal blocks during reads, even though we could emit blocks
for 10 `tsid` values across 1,000 positions. This bug disables
optimizations for value aggregation and block hashing.

This change tracks the minimum and maximum seen ordinals and uses them 
as an estimate for the number of ordinals. However, if a page contains
`ord=1` and `ord=9999`, ordinal blocks still won't be emitted.
Allocating a bitset or an array for `value_count` could track this more
accurately but would require additional memory. I need to think about
this trade off more before opening another PR to fix this issue
completely.

This is a quick, contained fix that significantly speeds up time-series 
aggregation (and other queries too).

The execution time of this query is reduced from 3.4s to 1.9s with 11M documents.

```
POST /_query
{
    "profile": true,
    "query": "TS metrics-hostmetricsreceiver.otel-default
            | STATS cpu = avg(avg_over_time(`metrics.system.cpu.load_average.1m`)) BY host.name, BUCKET(@timestamp, 5 minute)"
}
```

```
"took": 3475,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167
```

```
"took": 1965,
"is_partial": false,
"documents_found": 11368089,
"values_loaded": 34248167
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged backport pending >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.2 v8.19.0 v9.0.2 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants