SorterCompare
P1 is a sorter cursor.
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
1 of the 178 statements we compiled against the sample database use SorterCompare. This is the one with the shortest program; the rows that use SorterCompare are highlighted.
CREATE UNIQUE INDEX u ON albums(title, artist_id)
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 37 | 0 | 0 | |
| 1 | Noop | 0 | 36 | 0 | 0 | |
| 2 | CreateBtree | 0 | 1 | 2 | 0 | |
| 3 | OpenWrite | 0 | 1 | 0 | 5 | 0 |
| 4 | String8 | 0 | 3 | 0 | index | 0 |
| 5 | String8 | 0 | 4 | 0 | u | 0 |
| 6 | String8 | 0 | 5 | 0 | albums | 0 |
| 7 | SCopy | 1 | 6 | 0 | 0 | |
| 8 | String8 | 0 | 7 | 0 | CREATE UNIQUE INDEX u ON albums(title, artist_id) | 0 |
| 9 | NewRowid | 0 | 2 | 0 | 0 | |
| 10 | MakeRecord | 3 | 5 | 8 | BBBDB | 0 |
| 11 | Insert | 0 | 8 | 2 | 24 | |
| 12 | SorterOpen | 3 | 0 | 2 | k(2,,) | 0 |
| 13 | OpenRead | 1 | 3 | 0 | 4 | 0 |
| 14 | Rewind | 1 | 21 | 0 | 0 | |
| 15 | Column | 1 | 2 | 10 | 0 | |
| 16 | Column | 1 | 1 | 11 | 0 | |
| 17 | Rowid | 1 | 12 | 0 | 0 | |
| 18 | MakeRecord | 10 | 3 | 9 | 0 | |
| 19 | SorterInsert | 3 | 9 | 0 | 0 | |
| 20 | Next | 1 | 15 | 0 | 0 | |
| 21 | OpenWrite | 2 | 1 | 0 | k(2,,) | 17 |
| 22 | SorterSort | 3 | 30 | 0 | 0 | |
| 23 | Goto | 0 | 26 | 0 | 0 | |
| 24 | SorterCompare | 3 | 23 | 9 | 2 | 0 |
| 25 | Halt | 2067 | 2 | 0 | albums.title, albums.artist_id | 2 |
| 26 | SorterData | 3 | 9 | 2 | 0 | |
| 27 | SeekEnd | 2 | 0 | 0 | 0 | |
| 28 | IdxInsert | 2 | 9 | 0 | 16 | |
| 29 | SorterNext | 3 | 24 | 0 | 0 | |
| 30 | Close | 1 | 0 | 0 | 0 | |
| 31 | Close | 2 | 0 | 0 | 0 | |
| 32 | Close | 3 | 0 | 0 | 0 | |
| 33 | SetCookie | 0 | 1 | 2 | 0 | |
| 34 | ParseSchema | 0 | 0 | 0 | name='u' AND type='index' | 0 |
| 35 | Expire | 0 | 1 | 0 | 0 | |
| 36 | Halt | 0 | 0 | 0 | 0 | |
| 37 | Transaction | 0 | 1 | 1 | 0 | 1 |
| 38 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: Close, Column, CreateBtree, Expire, Goto, Halt, IdxInsert, Init, Insert, MakeRecord, NewRowid, Next, Noop, OpenRead, OpenWrite, ParseSchema, Rewind, Rowid, SCopy, SeekEnd, SetCookie, SorterData, SorterInsert, SorterNext, SorterOpen, SorterSort, String8, Transaction.
What the SQLite documentation says
P1 is a sorter cursor. This instruction compares a prefix of the record blob in register P3 against a prefix of the entry that the sorter cursor currently points to. Only the first P4 fields of r[P3] and the sorter record are compared.
If either P3 or the sorter contains a NULL in one of their significant fields (not counting the P4 fields at the end which are ignored) then the comparison is assumed to be equal.
Fall through to next instruction if the two records compare equal to each other. Jump to P2 if they are different.
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.
← Sort · SorterData →