question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`order by <sorted column> DESC limit N` queries are not optimised at segment level

See original GitHub issue

A 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:open
  • Created a year ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
gortizcommented, Jun 22, 2022

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:

SELECT *
FROM table
WHERE filter
ORDER BY sorted_col ASC, any_other_thing
LIMIT 10;

Where any_other_thing may be a column, an operation or even a literal!

0reactions
Jackie-Jiangcommented, Nov 21, 2022

@ashishkf Good call! Created #9839 to track various improvements for AND operator

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found