SQLite VDBE opcode

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)
addropcodep1p2p3p4p5
0Init03700
1Noop03600
2CreateBtree0120
3OpenWrite01050
4String8030index0
5String8040u0
6String8050albums0
7SCopy1600
8String8070CREATE UNIQUE INDEX u ON albums(title, artist_id)0
9NewRowid0200
10MakeRecord358BBBDB0
11Insert08224
12SorterOpen302k(2,,)0
13OpenRead13040
14Rewind12100
15Column12100
16Column11110
17Rowid11200
18MakeRecord10390
19SorterInsert3900
20Next11500
21OpenWrite210k(2,,)17
22SorterSort33000
23Goto02600
24SorterCompare323920
25Halt206720albums.title, albums.artist_id2
26SorterData3920
27SeekEnd2000
28IdxInsert29016
29SorterNext32400
30Close1000
31Close2000
32Close3000
33SetCookie0120
34ParseSchema000name='u' AND type='index'0
35Expire0100
36Halt0000
37Transaction01101
38Goto0100

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 →