Skip to content

app/vlstorage: extend top-N optimization to support sort_direction=asc#1355

Open
cuongleqq wants to merge 6 commits into
VictoriaMetrics:masterfrom
cuongleqq:924-logsql-query-sort-direction
Open

app/vlstorage: extend top-N optimization to support sort_direction=asc#1355
cuongleqq wants to merge 6 commits into
VictoriaMetrics:masterfrom
cuongleqq:924-logsql-query-sort-direction

Conversation

@cuongleqq
Copy link
Copy Markdown
Contributor

Summary:

This PR adds sort_direction query arg to /select/logsql/query. When limit=N is specified, passing sort_direction=asc returns the N oldest matching log entries.

Before, limit=N always used a binary-search optimization on the time range to efficiently return the last N logs without a full scan. This PR generalizes that optimization to support both directions.

  • It changes the name of most functions related to lastN, to timeSortedN, to mean "both directions"
  • It uses a struct timeSortedRangeOps to generalize the operation on the time range
  • Most of other changes are just passing isDesc around to specify the sort directions

Closes #924

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 59 files

@cuongleqq cuongleqq changed the title 924 add query sort_direction app/vlstorage: extend top-N optimization to support sort_direction=asc Apr 27, 2026
Comment thread app/vlstorage/nresultsoptimization.go Outdated
Comment thread lib/logstorage/pipe.go
@cuongleqq
Copy link
Copy Markdown
Contributor Author

cuongleqq commented May 1, 2026

@func25 thanks for the comments.

I changed the asc and desc struct to 2 helpers. I think it simplifies the code flow.

I also changed everything "last n" to "time sorted n results". I actually struggle with the naming for this, but I don't have a better name 🙃.

@cuongleqq cuongleqq force-pushed the 924-logsql-query-sort-direction branch from acade6a to 2c87bfc Compare May 11, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/select/logsql/query: add support for sort_direction query arg

2 participants