create table sample1( id int primary key auto_increment, val int not null );
create table sample2( id int primary key auto_increment, val int not null );
insert into sample1(val) values (2), (3), (5), (7), (11); insert into sample2(val) values (1), (3), (5), (7), (9);
explain select * from sample1 inner join sample2 on sample1.val = sample2.val;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+ | 1 | SIMPLE | sample1 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | NULL | | 1 | SIMPLE | sample2 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 20.00 | Using where; Using join buffer (Block Nested Loop) | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
sqlite> CREATE TABLE p (color TEXT); sqlite> CREATE TABLE s (color TEXT); sqlite> INSERT INTO p VALUES ("red"), ("blue"); sqlite> INSERT INTO s VALUES ("orange"), ("green"); sqlite> EXPLAIN SELECT * FROM p NATURAL JOIN s; addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 21 0 0 Start at 21 1 OpenRead 0 2 0 1 0 root=2 iDb=0; p 2 OpenRead 1 3 0 1 0 root=3 iDb=0; s 3 Rewind 0 20 0 0 4 Once 0 12 0 0 5 OpenAutoindex 2 2 0 k(2,B,) 0 nColumn=2; for s 6 Rewind 1 12 0 0 7 Column 1 0 2 0 r[2]=s.color 8 Rowid 1 3 0 0 r[3]=rowid 9 MakeRecord 2 2 1 0 r[1]=mkrec(r[2..3]) 10 IdxInsert 2 1 0 16 key=r[1] 11 Next 1 7 0 3 12 Column 0 0 4 0 r[4]=p.color 13 IsNull 4 19 0 0 if r[4]==NULL goto 19 14 SeekGE 2 19 4 1 0 key=r[4] 15 IdxGT 2 19 4 1 0 key=r[4] 16 Column 0 0 5 0 r[5]=p.color 17 ResultRow 5 1 0 0 output=r[5] 18 Next 2 15 0 0 19 Next 0 4 0 1 20 Halt 0 0 0 0 21 Transaction 0 0 2 0 1 usesStmtJournal=0 22 Goto 0 1 0 0
CREATE TABLE members ( member_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (member_id) );
CREATE TABLE committees ( committee_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (committee_id) );
INSERT INTO members(name) VALUES('John'),('Jane'),('Mary'),('David'),('Amelia');
INSERT INTO committees(name) VALUES('John'),('Mary'),('Amelia'),('Joe');
SELECT m.member_id, m.name member, c.committee_id, c.name committee FROM members m INNER JOIN committees c ON c.name = m.name;
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE c ALL NULL NULL NULL NULL 4 1 SIMPLE m ALL NULL NULL NULL NULL 5 Using where; Using join buffer(Edited: 2020-10-28)
CREATE TABLE sample1 (id INTEGER PRIMARY KEY AUTOINCREMENT, val INTEGER); CREATE TABLE sample2 (sample_id INTEGER PRIMARY KEY AUTOINCREMENT, val INTEGER); INSERT INTO sample1 (val) values (23); INSERT INTO sample1 (val) values (54); INSERT INTO sample1 (val) values (68); INSERT INTO sample2 (val) values (34); INSERT INTO sample2 (val) values (null); INSERT INTO sample2 (val) values (21); EXPLAIN SELECT * FROM sample1 NATURAL JOIN sample2;
Output -
addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 24 0 0 Start at 24 1 OpenRead 0 2 0 2 0 root=2 iDb=0; sample1 2 OpenRead 1 4 0 2 0 root=4 iDb=0; sample2 3 Rewind 0 23 0 0 4 Once 0 12 0 0 5 OpenAutoindex 2 2 0 k(2,B,) 0 nColumn=2; for sample2 6 Rewind 1 12 0 0 7 Column 1 1 2 0 r[2]=sample2.val 8 Rowid 1 3 0 0 r[3]=rowid 9 MakeRecord 2 2 1 0 r[1]=mkrec(r[2..3]) 10 IdxInsert 2 1 0 16 key=r[1] 11 Next 1 7 0 3 12 Column 0 1 4 0 r[4]=sample1.val 13 IsNull 4 22 0 0 if r[4]==NULL goto 22 14 Affinity 4 1 0 C 0 affinity(r[4]) 15 SeekGE 2 22 4 1 0 key=r[4] 16 IdxGT 2 22 4 1 0 key=r[4] 17 Rowid 0 5 0 0 r[5]=rowid 18 Column 0 1 6 0 r[6]=sample1.val 19 IdxRowid 2 7 0 0 r[7]=rowid 20 ResultRow 5 3 0 0 output=r[5..7] 21 Next 2 16 0 0 22 Next 0 4 0 1 23 Halt 0 0 0 0 24 Transaction 0 0 2 0 1 usesStmtJournal=0 25 Goto 0 1 0 0
create table foo(a integer primary key, b integer not null); create table moo(a integer primary key, b integer not null);
insert into foo values(1,5),(2,7); insert into moo values(1,10),(2,20);
explain select * from foo natural join moo;
addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 14 0 0 Start at 14 1 OpenRead 0 2 0 2 0 root=2 iDb=0; foo 2 OpenRead 1 3 0 2 0 root=3 iDb=0; moo 3 Rewind 0 13 0 0 4 Rowid 0 1 0 0 r[1]=rowid 5 SeekRowid 1 12 1 0 intkey=r[1] 6 Column 0 1 2 0 r[2]=foo.b 7 Column 1 1 3 0 r[3]=moo.b 8 Ne 3 12 2 BINARY-8 83 if r[2]!=r[3] goto 12 9 Rowid 0 4 0 0 r[4]=rowid 10 Column 0 1 5 0 r[5]=foo.b 11 ResultRow 4 2 0 0 output=r[4..5] 12 Next 0 4 0 1 13 Halt 0 0 0 0 14 Transaction 0 0 2 0 1 usesStmtJournal=0 15 Goto 0 1 0 0(Edited: 2020-10-28)
sqlite> create table tbl_A (x integer primary key, y int); sqlite> create table tbl_B (y integer primary key, z int); sqlite> insert into tbl_A values (1, 2); sqlite> insert into tbl_A values (3, 4); sqlite> insert into tbl_A values (5, 6); sqlite> insert into tbl_B values (2, 12); sqlite> insert into tbl_B values (4, 14); sqlite> insert into tbl_B values (6, 16); sqlite> explain select * from tbl_A natural join tbl_B; addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 12 0 0 Start at 12 1 OpenRead 0 3 0 2 0 root=3 iDb=0; tbl_A 2 OpenRead 1 2 0 2 0 root=2 iDb=0; tbl_B 3 Rewind 0 11 0 0 4 Column 0 1 1 0 r[1]=tbl_A.y 5 SeekRowid 1 10 1 0 intkey=r[1] 6 Rowid 0 2 0 0 r[2]=rowid 7 Column 0 1 3 0 r[3]=tbl_A.y 8 Column 1 1 4 0 r[4]=tbl_B.z 9 ResultRow 2 3 0 0 output=r[2..4] 10 Next 0 4 0 1 11 Halt 0 0 0 0 12 Transaction 0 0 6 0 1 usesStmtJournal=0 13 Goto 0 1 0 0
sqlite> create table first( ...> a integer not null, ...> b integer not null); sqlite> create table second( ...> a integer not null, ...> c integer not null); sqlite> insert into first values (1,2), (2,3), (3,4), (4,5); sqlite> insert into second values (1,3), (3,5), (5,7), (7,9); sqlite> explain select * from first NATURAL JOIN second; addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 24 0 00 Start at 24 1 OpenRead 0 2 0 2 00 root=2 iDb=0; first 2 OpenRead 1 3 0 2 00 root=3 iDb=0; second 3 Rewind 0 23 0 00 4 Once 0 13 0 00 5 OpenAutoindex 2 3 0 k(3,B,,) 00 nColumn=3; for second 6 Rewind 1 13 0 00 7 Column 1 0 2 00 r[2]=second.a 8 Column 1 1 3 00 r[3]=second.c 9 Rowid 1 4 0 00 r[4]=rowid 10 MakeRecord 2 3 1 00 r[1]=mkrec(r[2..4]) 11 IdxInsert 2 1 0 10 key=r[1] 12 Next 1 7 0 03 13 Column 0 0 5 00 r[5]=first.a 14 Affinity 5 1 0 C 00 affinity(r[5]) 15 SeekGE 2 22 5 1 00 key=r[5] 16 IdxGT 2 22 5 1 00 key=r[5] 17 Column 0 0 6 00 r[6]=first.a 18 Column 0 1 7 00 r[7]=first.b 19 Column 2 1 8 00 r[8]=second.c 20 ResultRow 6 3 0 00 output=r[6..8] 21 Next 2 16 0 00 22 Next 0 4 0 01 23 Halt 0 0 0 00 24 Transaction 0 0 2 0 01 usesStmtJournal=0 25 Goto 0 1 0 00
sqlite> create table tbl1(a integer primary key, b integer not null); sqlite> create table tbl2(a integer primary key, b integer not null); sqlite> insert into tbl1 values(1,2),(3,4); sqlite> insert into tbl2 values(5,6),(7,8); sqlite> explain select * from tbl1 natural join tbl2; addr opcode p1 p2 p3 p4 p5 comment ---- ------------- ---- ---- ---- ------------- -- ------------- 0 Init 0 14 0 00 Start at 14 1 OpenRead 0 2 0 2 00 root=2 iDb=0; tbl1 2 OpenRead 1 3 0 2 00 root=3 iDb=0; tbl2 3 Rewind 0 13 0 00 4 Rowid 0 1 0 00 r[1]=rowid 5 SeekRowid 1 12 1 00 intkey=r[1] 6 Column 0 1 2 00 r[2]=tbl1.b 7 Column 1 1 3 00 r[3]=tbl2.b 8 Ne 3 12 2 (BINARY) 53 if r[2]!=r[3] goto 12 9 Rowid 0 4 0 00 r[4]=rowid 10 Column 0 1 5 00 r[5]=tbl1.b 11 ResultRow 4 2 0 00 output=r[4..5] 12 Next 0 4 0 01 13 Halt 0 0 0 00 14 Transaction 0 0 2 0 01 usesStmtJournal=0 15 Goto 0 1 0 00