`order by <sorted column> DESC limit N` queries are not optimised at segment level
See original GitHub issueA query like
SELECT *
FROM table
WHERE filter
ORDER BY sorted_col DESC
LIMIT 10;
is not optimised and hits the generic SelectionOrderByOperator.computePartiallyOrdered()
which inserts up to 10k elements into a priority queue to discard all but the last 10. For ascending order, the query below is roughly 3-4x faster per segment.
SELECT *
FROM table
WHERE filter
ORDER BY sorted_col ASC
LIMIT 10;
Issue Analytics
- State:
- Created a year ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
MySQL ORDER BY LIMIT Performance Optimization - Percona
LIMIT you should try hard to have sorting column(s) to be in the leading table. If ORDER BY is going by field from...
Read more >Query optimization techniques in SQL Server: tips and tricks
In this blog post we will show you step by step some tips and tricks for successful Query optimization techniques in SQL Server....
Read more >Sort search results | Elasticsearch Guide [8.5] | Elastic
Sort search resultsedit ... Allows you to add one or more sorts on specific fields. Each sort can be reversed as well. The...
Read more >BigQuery Admin reference guide: Query optimization
4. Order by with limit: Writing results for a query with an ORDER BY clause can result in Resources Exceeded errors. Since the...
Read more >8.2.1.14 ORDER BY Optimization - MySQL :: Developer Zone
In that case, scanning an entire index and looking up table rows to find columns not in the index may be more expensive...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think we need to invest some time optimizing sorted by, as there are too many cases where the optimizations can be applied but they are not. One example:
Where
any_other_thing
may be a column, an operation or even a literal!@ashishkf Good call! Created #9839 to track various improvements for AND operator