coveridxscan.test 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # 2012 September 17
  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. # Tests for the optimization which attempts to use a covering index
  13. # for a full-table scan (under the theory that the index will be smaller
  14. # and require less I/O and hence will run faster.)
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. set testprefix coveridxscan
  19. do_test 1.1 {
  20. db eval {
  21. CREATE TABLE t1(a,b,c);
  22. INSERT INTO t1 VALUES(5,4,3), (4,8,2), (3,2,1);
  23. CREATE INDEX t1ab ON t1(a,b);
  24. CREATE INDEX t1b ON t1(b);
  25. SELECT a FROM t1;
  26. }
  27. # covering index used for the scan, hence values are increasing
  28. } {3 4 5}
  29. do_test 1.2 {
  30. db eval {
  31. SELECT a, c FROM t1;
  32. }
  33. # There is no covering index, hence the values are in rowid order
  34. } {5 3 4 2 3 1}
  35. do_test 1.3 {
  36. db eval {
  37. SELECT b FROM t1;
  38. }
  39. # Choice of two indices: use the one with fewest columns
  40. } {2 4 8}
  41. do_test 2.1 {
  42. optimization_control db cover-idx-scan 0
  43. db eval {SELECT a FROM t1}
  44. # With the optimization turned off, output in rowid order
  45. } {5 4 3}
  46. do_test 2.2 {
  47. db eval {SELECT a, c FROM t1}
  48. } {5 3 4 2 3 1}
  49. do_test 2.3 {
  50. db eval {SELECT b FROM t1}
  51. } {4 8 2}
  52. db close
  53. sqlite3_shutdown
  54. sqlite3_config_cis 0
  55. sqlite3 db test.db
  56. do_test 3.1 {
  57. db eval {SELECT a FROM t1}
  58. # With the optimization configured off, output in rowid order
  59. } {5 4 3}
  60. do_test 3.2 {
  61. db eval {SELECT a, c FROM t1}
  62. } {5 3 4 2 3 1}
  63. do_test 3.3 {
  64. db eval {SELECT b FROM t1}
  65. } {4 8 2}
  66. db close
  67. sqlite3_shutdown
  68. sqlite3_config_cis 1
  69. sqlite3 db test.db
  70. # The CIS optimization is enabled again. Covering indices are once again
  71. # used for all table scans.
  72. do_test 4.1 {
  73. db eval {SELECT a FROM t1}
  74. } {3 4 5}
  75. do_test 4.2 {
  76. db eval {SELECT a, c FROM t1}
  77. } {5 3 4 2 3 1}
  78. do_test 4.3 {
  79. db eval {SELECT b FROM t1}
  80. } {2 4 8}
  81. finish_test