analyze8.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # 2011 August 13
  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 is testing the capabilities of sqlite_stat3.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !stat4&&!stat3 {
  18. finish_test
  19. return
  20. }
  21. set testprefix analyze8
  22. proc eqp {sql {db db}} {
  23. uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
  24. }
  25. # Scenario:
  26. #
  27. # Two indices. One has mostly singleton entries, but for a few
  28. # values there are hundreds of entries. The other has 10-20
  29. # entries per value.
  30. #
  31. # Verify that the query planner chooses the first index for the singleton
  32. # entries and the second index for the others.
  33. #
  34. do_test 1.0 {
  35. db eval {
  36. CREATE TABLE t1(a,b,c,d);
  37. CREATE INDEX t1a ON t1(a);
  38. CREATE INDEX t1b ON t1(b);
  39. CREATE INDEX t1c ON t1(c);
  40. }
  41. for {set i 0} {$i<1000} {incr i} {
  42. if {$i%2==0} {set a $i} {set a [expr {($i%8)*100}]}
  43. set b [expr {$i/10}]
  44. set c [expr {$i/8}]
  45. set c [expr {$c*$c*$c}]
  46. db eval {INSERT INTO t1 VALUES($a,$b,$c,$i)}
  47. }
  48. db eval {ANALYZE}
  49. } {}
  50. # The a==100 comparison is expensive because there are many rows
  51. # with a==100. And so for those cases, choose the t1b index.
  52. #
  53. # Buf ro a==99 and a==101, there are far fewer rows so choose
  54. # the t1a index.
  55. #
  56. do_test 1.1 {
  57. eqp {SELECT * FROM t1 WHERE a=100 AND b=55}
  58. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  59. do_test 1.2 {
  60. eqp {SELECT * FROM t1 WHERE a=99 AND b=55}
  61. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  62. do_test 1.3 {
  63. eqp {SELECT * FROM t1 WHERE a=101 AND b=55}
  64. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  65. do_test 1.4 {
  66. eqp {SELECT * FROM t1 WHERE a=100 AND b=56}
  67. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  68. do_test 1.5 {
  69. eqp {SELECT * FROM t1 WHERE a=99 AND b=56}
  70. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  71. do_test 1.6 {
  72. eqp {SELECT * FROM t1 WHERE a=101 AND b=56}
  73. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  74. do_test 2.1 {
  75. eqp {SELECT * FROM t1 WHERE a=100 AND b BETWEEN 50 AND 54}
  76. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}}
  77. # There are many more values of c between 0 and 100000 than there are
  78. # between 800000 and 900000. So t1c is more selective for the latter
  79. # range.
  80. #
  81. # Test 3.2 is a little unstable. It depends on the planner estimating
  82. # that (b BETWEEN 50 AND 54) will match more rows than (c BETWEEN
  83. # 800000 AND 900000). Which is a pretty close call (50 vs. 32), so
  84. # the planner could get it wrong with an unlucky set of samples. This
  85. # case happens to work, but others ("b BETWEEN 40 AND 44" for example)
  86. # will fail.
  87. #
  88. do_execsql_test 3.0 {
  89. SELECT count(*) FROM t1 WHERE b BETWEEN 50 AND 54;
  90. SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 100000;
  91. SELECT count(*) FROM t1 WHERE c BETWEEN 800000 AND 900000;
  92. } {50 376 32}
  93. do_test 3.1 {
  94. eqp {SELECT * FROM t1 WHERE b BETWEEN 50 AND 54 AND c BETWEEN 0 AND 100000}
  95. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}}
  96. do_test 3.2 {
  97. eqp {SELECT * FROM t1
  98. WHERE b BETWEEN 50 AND 54 AND c BETWEEN 800000 AND 900000}
  99. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}}
  100. do_test 3.3 {
  101. eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 0 AND 100000}
  102. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  103. do_test 3.4 {
  104. eqp {SELECT * FROM t1
  105. WHERE a=100 AND c BETWEEN 800000 AND 900000}
  106. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}}
  107. finish_test