Compare
Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this vector "A") and in reg(P2)..reg(P2+P3-1) ("B").
An example
13 of the 178 statements we compiled against the sample database use Compare. This is the one with the shortest program; the rows that use Compare are highlighted.
SELECT album_id, count(*), sum(ms) FROM tracks GROUP BY album_id
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 39 | 0 | 0 | |
| 1 | Noop | 1 | 2 | 0 | 0 | |
| 2 | Integer | 0 | 2 | 0 | 0 | |
| 3 | Null | 0 | 5 | 5 | 0 | |
| 4 | Gosub | 4 | 35 | 0 | 0 | |
| 5 | OpenRead | 0 | 4 | 0 | 4 | 0 |
| 6 | OpenRead | 2 | 5 | 0 | k(2,,) | 0 |
| 7 | Rewind | 2 | 23 | 7 | 0 | 0 |
| 8 | DeferredSeek | 2 | 0 | 0 | 0 | |
| 9 | Column | 2 | 0 | 6 | 0 | |
| 10 | Compare | 5 | 6 | 1 | k(1,B) | 0 |
| 11 | Jump | 12 | 16 | 12 | 0 | |
| 12 | Gosub | 3 | 27 | 0 | 0 | |
| 13 | Move | 6 | 5 | 1 | 0 | |
| 14 | IfPos | 2 | 38 | 0 | 0 | |
| 15 | Gosub | 4 | 35 | 0 | 0 | |
| 16 | AggStep | 0 | 0 | 9 | count(0) | 0 |
| 17 | Column | 0 | 3 | 11 | 0 | |
| 18 | AggStep | 0 | 11 | 10 | sum(1) | 1 |
| 19 | If | 1 | 21 | 0 | 0 | |
| 20 | Column | 2 | 0 | 7 | 0 | |
| 21 | Integer | 1 | 1 | 0 | 0 | |
| 22 | Next | 2 | 8 | 0 | 1 | |
| 23 | Gosub | 3 | 27 | 0 | 0 | |
| 24 | Goto | 0 | 38 | 0 | 0 | |
| 25 | Integer | 1 | 2 | 0 | 0 | |
| 26 | Return | 3 | 0 | 0 | 0 | |
| 27 | IfPos | 1 | 29 | 0 | 0 | |
| 28 | Return | 3 | 0 | 0 | 0 | |
| 29 | AggFinal | 9 | 0 | 0 | count(0) | 0 |
| 30 | AggFinal | 10 | 1 | 0 | sum(1) | 0 |
| 31 | Copy | 7 | 12 | 0 | 0 | |
| 32 | Copy | 9 | 13 | 1 | 0 | |
| 33 | ResultRow | 12 | 3 | 0 | 0 | |
| 34 | Return | 3 | 0 | 0 | 0 | |
| 35 | Null | 0 | 7 | 10 | 0 | |
| 36 | Integer | 0 | 1 | 0 | 0 | |
| 37 | Return | 4 | 0 | 0 | 0 | |
| 38 | Halt | 0 | 0 | 0 | 0 | |
| 39 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 40 | Null | 0 | 5 | 5 | 0 | |
| 41 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: AggFinal, AggStep, Column, Copy, DeferredSeek, Gosub, Goto, Halt, If, IfPos, Init, Integer, Jump, Move, Next, Noop, Null, OpenRead, ResultRow, Return, Rewind, Transaction.
What the SQLite documentation says
Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of the comparison for use by the next Jump instruct.
If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is determined by the most recent Permutation operator. If the OPFLAG_PERMUTE bit is clear, then register are compared in sequential order.
P4 is a KeyInfo structure that defines collating sequences and sort orders for the comparison. The permutation applies to registers only. The KeyInfo elements are used sequentially.
The comparison is a sort comparison, so NULLs compare equal, NULLs are less than numbers, numbers are less than strings, and strings are less than blobs.
This opcode must be immediately followed by an Jump opcode.
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.
← ColumnsUsed · Concat →