OLD | NEW |
1 # 2008 December 30 | 1 # 2008 December 30 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 SELECT t3.x, t1.a, coalesce(t2.a,9999) | 351 SELECT t3.x, t1.a, coalesce(t2.a,9999) |
352 FROM t3 JOIN | 352 FROM t3 JOIN |
353 t1 LEFT JOIN t2 ON (t1.c=t2.c AND t1.d=t2.d) OR (t1.f)=t2.f | 353 t1 LEFT JOIN t2 ON (t1.c=t2.c AND t1.d=t2.d) OR (t1.f)=t2.f |
354 WHERE t1.a=t3.y OR t1.b=t3.y*11 OR (t1.c=27027 AND round(t1.d)==80) | 354 WHERE t1.a=t3.y OR t1.b=t3.y*11 OR (t1.c=27027 AND round(t1.d)==80) |
355 ORDER BY 1, 2, 3 | 355 ORDER BY 1, 2, 3 |
356 } | 356 } |
357 } {1 80 2 1 80 28 1 80 54 1 80 80 2 80 2 2 80 28 2 80 54 2 80 80 scan 1 sort 1} | 357 } {1 80 2 1 80 28 1 80 54 1 80 80 2 80 2 2 80 28 2 80 54 2 80 80 scan 1 sort 1} |
358 | 358 |
359 | 359 |
360 ifcapable explain { | 360 ifcapable explain { |
361 do_test where9-3.1 { | 361 do_execsql_test where9-3.1 { |
362 set r [db eval { | 362 EXPLAIN QUERY PLAN |
363 EXPLAIN QUERY PLAN | 363 SELECT t2.a FROM t1, t2 |
364 SELECT t2.a FROM t1, t2 | 364 WHERE t1.a=80 AND ((t1.c=t2.c AND t1.d=t2.d) OR t1.f=t2.f) |
365 WHERE t1.a=80 | 365 } { |
366 AND ((t1.c=t2.c AND t1.d=t2.d) OR t1.f=t2.f) | 366 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} |
367 }] | 367 0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?) (~2 rows)} |
368 set a [expr {[lsearch $r {TABLE t2 VIA MULTI-INDEX UNION}]>=0}] | 368 0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~10 rows)} |
369 set b [expr {[lsearch $r {TABLE t2 WITH INDEX t2f}]>=0}] | 369 } |
370 set c [expr {([lsearch $r {TABLE t2 WITH INDEX t2c}]>=0)+ | 370 do_execsql_test where9-3.2 { |
371 [lsearch $r {TABLE t2 WITH INDEX t2d}]>=0}] | 371 EXPLAIN QUERY PLAN |
372 concat $a $b $c | 372 SELECT coalesce(t2.a,9999) |
373 } {1 1 1} | 373 FROM t1 LEFT JOIN t2 ON (t1.c+1=t2.c AND t1.d=t2.d) OR (t1.f||'x')=t2.f |
374 do_test where9-3.2 { | 374 WHERE t1.a=80 |
375 set r [db eval { | 375 } { |
376 EXPLAIN QUERY PLAN | 376 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} |
377 SELECT coalesce(t2.a,9999) | 377 0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?) (~2 rows)} |
378 FROM t1 LEFT JOIN t2 ON (t1.c+1=t2.c AND t1.d=t2.d) OR (t1.f||'x')=t2.f | 378 0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~10 rows)} |
379 WHERE t1.a=80 | 379 } |
380 }] | |
381 set a [expr {[lsearch $r {TABLE t2 VIA MULTI-INDEX UNION}]>=0}] | |
382 set b [expr {[lsearch $r {TABLE t2 WITH INDEX t2f}]>=0}] | |
383 set c [expr {([lsearch $r {TABLE t2 WITH INDEX t2c}]>=0)+ | |
384 [lsearch $r {TABLE t2 WITH INDEX t2d}]>=0}] | |
385 concat $a $b $c | |
386 } {1 1 1} | |
387 } | 380 } |
388 | 381 |
389 # Make sure that INDEXED BY and multi-index OR clauses play well with | 382 # Make sure that INDEXED BY and multi-index OR clauses play well with |
390 # one another. | 383 # one another. |
391 # | 384 # |
392 do_test where9-4.1 { | 385 do_test where9-4.1 { |
393 count_steps { | 386 count_steps { |
394 SELECT a FROM t1 | 387 SELECT a FROM t1 |
395 WHERE b>1000 | 388 WHERE b>1000 |
396 AND (c=31031 OR d IS NULL) | 389 AND (c=31031 OR d IS NULL) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 WHERE b>1000 | 444 WHERE b>1000 |
452 AND (c=31031 OR d IS NULL) | 445 AND (c=31031 OR d IS NULL) |
453 ORDER BY +a | 446 ORDER BY +a |
454 } | 447 } |
455 } {1 {cannot use index: t1d}} | 448 } {1 {cannot use index: t1d}} |
456 | 449 |
457 ifcapable explain { | 450 ifcapable explain { |
458 # The (c=31031 OR d IS NULL) clause is preferred over b>1000 because | 451 # The (c=31031 OR d IS NULL) clause is preferred over b>1000 because |
459 # the former is an equality test which is expected to return fewer rows. | 452 # the former is an equality test which is expected to return fewer rows. |
460 # | 453 # |
461 do_test where9-5.1 { | 454 do_execsql_test where9-5.1 { |
462 set r [db eval { | 455 EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>1000 AND (c=31031 OR d IS NULL) |
463 EXPLAIN QUERY PLAN | 456 } { |
464 SELECT a FROM t1 | 457 0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?) (~10 rows)} |
465 WHERE b>1000 | 458 0 0 0 {SEARCH TABLE t1 USING INDEX t1d (d=?) (~10 rows)} |
466 AND (c=31031 OR d IS NULL) | 459 } |
467 }] | |
468 set a [expr {[lsearch $r {TABLE t1 VIA MULTI-INDEX UNION}]>=0}] | |
469 set b [expr {[lsearch $r {TABLE t1 WITH INDEX t1b}]>=0}] | |
470 concat $a $b | |
471 } {1 0} | |
472 | 460 |
473 # In contrast, b=1000 is preferred over any OR-clause. | 461 # In contrast, b=1000 is preferred over any OR-clause. |
474 # | 462 # |
475 do_test where9-5.2 { | 463 do_execsql_test where9-5.2 { |
476 set r [db eval { | 464 EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b=1000 AND (c=31031 OR d IS NULL) |
477 EXPLAIN QUERY PLAN | 465 } { |
478 SELECT a FROM t1 | 466 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~5 rows)} |
479 WHERE b=1000 | 467 } |
480 AND (c=31031 OR d IS NULL) | |
481 }] | |
482 set a [expr {[lsearch $r {TABLE t1 VIA MULTI-INDEX UNION}]>=0}] | |
483 set b [expr {[lsearch $r {TABLE t1 WITH INDEX t1b}]>=0}] | |
484 concat $a $b | |
485 } {0 1} | |
486 | 468 |
487 # Likewise, inequalities in an AND are preferred over inequalities in | 469 # Likewise, inequalities in an AND are preferred over inequalities in |
488 # an OR. | 470 # an OR. |
489 # | 471 # |
490 do_test where9-5.3 { | 472 do_execsql_test where9-5.3 { |
491 set r [db eval { | 473 EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>1000 AND (c>=31031 OR d IS NULL) |
492 EXPLAIN QUERY PLAN | 474 } { |
493 SELECT a FROM t1 | 475 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>?) (~125000 rows)} |
494 WHERE b>1000 | 476 } |
495 AND (c>=31031 OR d IS NULL) | |
496 }] | |
497 set a [expr {[lsearch $r {TABLE t1 VIA MULTI-INDEX UNION}]>=0}] | |
498 set b [expr {[lsearch $r {TABLE t1 WITH INDEX t1b}]>=0}] | |
499 concat $a $b | |
500 } {0 1} | |
501 } | 477 } |
502 | 478 |
503 ############################################################################ | 479 ############################################################################ |
504 # Make sure OR-clauses work correctly on UPDATE and DELETE statements. | 480 # Make sure OR-clauses work correctly on UPDATE and DELETE statements. |
505 | 481 |
506 do_test where9-6.2.1 { | 482 do_test where9-6.2.1 { |
507 db eval {SELECT count(*) FROM t1 UNION ALL SELECT a FROM t1 WHERE a>=85} | 483 db eval {SELECT count(*) FROM t1 UNION ALL SELECT a FROM t1 WHERE a>=85} |
508 } {99 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99} | 484 } {99 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99} |
509 | 485 |
510 do_test where9-6.2.2 { ;# Deletes entries 90 91 92 96 97 99 | 486 do_test where9-6.2.2 { ;# Deletes entries 90 91 92 96 97 99 |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 do_test where9-6.8.2 { | 777 do_test where9-6.8.2 { |
802 catchsql { | 778 catchsql { |
803 UPDATE t1 INDEXED BY t1b SET a=a+100 | 779 UPDATE t1 INDEXED BY t1b SET a=a+100 |
804 WHERE (b IS NULL AND c NOT NULL AND d NOT NULL) | 780 WHERE (b IS NULL AND c NOT NULL AND d NOT NULL) |
805 OR (b NOT NULL AND c IS NULL AND d NOT NULL) | 781 OR (b NOT NULL AND c IS NULL AND d NOT NULL) |
806 OR (b NOT NULL AND c NOT NULL AND d IS NULL) | 782 OR (b NOT NULL AND c NOT NULL AND d IS NULL) |
807 } | 783 } |
808 } {1 {cannot use index: t1b}} | 784 } {1 {cannot use index: t1b}} |
809 | 785 |
810 finish_test | 786 finish_test |
OLD | NEW |