IdxInsert
Register P2 holds an SQL index key made using the MakeRecord instructions.
An example
37 of the 178 statements we compiled against the sample database use IdxInsert. This is the one with the shortest program; the rows that use IdxInsert are highlighted.
SELECT DISTINCT country FROM artists
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 11 | 0 | 0 | |
| 1 | OpenEphemeral | 1 | 0 | 0 | k(1,B) | 8 |
| 2 | OpenRead | 0 | 2 | 0 | 3 | 0 |
| 3 | Rewind | 0 | 10 | 0 | 0 | |
| 4 | Column | 0 | 2 | 1 | 0 | |
| 5 | Found | 1 | 9 | 1 | 1 | 0 |
| 6 | MakeRecord | 1 | 1 | 2 | 0 | |
| 7 | IdxInsert | 1 | 2 | 1 | 1 | 16 |
| 8 | ResultRow | 1 | 1 | 0 | 0 | |
| 9 | Next | 0 | 4 | 0 | 1 | |
| 10 | Halt | 0 | 0 | 0 | 0 | |
| 11 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 12 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: Column, Found, Goto, Halt, Init, MakeRecord, Next, OpenEphemeral, OpenRead, ResultRow, Rewind, Transaction.
What the SQLite documentation says
Register P2 holds an SQL index key made using the MakeRecord instructions. This opcode writes that key into the index P1. Data for the entry is nil.
If P4 is not zero, then it is the number of values in the unpacked key of reg(P2). In that case, P3 is the index of the first register for the unpacked key. The availability of the unpacked key can sometimes be an optimization.
If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer that this insert is likely to be an append.
If P5 has the OPFLAG_NCHANGE bit set, then the change counter is incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, then the change counter is unchanged.
If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might run faster by avoiding an unnecessary seek on cursor P1. However, the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior seeks on the cursor or if the most recent seek used a key equivalent to P2.
This instruction only works for indices. The equivalent instruction for tables is Insert.
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.