Skip to content
← Back to activity

Solution skill

Improve limit -1 performance

Version 2Sign in to save

Complete solution

Shows how to fix improve limit -1 performance. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

This can be tested on demo box with the new dataset. ` select from market\_data limit -1; select from market\_data latest by symbol; ` Explain says latest on does a frame backward scan, while limit -1 needs to skip over billions of frames, then do a row forward scan.

Steps

  1. Issue questdb/questdb#5915 (closed, 15 comments): ### Is your feature request related to a problem? Noticed that limit -1 can be quite slow, specially while ingesting data on a large table. This can be tested on demo box with the new dataset.

2. Explain says latest on does a frame backward scan, while limit -1 needs to skip over billions of frames, then do a row forward scan.

3. limit -1 on a large QuestDB table is slow because it skips over billions of rows before scanning forward. The maintainer's workaround: use (select * from trades order by timestamp desc limit 1) order by timestamp instead, or latest by symbol. It is not 100 percent equivalent for rows with identical timestamps, but it is much faster.

## When to use

You are seeing this: Issue questdb/questdb#5915 (closed, 15 comments): ### Is your feature request related to a problem? Use this skill when you run into "Improve limit -1 performance".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

## Sources

- https://github.com/questdb/questdb/issues/5915

Evidence

In gh:questdb/questdb#5915, the reporter ran into: Issue questdb/questdb#5915 (closed, 15 comments): ### Is your feature request related to a problem? Noticed that limit -1 can be quite slow, specially while ingesting data on a large table. This can be tested on demo box with the new dataset. `` select * from market_data limit -1; select * from market_data latest by symbol; `` Explain says latest on does a frame backward scan, while limit -1 needs to skip over billions of frames, then do a row forward scan. The verified answer was: limit -1 on a large QuestDB table is slow because it skips over billions of rows before scanning forward. The maintainer's workaround: use (select * from trades order by timestamp desc limit 1) order by timestamp instead, or latest by symbol. It is not 100 percent equivalent for rows with identical timestamps, but it is much faster. Source issue: https://github.com/questdb/questdb/issues/5915

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_tDICnZyU5wFCeTsmCrEGJQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Improve limit -1 performance <span>— Vectle</span></a>