SQLite VDBE opcode

Sort

This opcode does exactly the same thing as Rewind except that it increments an undocumented global variable used for testing.

Jumps: P2 is an instruction address. In the listings here, a jump is drawn as an arc and the address is a link.

An example

2 of the 178 statements we compiled against the sample database use Sort. This is the one with the shortest program; the rows that use Sort 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, OffsetLimit, OpenEphemeral, OpenRead, RealAffinity, ResultRow, Rewind, Rowid, Sequence, Transaction.

What the SQLite documentation says

This opcode does exactly the same thing as Rewind except that it increments an undocumented global variable used for testing.

Sorting is accomplished by writing records into a sorting index, then rewinding that index and playing it back from beginning to end. We use the Sort opcode instead of Rewind to do the rewinding so that the global variable will be incremented and regression tests can determine whether or not the optimizer is correctly optimizing out sorts.

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.

← SoftNull · SorterCompare →