analyze6.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # 2011 March 3
  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. # This file implements tests for SQLite library. The focus of the tests
  13. # in this file a corner-case query planner optimization involving the
  14. # join order of two tables of different sizes.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !stat4&&!stat3 {
  19. finish_test
  20. return
  21. }
  22. set testprefix analyze6
  23. proc eqp {sql {db db}} {
  24. uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
  25. }
  26. do_test analyze6-1.0 {
  27. db eval {
  28. CREATE TABLE cat(x INT, yz TEXT);
  29. CREATE UNIQUE INDEX catx ON cat(x);
  30. /* Give cat 16 unique integers */
  31. INSERT INTO cat(x) VALUES(1);
  32. INSERT INTO cat(x) VALUES(2);
  33. INSERT INTO cat(x) SELECT x+2 FROM cat;
  34. INSERT INTO cat(x) SELECT x+4 FROM cat;
  35. INSERT INTO cat(x) SELECT x+8 FROM cat;
  36. CREATE TABLE ev(y INT);
  37. CREATE INDEX evy ON ev(y);
  38. /* ev will hold 32 copies of 16 integers found in cat */
  39. INSERT INTO ev SELECT x FROM cat;
  40. INSERT INTO ev SELECT x FROM cat;
  41. INSERT INTO ev SELECT y FROM ev;
  42. INSERT INTO ev SELECT y FROM ev;
  43. INSERT INTO ev SELECT y FROM ev;
  44. INSERT INTO ev SELECT y FROM ev;
  45. ANALYZE;
  46. SELECT count(*) FROM cat;
  47. SELECT count(*) FROM ev;
  48. }
  49. } {16 512}
  50. # The lowest cost plan is to scan CAT and for each integer there, do a single
  51. # lookup of the first corresponding entry in EV then read off the equal values
  52. # in EV. (Prior to the 2011-03-04 enhancement to where.c, this query would
  53. # have used EV for the outer loop instead of CAT - which was about 3x slower.)
  54. #
  55. do_test analyze6-1.1 {
  56. eqp {SELECT count(*) FROM ev, cat WHERE x=y}
  57. } {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
  58. # The same plan is chosen regardless of the order of the tables in the
  59. # FROM clause.
  60. #
  61. do_test analyze6-1.2 {
  62. eqp {SELECT count(*) FROM cat, ev WHERE x=y}
  63. } {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
  64. # Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
  65. # If ANALYZE is run on an empty table, make sure indices are used
  66. # on the table.
  67. #
  68. do_test analyze6-2.1 {
  69. execsql {
  70. CREATE TABLE t201(x INTEGER PRIMARY KEY, y UNIQUE, z);
  71. CREATE INDEX t201z ON t201(z);
  72. ANALYZE;
  73. }
  74. eqp {SELECT * FROM t201 WHERE z=5}
  75. } {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
  76. do_test analyze6-2.2 {
  77. eqp {SELECT * FROM t201 WHERE y=5}
  78. } {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
  79. do_test analyze6-2.3 {
  80. eqp {SELECT * FROM t201 WHERE x=5}
  81. } {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
  82. do_test analyze6-2.4 {
  83. execsql {
  84. INSERT INTO t201 VALUES(1,2,3);
  85. ANALYZE t201;
  86. }
  87. eqp {SELECT * FROM t201 WHERE z=5}
  88. } {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
  89. do_test analyze6-2.5 {
  90. eqp {SELECT * FROM t201 WHERE y=5}
  91. } {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
  92. do_test analyze6-2.6 {
  93. eqp {SELECT * FROM t201 WHERE x=5}
  94. } {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
  95. do_test analyze6-2.7 {
  96. execsql {
  97. INSERT INTO t201 VALUES(4,5,7);
  98. INSERT INTO t201 SELECT x+100, y+100, z+100 FROM t201;
  99. INSERT INTO t201 SELECT x+200, y+200, z+200 FROM t201;
  100. INSERT INTO t201 SELECT x+400, y+400, z+400 FROM t201;
  101. ANALYZE t201;
  102. }
  103. eqp {SELECT * FROM t201 WHERE z=5}
  104. } {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
  105. do_test analyze6-2.8 {
  106. eqp {SELECT * FROM t201 WHERE y=5}
  107. } {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
  108. do_test analyze6-2.9 {
  109. eqp {SELECT * FROM t201 WHERE x=5}
  110. } {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
  111. finish_test