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
| 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, 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 →