0000: 23 20 32 30 30 36 20 4a 61 6e 75 61 72 79 20 33 # 2006 January 3
0010: 31 0a 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 1.#.# The author
0020: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
0030: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
0040: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
0050: 61 63 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c ace of.# a legal
0060: 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
0070: 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 a blessing:.#.#
0080: 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
0090: 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
00a0: 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 ..# May you f
00b0: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
00c0: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
00d0: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
00e0: 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 .# May you sh
00f0: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
0100: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
0110: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 an you give..#.#
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 0a 23 20 54 68 69 73 20 66 *******.# This f
0170: 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 ile implements r
0180: 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 egression tests
0190: 66 6f 72 20 53 51 4c 69 74 65 20 6c 69 62 72 61 for SQLite libra
01a0: 72 79 2e 20 20 54 68 65 0a 23 20 66 6f 63 75 73 ry. The.# focus
01b0: 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 69 73 of this file is
01c0: 20 74 65 73 74 69 6e 67 20 74 68 65 20 6a 6f 69 testing the joi
01d0: 6e 20 72 65 6f 72 64 65 72 69 6e 67 20 6f 70 74 n reordering opt
01e0: 69 6d 69 7a 61 74 69 6f 6e 0a 23 20 69 6e 20 63 imization.# in c
01f0: 61 73 65 73 20 74 68 61 74 20 69 6e 63 6c 75 64 ases that includ
0200: 65 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2e 0a 23 e a LEFT JOIN..#
0210: 0a 23 20 24 49 64 3a 20 77 68 65 72 65 33 2e 74 .# $Id: where3.t
0220: 65 73 74 2c 76 20 31 2e 34 20 32 30 30 38 2f 30 est,v 1.4 2008/0
0230: 34 2f 31 37 20 31 39 3a 31 34 3a 30 32 20 64 72 4/17 19:14:02 dr
0240: 68 20 45 78 70 20 24 0a 0a 73 65 74 20 74 65 73 h Exp $..set tes
0250: 74 64 69 72 20 5b 66 69 6c 65 20 64 69 72 6e 61 tdir [file dirna
0260: 6d 65 20 24 61 72 67 76 30 5d 0a 73 6f 75 72 63 me $argv0].sourc
0270: 65 20 24 74 65 73 74 64 69 72 2f 74 65 73 74 65 e $testdir/teste
0280: 72 2e 74 63 6c 0a 0a 23 20 54 68 65 20 66 6f 6c r.tcl..# The fol
0290: 6c 6f 77 69 6e 67 20 69 73 20 66 72 6f 6d 20 74 lowing is from t
02a0: 69 63 6b 65 74 20 23 31 36 35 32 2e 0a 23 0a 23 icket #1652..#.#
02b0: 20 41 20 63 6f 6d 6d 61 20 6a 6f 69 6e 20 74 68 A comma join th
02c0: 65 6e 20 61 20 6c 65 66 74 20 6f 75 74 65 72 20 en a left outer
02d0: 6a 6f 69 6e 3a 20 20 41 2c 42 20 6c 65 66 74 20 join: A,B left
02e0: 6a 6f 69 6e 20 43 2e 0a 23 20 41 72 72 61 6e 67 join C..# Arrang
02f0: 65 20 69 6e 64 69 63 65 73 20 73 6f 20 74 68 61 e indices so tha
0300: 74 20 74 68 65 20 42 20 74 61 62 6c 65 20 69 73 t the B table is
0310: 20 63 68 6f 73 65 6e 20 74 6f 20 67 6f 20 66 69 chosen to go fi
0320: 72 73 74 2e 0a 23 20 41 6c 73 6f 20 70 75 74 20 rst..# Also put
0330: 61 6e 20 69 6e 64 65 78 20 6f 6e 20 43 2c 20 62 an index on C, b
0340: 75 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 ut make sure tha
0350: 74 20 41 20 69 73 20 63 68 6f 73 65 6e 20 62 65 t A is chosen be
0360: 66 6f 72 65 20 43 2e 0a 23 0a 64 6f 5f 74 65 73 fore C..#.do_tes
0370: 74 20 77 68 65 72 65 33 2d 31 2e 31 20 7b 0a 20 t where3-1.1 {.
0380: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 execsql {. C
0390: 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 REATE TABLE t1(a
03a0: 2c 20 62 29 3b 0a 20 20 20 20 43 52 45 41 54 45 , b);. CREATE
03b0: 20 54 41 42 4c 45 20 74 32 28 70 2c 20 71 29 3b TABLE t2(p, q);
03c0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c . CREATE TABL
03d0: 45 20 74 33 28 78 2c 20 79 29 3b 0a 20 20 20 20 E t3(x, y);.
03e0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f . INSERT INTO
03f0: 20 74 31 20 56 41 4c 55 45 53 28 31 31 31 2c 27 t1 VALUES(111,'
0400: 6f 6e 65 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 one');. INSER
0410: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 T INTO t1 VALUES
0420: 28 32 32 32 2c 27 74 77 6f 27 29 3b 0a 20 20 20 (222,'two');.
0430: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 INSERT INTO t1
0440: 56 41 4c 55 45 53 28 33 33 33 2c 27 74 68 72 65 VALUES(333,'thre
0450: 65 27 29 3b 0a 20 20 20 20 0a 20 20 20 20 49 4e e');. . IN
0460: 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56 41 4c SERT INTO t2 VAL
0470: 55 45 53 28 31 2c 31 31 31 29 3b 0a 20 20 20 20 UES(1,111);.
0480: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56 INSERT INTO t2 V
0490: 41 4c 55 45 53 28 32 2c 32 32 32 29 3b 0a 20 20 ALUES(2,222);.
04a0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 INSERT INTO t2
04b0: 20 56 41 4c 55 45 53 28 34 2c 34 34 34 29 3b 0a VALUES(4,444);.
04c0: 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 CREATE INDEX
04d0: 20 74 32 69 31 20 4f 4e 20 74 32 28 70 29 3b 0a t2i1 ON t2(p);.
04e0: 20 20 20 20 0a 20 20 20 20 49 4e 53 45 52 54 20 . INSERT
04f0: 49 4e 54 4f 20 74 33 20 56 41 4c 55 45 53 28 39 INTO t3 VALUES(9
0500: 39 39 2c 27 6e 69 6e 65 27 29 3b 0a 20 20 20 20 99,'nine');.
0510: 43 52 45 41 54 45 20 49 4e 44 45 58 20 74 33 69 CREATE INDEX t3i
0520: 31 20 4f 4e 20 74 33 28 78 29 3b 0a 20 20 20 20 1 ON t3(x);.
0530: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 . SELECT * FR
0540: 4f 4d 20 74 31 2c 20 74 32 20 4c 45 46 54 20 4a OM t1, t2 LEFT J
0550: 4f 49 4e 20 74 33 20 4f 4e 20 71 3d 78 20 57 48 OIN t3 ON q=x WH
0560: 45 52 45 20 70 3d 32 20 41 4e 44 20 61 3d 71 3b ERE p=2 AND a=q;
0570: 0a 20 20 7d 0a 7d 20 7b 32 32 32 20 74 77 6f 20 . }.} {222 two
0580: 32 20 32 32 32 20 7b 7d 20 7b 7d 7d 0a 0a 69 66 2 222 {} {}}..if
0590: 63 61 70 61 62 6c 65 20 65 78 70 6c 61 69 6e 20 capable explain
05a0: 7b 0a 20 20 64 6f 5f 74 65 73 74 20 77 68 65 72 {. do_test wher
05b0: 65 33 2d 31 2e 31 2e 31 20 7b 0a 20 20 20 20 20 e3-1.1.1 {.
05c0: 65 78 70 6c 61 69 6e 5f 6e 6f 5f 74 72 61 63 65 explain_no_trace
05d0: 20 7b 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 {SELECT * FROM
05e0: 74 31 2c 20 74 32 20 4c 45 46 54 20 4a 4f 49 4e t1, t2 LEFT JOIN
05f0: 20 74 33 20 4f 4e 20 71 3d 78 0a 20 20 20 20 20 t3 ON q=x.
0600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0610: 20 20 20 57 48 45 52 45 20 70 3d 32 20 41 4e 44 WHERE p=2 AND
0620: 20 61 3d 71 7d 0a 20 20 7d 20 5b 65 78 70 6c 61 a=q}. } [expla
0630: 69 6e 5f 6e 6f 5f 74 72 61 63 65 20 7b 53 45 4c in_no_trace {SEL
0640: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 ECT * FROM t1, t
0650: 32 20 4c 45 46 54 20 4a 4f 49 4e 20 74 33 20 4f 2 LEFT JOIN t3 O
0660: 4e 20 78 3d 71 0a 20 20 20 20 20 20 20 20 20 20 N x=q.
0670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 WH
0680: 45 52 45 20 70 3d 32 20 41 4e 44 20 61 3d 71 7d ERE p=2 AND a=q}
0690: 5d 0a 7d 0a 0a 23 20 54 69 63 6b 65 74 20 23 31 ].}..# Ticket #1
06a0: 38 33 30 0a 23 0a 23 20 54 68 69 73 20 69 73 20 830.#.# This is
06b0: 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 20 61 similar to the a
06c0: 62 6f 76 65 20 62 75 74 20 77 69 74 68 20 74 68 bove but with th
06d0: 65 20 4c 45 46 54 20 4a 4f 49 4e 20 6f 6e 20 74 e LEFT JOIN on t
06e0: 68 65 0a 23 20 6f 74 68 65 72 20 73 69 64 65 2e he.# other side.
06f0: 0a 23 0a 64 6f 5f 74 65 73 74 20 77 68 65 72 65 .#.do_test where
0700: 33 2d 31 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 3-1.2 {. execsq
0710: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 l {. CREATE T
0720: 41 42 4c 45 20 70 61 72 65 6e 74 31 28 70 61 72 ABLE parent1(par
0730: 65 6e 74 31 6b 65 79 2c 20 63 68 69 6c 64 31 6b ent1key, child1k
0740: 65 79 2c 20 43 68 69 6c 64 32 6b 65 79 2c 20 63 ey, Child2key, c
0750: 68 69 6c 64 33 6b 65 79 29 3b 0a 20 20 20 20 43 hild3key);. C
0760: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c REATE TABLE chil
0770: 64 31 20 28 20 63 68 69 6c 64 31 6b 65 79 20 4e d1 ( child1key N
0780: 56 41 52 43 48 41 52 2c 20 76 61 6c 75 65 20 4e VARCHAR, value N
0790: 56 41 52 43 48 41 52 20 29 3b 0a 20 20 20 20 43 VARCHAR );. C
07a0: 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 REATE UNIQUE IND
07b0: 45 58 20 50 4b 49 44 58 43 68 69 6c 64 31 20 4f EX PKIDXChild1 O
07c0: 4e 20 63 68 69 6c 64 31 20 28 20 63 68 69 6c 64 N child1 ( child
07d0: 31 6b 65 79 20 29 3b 0a 20 20 20 20 43 52 45 41 1key );. CREA
07e0: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 32 20 TE TABLE child2
07f0: 28 20 63 68 69 6c 64 32 6b 65 79 20 4e 56 41 52 ( child2key NVAR
0800: 43 48 41 52 2c 20 76 61 6c 75 65 20 4e 56 41 52 CHAR, value NVAR
0810: 43 48 41 52 20 29 3b 0a 0a 20 20 20 20 49 4e 53 CHAR );.. INS
0820: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 31 ERT INTO parent1
0830: 28 70 61 72 65 6e 74 31 6b 65 79 2c 63 68 69 6c (parent1key,chil
0840: 64 31 6b 65 79 2c 63 68 69 6c 64 32 6b 65 79 29 d1key,child2key)
0850: 0a 20 20 20 20 20 20 20 56 41 4c 55 45 53 20 28 . VALUES (
0860: 20 31 2c 20 27 43 31 2e 31 27 2c 20 27 43 32 2e 1, 'C1.1', 'C2.
0870: 31 27 20 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 1' );. INSERT
0880: 20 49 4e 54 4f 20 63 68 69 6c 64 31 20 28 20 63 INTO child1 ( c
0890: 68 69 6c 64 31 6b 65 79 2c 20 76 61 6c 75 65 20 hild1key, value
08a0: 29 20 56 41 4c 55 45 53 20 28 20 27 43 31 2e 31 ) VALUES ( 'C1.1
08b0: 27 2c 20 27 56 61 6c 75 65 20 66 6f 72 20 43 31 ', 'Value for C1
08c0: 2e 31 27 20 29 3b 0a 20 20 20 20 49 4e 53 45 52 .1' );. INSER
08d0: 54 20 49 4e 54 4f 20 63 68 69 6c 64 32 20 28 20 T INTO child2 (
08e0: 63 68 69 6c 64 32 6b 65 79 2c 20 76 61 6c 75 65 child2key, value
08f0: 20 29 20 56 41 4c 55 45 53 20 28 20 27 43 32 2e ) VALUES ( 'C2.
0900: 31 27 2c 20 27 56 61 6c 75 65 20 66 6f 72 20 43 1', 'Value for C
0910: 32 2e 31 27 20 29 3b 0a 0a 20 20 20 20 49 4e 53 2.1' );.. INS
0920: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 31 ERT INTO parent1
0930: 20 28 20 70 61 72 65 6e 74 31 6b 65 79 2c 20 63 ( parent1key, c
0940: 68 69 6c 64 31 6b 65 79 2c 20 63 68 69 6c 64 32 hild1key, child2
0950: 6b 65 79 20 29 0a 20 20 20 20 20 20 20 56 41 4c key ). VAL
0960: 55 45 53 20 28 20 32 2c 20 27 43 31 2e 32 27 2c UES ( 2, 'C1.2',
0970: 20 27 43 32 2e 32 27 20 29 3b 0a 20 20 20 20 49 'C2.2' );. I
0980: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 NSERT INTO child
0990: 32 20 28 20 63 68 69 6c 64 32 6b 65 79 2c 20 76 2 ( child2key, v
09a0: 61 6c 75 65 20 29 20 56 41 4c 55 45 53 20 28 20 alue ) VALUES (
09b0: 27 43 32 2e 32 27 2c 20 27 56 61 6c 75 65 20 66 'C2.2', 'Value f
09c0: 6f 72 20 43 32 2e 32 27 20 29 3b 0a 0a 20 20 20 or C2.2' );..
09d0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 INSERT INTO par
09e0: 65 6e 74 31 20 28 20 70 61 72 65 6e 74 31 6b 65 ent1 ( parent1ke
09f0: 79 2c 20 63 68 69 6c 64 31 6b 65 79 2c 20 63 68 y, child1key, ch
0a00: 69 6c 64 32 6b 65 79 20 29 0a 20 20 20 20 20 20 ild2key ).
0a10: 20 56 41 4c 55 45 53 20 28 20 33 2c 20 27 43 31 VALUES ( 3, 'C1
0a20: 2e 33 27 2c 20 27 43 32 2e 33 27 20 29 3b 0a 20 .3', 'C2.3' );.
0a30: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 INSERT INTO c
0a40: 68 69 6c 64 31 20 28 20 63 68 69 6c 64 31 6b 65 hild1 ( child1ke
0a50: 79 2c 20 76 61 6c 75 65 20 29 20 56 41 4c 55 45 y, value ) VALUE
0a60: 53 20 28 20 27 43 31 2e 33 27 2c 20 27 56 61 6c S ( 'C1.3', 'Val
0a70: 75 65 20 66 6f 72 20 43 31 2e 33 27 20 29 3b 0a ue for C1.3' );.
0a80: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 INSERT INTO
0a90: 63 68 69 6c 64 32 20 28 20 63 68 69 6c 64 32 6b child2 ( child2k
0aa0: 65 79 2c 20 76 61 6c 75 65 20 29 20 56 41 4c 55 ey, value ) VALU
0ab0: 45 53 20 28 20 27 43 32 2e 33 27 2c 20 27 56 61 ES ( 'C2.3', 'Va
0ac0: 6c 75 65 20 66 6f 72 20 43 32 2e 33 27 20 29 3b lue for C2.3' );
0ad0: 0a 0a 20 20 20 20 53 45 4c 45 43 54 20 70 61 72 .. SELECT par
0ae0: 65 6e 74 31 2e 70 61 72 65 6e 74 31 6b 65 79 2c ent1.parent1key,
0af0: 20 63 68 69 6c 64 31 2e 76 61 6c 75 65 2c 20 63 child1.value, c
0b00: 68 69 6c 64 32 2e 76 61 6c 75 65 0a 20 20 20 20 hild2.value.
0b10: 46 52 4f 4d 20 70 61 72 65 6e 74 31 0a 20 20 20 FROM parent1.
0b20: 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e LEFT OUTER JOIN
0b30: 20 63 68 69 6c 64 31 20 4f 4e 20 63 68 69 6c 64 child1 ON child
0b40: 31 2e 63 68 69 6c 64 31 6b 65 79 20 3d 20 70 61 1.child1key = pa
0b50: 72 65 6e 74 31 2e 63 68 69 6c 64 31 6b 65 79 0a rent1.child1key.
0b60: 20 20 20 20 49 4e 4e 45 52 20 4a 4f 49 4e 20 63 INNER JOIN c
0b70: 68 69 6c 64 32 20 4f 4e 20 63 68 69 6c 64 32 2e hild2 ON child2.
0b80: 63 68 69 6c 64 32 6b 65 79 20 3d 20 70 61 72 65 child2key = pare
0b90: 6e 74 31 2e 63 68 69 6c 64 32 6b 65 79 3b 0a 20 nt1.child2key;.
0ba0: 20 7d 0a 7d 20 7b 31 20 7b 56 61 6c 75 65 20 66 }.} {1 {Value f
0bb0: 6f 72 20 43 31 2e 31 7d 20 7b 56 61 6c 75 65 20 or C1.1} {Value
0bc0: 66 6f 72 20 43 32 2e 31 7d 20 32 20 7b 7d 20 7b for C2.1} 2 {} {
0bd0: 56 61 6c 75 65 20 66 6f 72 20 43 32 2e 32 7d 20 Value for C2.2}
0be0: 33 20 7b 56 61 6c 75 65 20 66 6f 72 20 43 31 2e 3 {Value for C1.
0bf0: 33 7d 20 7b 56 61 6c 75 65 20 66 6f 72 20 43 32 3} {Value for C2
0c00: 2e 33 7d 7d 0a 0a 69 66 63 61 70 61 62 6c 65 20 .3}}..ifcapable
0c10: 65 78 70 6c 61 69 6e 20 7b 0a 20 20 64 6f 5f 74 explain {. do_t
0c20: 65 73 74 20 77 68 65 72 65 33 2d 31 2e 32 2e 31 est where3-1.2.1
0c30: 20 7b 0a 20 20 20 20 20 65 78 70 6c 61 69 6e 5f {. explain_
0c40: 6e 6f 5f 74 72 61 63 65 20 7b 0a 20 20 20 20 20 no_trace {.
0c50: 20 20 53 45 4c 45 43 54 20 70 61 72 65 6e 74 31 SELECT parent1
0c60: 2e 70 61 72 65 6e 74 31 6b 65 79 2c 20 63 68 69 .parent1key, chi
0c70: 6c 64 31 2e 76 61 6c 75 65 2c 20 63 68 69 6c 64 ld1.value, child
0c80: 32 2e 76 61 6c 75 65 0a 20 20 20 20 20 20 20 46 2.value. F
0c90: 52 4f 4d 20 70 61 72 65 6e 74 31 0a 20 20 20 20 ROM parent1.
0ca0: 20 20 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f LEFT OUTER JO
0cb0: 49 4e 20 63 68 69 6c 64 31 20 4f 4e 20 63 68 69 IN child1 ON chi
0cc0: 6c 64 31 2e 63 68 69 6c 64 31 6b 65 79 20 3d 20 ld1.child1key =
0cd0: 70 61 72 65 6e 74 31 2e 63 68 69 6c 64 31 6b 65 parent1.child1ke
0ce0: 79 0a 20 20 20 20 20 20 20 49 4e 4e 45 52 20 4a y. INNER J
0cf0: 4f 49 4e 20 63 68 69 6c 64 32 20 4f 4e 20 63 68 OIN child2 ON ch
0d00: 69 6c 64 32 2e 63 68 69 6c 64 32 6b 65 79 20 3d ild2.child2key =
0d10: 20 70 61 72 65 6e 74 31 2e 63 68 69 6c 64 32 6b parent1.child2k
0d20: 65 79 3b 0a 20 20 20 20 20 7d 0a 20 20 7d 20 5b ey;. }. } [
0d30: 65 78 70 6c 61 69 6e 5f 6e 6f 5f 74 72 61 63 65 explain_no_trace
0d40: 20 7b 0a 20 20 20 20 20 20 20 53 45 4c 45 43 54 {. SELECT
0d50: 20 70 61 72 65 6e 74 31 2e 70 61 72 65 6e 74 31 parent1.parent1
0d60: 6b 65 79 2c 20 63 68 69 6c 64 31 2e 76 61 6c 75 key, child1.valu
0d70: 65 2c 20 63 68 69 6c 64 32 2e 76 61 6c 75 65 0a e, child2.value.
0d80: 20 20 20 20 20 20 20 46 52 4f 4d 20 70 61 72 65 FROM pare
0d90: 6e 74 31 0a 20 20 20 20 20 20 20 4c 45 46 54 20 nt1. LEFT
0da0: 4f 55 54 45 52 20 4a 4f 49 4e 20 63 68 69 6c 64 OUTER JOIN child
0db0: 31 20 4f 4e 20 70 61 72 65 6e 74 31 2e 63 68 69 1 ON parent1.chi
0dc0: 6c 64 31 6b 65 79 20 3d 20 63 68 69 6c 64 31 2e ld1key = child1.
0dd0: 63 68 69 6c 64 31 6b 65 79 20 0a 20 20 20 20 20 child1key .
0de0: 20 20 49 4e 4e 45 52 20 4a 4f 49 4e 20 63 68 69 INNER JOIN chi
0df0: 6c 64 32 20 4f 4e 20 63 68 69 6c 64 32 2e 63 68 ld2 ON child2.ch
0e00: 69 6c 64 32 6b 65 79 20 3d 20 70 61 72 65 6e 74 ild2key = parent
0e10: 31 2e 63 68 69 6c 64 32 6b 65 79 3b 0a 20 20 20 1.child2key;.
0e20: 20 20 7d 5d 0a 7d 0a 0a 23 20 54 68 69 73 20 70 }].}..# This p
0e30: 72 6f 63 65 64 75 72 65 20 65 78 65 63 75 74 65 rocedure execute
0e40: 73 20 74 68 65 20 53 51 4c 2e 20 20 54 68 65 6e s the SQL. Then
0e50: 20 69 74 20 61 70 70 65 6e 64 73 20 0a 23 20 74 it appends .# t
0e60: 68 65 20 3a 3a 73 71 6c 69 74 65 5f 71 75 65 72 he ::sqlite_quer
0e70: 79 5f 70 6c 61 6e 20 76 61 72 69 61 62 6c 65 2e y_plan variable.
0e80: 0a 23 0a 70 72 6f 63 20 71 75 65 72 79 70 6c 61 .#.proc querypla
0e90: 6e 20 7b 73 71 6c 7d 20 7b 0a 20 20 73 65 74 20 n {sql} {. set
0ea0: 3a 3a 73 71 6c 69 74 65 5f 73 6f 72 74 5f 63 6f ::sqlite_sort_co
0eb0: 75 6e 74 20 30 0a 20 20 73 65 74 20 64 61 74 61 unt 0. set data
0ec0: 20 5b 65 78 65 63 73 71 6c 20 24 73 71 6c 5d 0a [execsql $sql].
0ed0: 20 20 72 65 74 75 72 6e 20 5b 63 6f 6e 63 61 74 return [concat
0ee0: 20 24 64 61 74 61 20 24 3a 3a 73 71 6c 69 74 65 $data $::sqlite
0ef0: 5f 71 75 65 72 79 5f 70 6c 61 6e 5d 0a 7d 0a 0a _query_plan].}..
0f00: 0a 23 20 49 66 20 79 6f 75 20 68 61 76 65 20 61 .# If you have a
0f10: 20 66 72 6f 6d 20 63 6c 61 75 73 65 20 6f 66 20 from clause of
0f20: 74 68 65 20 66 6f 72 6d 3a 20 20 20 41 20 42 20 the form: A B
0f30: 43 20 6c 65 66 74 20 6a 6f 69 6e 20 44 0a 23 20 C left join D.#
0f40: 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 74 then make sure t
0f50: 68 65 20 71 75 65 72 79 20 6f 70 74 69 6d 69 7a he query optimiz
0f60: 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 er is able to re
0f70: 6f 72 64 65 72 20 74 68 65 20 0a 23 20 41 20 42 order the .# A B
0f80: 20 43 20 70 61 72 74 20 61 6e 79 77 61 79 20 69 C part anyway i
0f90: 74 20 77 61 6e 74 73 2e 20 0a 23 0a 23 20 46 6f t wants. .#.# Fo
0fa0: 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 78 20 llowing the fix
0fb0: 74 6f 20 74 69 63 6b 65 74 20 23 31 36 35 32 2c to ticket #1652,
0fc0: 20 74 68 65 72 65 20 77 61 73 20 61 20 74 69 6d there was a tim
0fd0: 65 20 77 68 65 6e 0a 23 20 74 68 65 20 43 20 74 e when.# the C t
0fe0: 61 62 6c 65 20 77 6f 75 6c 64 20 6e 6f 74 20 72 able would not r
0ff0: 65 6f 72 64 65 72 2e 20 20 53 6f 20 74 68 65 20 eorder. So the
1000: 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 6f 72 64 65 following reorde
1010: 72 69 6e 67 73 0a 23 20 77 65 72 65 20 70 6f 73 rings.# were pos
1020: 73 69 62 6c 65 3a 0a 23 0a 23 20 20 20 20 20 20 sible:.#.#
1030: 20 20 20 20 20 20 41 20 42 20 43 20 6c 65 66 74 A B C left
1040: 20 6a 6f 69 6e 20 44 0a 23 20 20 20 20 20 20 20 join D.#
1050: 20 20 20 20 20 42 20 41 20 43 20 6c 65 66 74 20 B A C left
1060: 6a 6f 69 6e 20 44 0a 23 0a 23 20 42 75 74 20 74 join D.#.# But t
1070: 68 65 73 65 20 72 65 6f 72 64 65 72 73 20 77 65 hese reorders we
1080: 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 0a 23 re not allowed.#
1090: 0a 23 20 20 20 20 20 20 20 20 20 20 20 20 43 20 .# C
10a0: 41 20 42 20 6c 65 66 74 20 6a 6f 69 6e 20 44 0a A B left join D.
10b0: 23 20 20 20 20 20 20 20 20 20 20 20 20 41 20 43 # A C
10c0: 20 42 20 6c 65 66 74 20 6a 6f 69 6e 20 44 0a 23 B left join D.#
10d0: 20 20 20 20 20 20 20 20 20 20 20 20 43 20 42 20 C B
10e0: 41 20 6c 65 66 74 20 6a 6f 69 6e 20 44 0a 23 20 A left join D.#
10f0: 20 20 20 20 20 20 20 20 20 20 20 42 20 43 20 41 B C A
1100: 20 6c 65 66 74 20 6a 6f 69 6e 20 44 0a 23 0a 23 left join D.#.#
1110: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
1120: 65 73 74 73 20 61 72 65 20 68 65 72 65 20 74 6f ests are here to
1130: 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 verify that the
1140: 20 6c 61 74 74 65 72 20 66 6f 75 72 0a 23 20 72 latter four.# r
1150: 65 6f 72 64 65 72 69 6e 67 73 20 61 72 65 20 61 eorderings are a
1160: 6c 6c 6f 77 65 64 20 61 67 61 69 6e 2e 0a 23 0a llowed again..#.
1170: 64 6f 5f 74 65 73 74 20 77 68 65 72 65 33 2d 32 do_test where3-2
1180: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b .1 {. execsql {
1190: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c . CREATE TABL
11a0: 45 20 74 41 28 61 70 6b 20 69 6e 74 65 67 65 72 E tA(apk integer
11b0: 20 70 72 69 6d 61 72 79 20 6b 65 79 2c 20 61 78 primary key, ax
11c0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 );. CREATE TA
11d0: 42 4c 45 20 74 42 28 62 70 6b 20 69 6e 74 65 67 BLE tB(bpk integ
11e0: 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 2c 20 er primary key,
11f0: 62 78 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 bx);. CREATE
1200: 54 41 42 4c 45 20 74 43 28 63 70 6b 20 69 6e 74 TABLE tC(cpk int
1210: 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 eger primary key
1220: 2c 20 63 78 29 3b 0a 20 20 20 20 43 52 45 41 54 , cx);. CREAT
1230: 45 20 54 41 42 4c 45 20 74 44 28 64 70 6b 20 69 E TABLE tD(dpk i
1240: 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b nteger primary k
1250: 65 79 2c 20 64 78 29 3b 0a 20 20 7d 0a 20 20 71 ey, dx);. }. q
1260: 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 20 20 53 ueryplan {. S
1270: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 41 2c ELECT * FROM tA,
1280: 20 74 42 2c 20 74 43 20 4c 45 46 54 20 4a 4f 49 tB, tC LEFT JOI
1290: 4e 20 74 44 20 4f 4e 20 64 70 6b 3d 63 78 0a 20 N tD ON dpk=cx.
12a0: 20 20 20 20 57 48 45 52 45 20 63 70 6b 3d 62 78 WHERE cpk=bx
12b0: 20 41 4e 44 20 62 70 6b 3d 61 78 0a 20 20 7d 0a AND bpk=ax. }.
12c0: 7d 20 7b 74 41 20 7b 7d 20 74 42 20 2a 20 74 43 } {tA {} tB * tC
12d0: 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 65 73 74 * tD *}.do_test
12e0: 20 77 68 65 72 65 33 2d 32 2e 31 2e 31 20 7b 0a where3-2.1.1 {.
12f0: 20 20 71 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 queryplan {.
1300: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 SELECT * FROM
1310: 74 41 2c 20 74 42 2c 20 74 43 20 4c 45 46 54 20 tA, tB, tC LEFT
1320: 4a 4f 49 4e 20 74 44 20 4f 4e 20 63 78 3d 64 70 JOIN tD ON cx=dp
1330: 6b 0a 20 20 20 20 20 57 48 45 52 45 20 63 70 6b k. WHERE cpk
1340: 3d 62 78 20 41 4e 44 20 62 70 6b 3d 61 78 0a 20 =bx AND bpk=ax.
1350: 20 7d 0a 7d 20 7b 74 41 20 7b 7d 20 74 42 20 2a }.} {tA {} tB *
1360: 20 74 43 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 tC * tD *}.do_t
1370: 65 73 74 20 77 68 65 72 65 33 2d 32 2e 31 2e 32 est where3-2.1.2
1380: 20 7b 0a 20 20 71 75 65 72 79 70 6c 61 6e 20 7b {. queryplan {
1390: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 . SELECT * FR
13a0: 4f 4d 20 74 41 2c 20 74 42 2c 20 74 43 20 4c 45 OM tA, tB, tC LE
13b0: 46 54 20 4a 4f 49 4e 20 74 44 20 4f 4e 20 63 78 FT JOIN tD ON cx
13c0: 3d 64 70 6b 0a 20 20 20 20 20 57 48 45 52 45 20 =dpk. WHERE
13d0: 62 78 3d 63 70 6b 20 41 4e 44 20 62 70 6b 3d 61 bx=cpk AND bpk=a
13e0: 78 0a 20 20 7d 0a 7d 20 7b 74 41 20 7b 7d 20 74 x. }.} {tA {} t
13f0: 42 20 2a 20 74 43 20 2a 20 74 44 20 2a 7d 0a 64 B * tC * tD *}.d
1400: 6f 5f 74 65 73 74 20 77 68 65 72 65 33 2d 32 2e o_test where3-2.
1410: 31 2e 33 20 7b 0a 20 20 71 75 65 72 79 70 6c 61 1.3 {. querypla
1420: 6e 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a n {. SELECT *
1430: 20 46 52 4f 4d 20 74 41 2c 20 74 42 2c 20 74 43 FROM tA, tB, tC
1440: 20 4c 45 46 54 20 4a 4f 49 4e 20 74 44 20 4f 4e LEFT JOIN tD ON
1450: 20 63 78 3d 64 70 6b 0a 20 20 20 20 20 57 48 45 cx=dpk. WHE
1460: 52 45 20 62 78 3d 63 70 6b 20 41 4e 44 20 61 78 RE bx=cpk AND ax
1470: 3d 62 70 6b 0a 20 20 7d 0a 7d 20 7b 74 41 20 7b =bpk. }.} {tA {
1480: 7d 20 74 42 20 2a 20 74 43 20 2a 20 74 44 20 2a } tB * tC * tD *
1490: 7d 0a 64 6f 5f 74 65 73 74 20 77 68 65 72 65 33 }.do_test where3
14a0: 2d 32 2e 31 2e 34 20 7b 0a 20 20 71 75 65 72 79 -2.1.4 {. query
14b0: 70 6c 61 6e 20 7b 0a 20 20 20 20 53 45 4c 45 43 plan {. SELEC
14c0: 54 20 2a 20 46 52 4f 4d 20 74 41 2c 20 74 42 2c T * FROM tA, tB,
14d0: 20 74 43 20 4c 45 46 54 20 4a 4f 49 4e 20 74 44 tC LEFT JOIN tD
14e0: 20 4f 4e 20 64 70 6b 3d 63 78 0a 20 20 20 20 20 ON dpk=cx.
14f0: 57 48 45 52 45 20 62 78 3d 63 70 6b 20 41 4e 44 WHERE bx=cpk AND
1500: 20 61 78 3d 62 70 6b 0a 20 20 7d 0a 7d 20 7b 74 ax=bpk. }.} {t
1510: 41 20 7b 7d 20 74 42 20 2a 20 74 43 20 2a 20 74 A {} tB * tC * t
1520: 44 20 2a 7d 0a 64 6f 5f 74 65 73 74 20 77 68 65 D *}.do_test whe
1530: 72 65 33 2d 32 2e 31 2e 35 20 7b 0a 20 20 71 75 re3-2.1.5 {. qu
1540: 65 72 79 70 6c 61 6e 20 7b 0a 20 20 20 20 53 45 eryplan {. SE
1550: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 41 2c 20 LECT * FROM tA,
1560: 74 42 2c 20 74 43 20 4c 45 46 54 20 4a 4f 49 4e tB, tC LEFT JOIN
1570: 20 74 44 20 4f 4e 20 64 70 6b 3d 63 78 0a 20 20 tD ON dpk=cx.
1580: 20 20 20 57 48 45 52 45 20 63 70 6b 3d 62 78 20 WHERE cpk=bx
1590: 41 4e 44 20 61 78 3d 62 70 6b 0a 20 20 7d 0a 7d AND ax=bpk. }.}
15a0: 20 7b 74 41 20 7b 7d 20 74 42 20 2a 20 74 43 20 {tA {} tB * tC
15b0: 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 65 73 74 20 * tD *}.do_test
15c0: 77 68 65 72 65 33 2d 32 2e 32 20 7b 0a 20 20 71 where3-2.2 {. q
15d0: 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 20 20 53 ueryplan {. S
15e0: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 41 2c ELECT * FROM tA,
15f0: 20 74 42 2c 20 74 43 20 4c 45 46 54 20 4a 4f 49 tB, tC LEFT JOI
1600: 4e 20 74 44 20 4f 4e 20 64 70 6b 3d 63 78 0a 20 N tD ON dpk=cx.
1610: 20 20 20 20 57 48 45 52 45 20 63 70 6b 3d 62 78 WHERE cpk=bx
1620: 20 41 4e 44 20 61 70 6b 3d 62 78 0a 20 20 7d 0a AND apk=bx. }.
1630: 7d 20 7b 74 42 20 7b 7d 20 74 41 20 2a 20 74 43 } {tB {} tA * tC
1640: 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 65 73 74 * tD *}.do_test
1650: 20 77 68 65 72 65 33 2d 32 2e 33 20 7b 0a 20 20 where3-2.3 {.
1660: 71 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 20 20 queryplan {.
1670: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 41 SELECT * FROM tA
1680: 2c 20 74 42 2c 20 74 43 20 4c 45 46 54 20 4a 4f , tB, tC LEFT JO
1690: 49 4e 20 74 44 20 4f 4e 20 64 70 6b 3d 63 78 0a IN tD ON dpk=cx.
16a0: 20 20 20 20 20 57 48 45 52 45 20 63 70 6b 3d 62 WHERE cpk=b
16b0: 78 20 41 4e 44 20 61 70 6b 3d 62 78 0a 20 20 7d x AND apk=bx. }
16c0: 0a 7d 20 7b 74 42 20 7b 7d 20 74 41 20 2a 20 74 .} {tB {} tA * t
16d0: 43 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 65 73 C * tD *}.do_tes
16e0: 74 20 77 68 65 72 65 33 2d 32 2e 34 20 7b 0a 20 t where3-2.4 {.
16f0: 20 71 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 20 queryplan {.
1700: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 SELECT * FROM t
1710: 41 2c 20 74 42 2c 20 74 43 20 4c 45 46 54 20 4a A, tB, tC LEFT J
1720: 4f 49 4e 20 74 44 20 4f 4e 20 64 70 6b 3d 63 78 OIN tD ON dpk=cx
1730: 0a 20 20 20 20 20 57 48 45 52 45 20 61 70 6b 3d . WHERE apk=
1740: 63 78 20 41 4e 44 20 62 70 6b 3d 61 78 0a 20 20 cx AND bpk=ax.
1750: 7d 0a 7d 20 7b 74 43 20 7b 7d 20 74 41 20 2a 20 }.} {tC {} tA *
1760: 74 42 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 65 tB * tD *}.do_te
1770: 73 74 20 77 68 65 72 65 33 2d 32 2e 35 20 7b 0a st where3-2.5 {.
1780: 20 20 71 75 65 72 79 70 6c 61 6e 20 7b 0a 20 20 queryplan {.
1790: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 SELECT * FROM
17a0: 74 41 2c 20 74 42 2c 20 74 43 20 4c 45 46 54 20 tA, tB, tC LEFT
17b0: 4a 4f 49 4e 20 74 44 20 4f 4e 20 64 70 6b 3d 63 JOIN tD ON dpk=c
17c0: 78 0a 20 20 20 20 20 57 48 45 52 45 20 63 70 6b x. WHERE cpk
17d0: 3d 61 78 20 41 4e 44 20 62 70 6b 3d 63 78 0a 20 =ax AND bpk=cx.
17e0: 20 7d 0a 7d 20 7b 74 41 20 7b 7d 20 74 43 20 2a }.} {tA {} tC *
17f0: 20 74 42 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f 74 tB * tD *}.do_t
1800: 65 73 74 20 77 68 65 72 65 33 2d 32 2e 35 20 7b est where3-2.5 {
1810: 0a 20 20 71 75 65 72 79 70 6c 61 6e 20 7b 0a 20 . queryplan {.
1820: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d SELECT * FROM
1830: 20 74 41 2c 20 74 42 2c 20 74 43 20 4c 45 46 54 tA, tB, tC LEFT
1840: 20 4a 4f 49 4e 20 74 44 20 4f 4e 20 64 70 6b 3d JOIN tD ON dpk=
1850: 63 78 0a 20 20 20 20 20 57 48 45 52 45 20 62 70 cx. WHERE bp
1860: 6b 3d 63 78 20 41 4e 44 20 61 70 6b 3d 62 78 0a k=cx AND apk=bx.
1870: 20 20 7d 0a 7d 20 7b 74 43 20 7b 7d 20 74 42 20 }.} {tC {} tB
1880: 2a 20 74 41 20 2a 20 74 44 20 2a 7d 0a 64 6f 5f * tA * tD *}.do_
1890: 74 65 73 74 20 77 68 65 72 65 33 2d 32 2e 36 20 test where3-2.6
18a0: 7b 0a 20 20 71 75 65 72 79 70 6c 61 6e 20 7b 0a {. queryplan {.
18b0: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f SELECT * FRO
18c0: 4d 20 74 41 2c 20 74 42 2c 20 74 43 20 4c 45 46 M tA, tB, tC LEF
18d0: 54 20 4a 4f 49 4e 20 74 44 20 4f 4e 20 64 70 6b T JOIN tD ON dpk
18e0: 3d 63 78 0a 20 20 20 20 20 57 48 45 52 45 20 63 =cx. WHERE c
18f0: 70 6b 3d 62 78 20 41 4e 44 20 61 70 6b 3d 63 78 pk=bx AND apk=cx
1900: 0a 20 20 7d 0a 7d 20 7b 74 42 20 7b 7d 20 74 43 . }.} {tB {} tC
1910: 20 2a 20 74 41 20 2a 20 74 44 20 2a 7d 0a 0a 0a * tA * tD *}...
1920: 66 69 6e 69 73 68 5f 74 65 73 74 0a finish_test.