analyze7.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # 2011 April 1
  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. # This file implements regression tests for SQLite library.
  12. # This file implements tests for the ANALYZE command when an idnex
  13. # name is given as the argument.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # There is nothing to test if ANALYZE is disable for this build.
  18. #
  19. ifcapable {!analyze||!vtab} {
  20. finish_test
  21. return
  22. }
  23. # Generate some test data
  24. #
  25. do_test analyze7-1.0 {
  26. load_static_extension db wholenumber
  27. execsql {
  28. CREATE TABLE t1(a,b,c,d);
  29. CREATE INDEX t1a ON t1(a);
  30. CREATE INDEX t1b ON t1(b);
  31. CREATE INDEX t1cd ON t1(c,d);
  32. CREATE VIRTUAL TABLE nums USING wholenumber;
  33. INSERT INTO t1 SELECT value, value, value/100, value FROM nums
  34. WHERE value BETWEEN 1 AND 256;
  35. EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;
  36. }
  37. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  38. do_test analyze7-1.1 {
  39. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
  40. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  41. do_test analyze7-1.2 {
  42. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  43. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
  44. # Run an analyze on one of the three indices. Verify that this
  45. # effects the row-count estimate on the one query that uses that
  46. # one index.
  47. #
  48. do_test analyze7-2.0 {
  49. execsql {ANALYZE t1a;}
  50. db cache flush
  51. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
  52. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  53. do_test analyze7-2.1 {
  54. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
  55. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  56. do_test analyze7-2.2 {
  57. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  58. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
  59. # Verify that since the query planner now things that t1a is more
  60. # selective than t1b, it prefers to use t1a.
  61. #
  62. do_test analyze7-2.3 {
  63. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
  64. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  65. # Run an analysis on another of the three indices. Verify that this
  66. # new analysis works and does not disrupt the previous analysis.
  67. #
  68. do_test analyze7-3.0 {
  69. execsql {ANALYZE t1cd;}
  70. db cache flush;
  71. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
  72. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  73. do_test analyze7-3.1 {
  74. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
  75. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  76. do_test analyze7-3.2.1 {
  77. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
  78. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
  79. ifcapable stat4||stat3 {
  80. # If ENABLE_STAT4 is defined, SQLite comes up with a different estimated
  81. # row count for (c=2) than it does for (c=?).
  82. do_test analyze7-3.2.2 {
  83. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  84. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
  85. } else {
  86. # If ENABLE_STAT4 is not defined, the expected row count for (c=2) is the
  87. # same as that for (c=?).
  88. do_test analyze7-3.2.3 {
  89. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  90. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
  91. }
  92. do_test analyze7-3.3 {
  93. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
  94. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  95. ifcapable {!stat4 && !stat3} {
  96. do_test analyze7-3.4 {
  97. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
  98. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  99. do_test analyze7-3.5 {
  100. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
  101. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  102. }
  103. do_test analyze7-3.6 {
  104. execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123}
  105. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?)}}
  106. finish_test