notes

May 01
Permalink

Virtual Machine, SQL Database … or Both?

Spending today working with Jonathan on some iPhone/Mac Objective-C libraries for SQLite (managing data/schema migrations, etc), I stumbled across this bit of documentation:

After the parser assembles tokens into complete SQL statements, it calls the code generator to produce virtual machine code that will do the work that the SQL statements request.

Cool! SQLite’s Virtual Database Engine (VDBE) is documented in this tutorial, and the vm opcode reference. You can peruse the code generated for your own queries using explain (first using .explain to enable more readable output):

sqlite> .explain
sqlite> EXPLAIN INSERT INTO "testtable" VALUES ("testing");
addr opcode p1 p2 p3
---- -------------- ---------- ---------- --------------------
0 Goto 0 10
1 Integer 0 0
2 OpenWrite 0 2
3 SetNumColumns 0 1
4 NewRowid 0 0
5 String8 0 0 testing
6 MakeRecord 1 0 a
7 Insert 0 11 testtable
8 Close 0 0
9 Halt 0 0
10 Transaction 0 1
11 VerifyCookie 0 1
12 Goto 0 1
13 Noop 0 0