SQLite VDBE opcode

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
addropcodep1p2p3p4p5
0Init03500
1OpenEphemeral180k(1,-B)0
2Integer10100
3Integer20200
4MustBeInt2000
5OffsetLimit1320
6OpenRead04060
7Rewind02300
8Column0340
9Sequence1500
10IfNotZero31400
11Last1000
12IdxLE122410
13Delete1000
14Rowid0600
15Column0170
16Column0280
17Column0490
18Column05100
19RealAffinity10000
20MakeRecord47120
21IdxInsert112470
22Next0801
23Sort13400
24IfPos23310
25AddImm1-100
26Column16110
27Column15100
28Column1090
29Column1480
30Column1370
31Column1260
32ResultRow6600
33Next12400
34Halt0000
35Transaction00101
36Goto0100

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.

← Offset · Once →