whereF.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # 2012 November 9
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. #
  12. # Test cases for query planning decisions.
  13. #
  14. # The tests in this file demonstrate the behaviour of the query planner
  15. # in determining the order in which joined tables are scanned.
  16. #
  17. # Assume there are two tables being joined - t1 and t2. Each has a cost
  18. # if it is the outer loop, and a cost if it is the inner loop. As follows:
  19. #
  20. # t1(outer) - cost of scanning t1 as the outer loop.
  21. # t1(inner) - cost of scanning t1 as the inner loop.
  22. # t2(outer) - cost of scanning t2 as the outer loop.
  23. # t2(inner) - cost of scanning t2 as the inner loop.
  24. #
  25. # Depending on the order in which the planner nests the scans, the total
  26. # cost of the join query is one of:
  27. #
  28. # t1(outer) * t2(inner)
  29. # t2(outer) * t1(inner)
  30. #
  31. # The tests in this file attempt to verify that the planner nests joins in
  32. # the correct order when the following are true:
  33. #
  34. # + (t1(outer) * t2(inner)) > (t1(inner) * t2(outer)
  35. # + t1(outer) < t2(outer)
  36. #
  37. # In other words, when the best overall query plan has t2 as the outer loop,
  38. # but when the outer loop is considered independent of the inner, t1 is the
  39. # most efficient choice.
  40. #
  41. # In order to make them more predictable, automatic indexes are turned off for
  42. # the tests in this file.
  43. #
  44. set testdir [file dirname $argv0]
  45. source $testdir/tester.tcl
  46. set testprefix whereF
  47. do_execsql_test 1.0 {
  48. PRAGMA automatic_index = 0;
  49. CREATE TABLE t1(a, b, c);
  50. CREATE TABLE t2(d, e, f);
  51. CREATE UNIQUE INDEX i1 ON t1(a);
  52. CREATE UNIQUE INDEX i2 ON t2(d);
  53. } {}
  54. foreach {tn sql} {
  55. 1 "SELECT * FROM t1, t2 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
  56. 2 "SELECT * FROM t2, t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
  57. 3 "SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
  58. } {
  59. do_test 1.$tn {
  60. db eval "EXPLAIN QUERY PLAN $sql"
  61. } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/}
  62. }
  63. do_execsql_test 2.0 {
  64. DROP TABLE t1;
  65. DROP TABLE t2;
  66. CREATE TABLE t1(a, b, c);
  67. CREATE TABLE t2(d, e, f);
  68. CREATE UNIQUE INDEX i1 ON t1(a);
  69. CREATE UNIQUE INDEX i2 ON t1(b);
  70. CREATE UNIQUE INDEX i3 ON t2(d);
  71. } {}
  72. foreach {tn sql} {
  73. 1 "SELECT * FROM t1, t2 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
  74. 2 "SELECT * FROM t2, t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
  75. 3 "SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
  76. } {
  77. do_test 2.$tn {
  78. db eval "EXPLAIN QUERY PLAN $sql"
  79. } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/}
  80. }
  81. do_execsql_test 3.0 {
  82. DROP TABLE t1;
  83. DROP TABLE t2;
  84. CREATE TABLE t1(a, b, c);
  85. CREATE TABLE t2(d, e, f);
  86. CREATE UNIQUE INDEX i1 ON t1(a, b);
  87. CREATE INDEX i2 ON t2(d);
  88. } {}
  89. foreach {tn sql} {
  90. 1 {SELECT t1.a, t1.b, t2.d, t2.e FROM t1, t2
  91. WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
  92. 2 {SELECT t1.a, t1.b, t2.d, t2.e FROM t2, t1
  93. WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
  94. 3 {SELECT t1.a, t1.b, t2.d, t2.e FROM t2 CROSS JOIN t1
  95. WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
  96. } {
  97. do_test 3.$tn {
  98. db eval "EXPLAIN QUERY PLAN $sql"
  99. } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/}
  100. }
  101. do_execsql_test 4.0 {
  102. CREATE TABLE t4(a,b,c,d,e, PRIMARY KEY(a,b,c));
  103. CREATE INDEX t4adc ON t4(a,d,c);
  104. CREATE UNIQUE INDEX t4aebc ON t4(a,e,b,c);
  105. EXPLAIN QUERY PLAN SELECT rowid FROM t4 WHERE a=? AND b=?;
  106. } {/a=. AND b=./}
  107. finish_test