Merged
Conversation
carlopi
reviewed
Apr 16, 2025
| std::pop_heap(heap_copy.begin(), heap_copy.end()); | ||
| state.scan_order[heap_copy.size() - 1] = UnsafeNumericCast<sel_t>(heap_copy.back().index); | ||
| heap_copy.pop_back(); | ||
|
|
Contributor
There was a problem hiding this comment.
Just for my understanding, should there not be a sort here? I am not sure I see where the sort happens.
Collaborator
Author
There was a problem hiding this comment.
The sort happens in Finalize(), I can clarify with a comment
Collaborator
|
Thanks! |
krlmlr
added a commit
to duckdb/duckdb-r
that referenced
this pull request
May 18, 2025
Optimize large Top N queries (duckdb/duckdb#17141)
krlmlr
added a commit
to duckdb/duckdb-r
that referenced
this pull request
May 18, 2025
Optimize large Top N queries (duckdb/duckdb#17141)
krlmlr
added a commit
to duckdb/duckdb-r
that referenced
this pull request
May 19, 2025
Optimize large Top N queries (duckdb/duckdb#17141)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With large
ORDER BY ... LIMIT ...queries, at some point, it becomes better to fully sort the data and apply a limit instead of computing a heap. I think @Tmonster is addressing this in a separate PR in the optimizer.Nonetheless, I looked at the
PhysicalTopNoperator and found that some things could be improved, mostly the merging of heaps, which is inefficient if you iterate and dopop_heapandpush_heap. Instead, we are better off just sorting the heap. I also parallelized scanning.I ran this little benchmark to check the performance. Create some data:
Query:
Current
main: ~5.5s and ~7.6sThis PR: ~2.7s and ~3.8s
Without TopN optimization: ~0.25s and ~0.43s
There is still a lot of room for improvement in the
PhysicalTopNoperator for large inputs, but I'm not sure if we should spend too much time on this because we can just use the optimizer to avoid using a TopN and sort the data if N is large, but I figured this was worth the effort. With this input data, I found that the Top N was only better than sorting for limits less than 250k.