How LIMIT and OFFSET are implemented in TiDB

you can use LIMIT and OFFSET to paginate the data in TiDB. However, TiDB’s underlying storage engine is TiKV.

Reading TiKV source code reveals that TiKV does not seem to find query interfaces for OFFSET and LIMIT. The only relevant ones are:

  • rawKV.Scan
  • txn.Scan

So, my question is: How does TiDB use TiKV to implement OFFSET and LIMIT?

The offset is usually encoded as part of key if possible, TiDB predicts the offset from an ordered index for example. Otherwise, it will be a full range scan.

1 Like

it there any code which shows tidb predicts the offset from an ordered index?

So, my question is: How does TiDB use TiKV to implement OFFSET and LIMIT?

limit_executor.rs?