OffsetLimit
This opcode performs a commonly used computation associated with LIMIT and OFFSET processing.
An example
1 of the 178 statements we compiled against the sample database use OffsetLimit. This is the one with the shortest program; the rows that use OffsetLimit are highlighted.
SELECT * FROM tracks ORDER BY ms DESC LIMIT 10 OFFSET 20
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 35 | 0 | 0 | |
| 1 | OpenEphemeral | 1 | 8 | 0 | k(1,-B) | 0 |
| 2 | Integer | 10 | 1 | 0 | 0 | |
| 3 | Integer | 20 | 2 | 0 | 0 | |
| 4 | MustBeInt | 2 | 0 | 0 | 0 | |
| 5 | OffsetLimit | 1 | 3 | 2 | 0 | |
| 6 | OpenRead | 0 | 4 | 0 | 6 | 0 |
| 7 | Rewind | 0 | 23 | 0 | 0 | |
| 8 | Column | 0 | 3 | 4 | 0 | |
| 9 | Sequence | 1 | 5 | 0 | 0 | |
| 10 | IfNotZero | 3 | 14 | 0 | 0 | |
| 11 | Last | 1 | 0 | 0 | 0 | |
| 12 | IdxLE | 1 | 22 | 4 | 1 | 0 |
| 13 | Delete | 1 | 0 | 0 | 0 | |
| 14 | Rowid | 0 | 6 | 0 | 0 | |
| 15 | Column | 0 | 1 | 7 | 0 | |
| 16 | Column | 0 | 2 | 8 | 0 | |
| 17 | Column | 0 | 4 | 9 | 0 | |
| 18 | Column | 0 | 5 | 10 | 0 | |
| 19 | RealAffinity | 10 | 0 | 0 | 0 | |
| 20 | MakeRecord | 4 | 7 | 12 | 0 | |
| 21 | IdxInsert | 1 | 12 | 4 | 7 | 0 |
| 22 | Next | 0 | 8 | 0 | 1 | |
| 23 | Sort | 1 | 34 | 0 | 0 | |
| 24 | IfPos | 2 | 33 | 1 | 0 | |
| 25 | AddImm | 1 | -1 | 0 | 0 | |
| 26 | Column | 1 | 6 | 11 | 0 | |
| 27 | Column | 1 | 5 | 10 | 0 | |
| 28 | Column | 1 | 0 | 9 | 0 | |
| 29 | Column | 1 | 4 | 8 | 0 | |
| 30 | Column | 1 | 3 | 7 | 0 | |
| 31 | Column | 1 | 2 | 6 | 0 | |
| 32 | ResultRow | 6 | 6 | 0 | 0 | |
| 33 | Next | 1 | 24 | 0 | 0 | |
| 34 | Halt | 0 | 0 | 0 | 0 | |
| 35 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 36 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: AddImm, Column, Delete, Goto, Halt, IdxInsert, IdxLE, IfNotZero, IfPos, Init, Integer, Last, MakeRecord, MustBeInt, Next, OpenEphemeral, OpenRead, RealAffinity, ResultRow, Rewind, Rowid, Sequence, Sort, Transaction.
What the SQLite documentation says
This opcode performs a commonly used computation associated with LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] holds the offset counter. The opcode computes the combined value of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] value computed is the total number of rows that will need to be visited in order to complete the query.
If r[P3] is zero or negative, that means there is no OFFSET and r[P2] is set to be the value of the LIMIT, r[P1].
if r[P1] is zero or negative, that means there is no LIMIT and r[P2] is set to -1.
Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.