analyze4.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # 2011 January 04
  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 regression tests for SQLite library. This file
  13. # implements tests for ANALYZE to verify that multiple rows containing
  14. # a NULL value count as distinct rows for the purposes of analyze
  15. # statistics.
  16. #
  17. # Also include test cases for collating sequences on indices.
  18. #
  19. set testdir [file dirname $argv0]
  20. source $testdir/tester.tcl
  21. do_test analyze4-1.0 {
  22. db eval {
  23. CREATE TABLE t1(a,b);
  24. CREATE INDEX t1a ON t1(a);
  25. CREATE INDEX t1b ON t1(b);
  26. INSERT INTO t1 VALUES(1,NULL);
  27. INSERT INTO t1 SELECT a+1, b FROM t1;
  28. INSERT INTO t1 SELECT a+2, b FROM t1;
  29. INSERT INTO t1 SELECT a+4, b FROM t1;
  30. INSERT INTO t1 SELECT a+8, b FROM t1;
  31. INSERT INTO t1 SELECT a+16, b FROM t1;
  32. INSERT INTO t1 SELECT a+32, b FROM t1;
  33. INSERT INTO t1 SELECT a+64, b FROM t1;
  34. ANALYZE;
  35. }
  36. # Should choose the t1a index since it is more specific than t1b.
  37. db eval {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=5 AND b IS NULL}
  38. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
  39. # Verify that the t1b index shows that it does not narrow down the
  40. # search any at all.
  41. #
  42. do_test analyze4-1.1 {
  43. db eval {
  44. SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
  45. }
  46. } {t1a {128 1} t1b {128 128}}
  47. # Change half of the b values from NULL to a constant. Verify
  48. # that the number of rows selected in stat1 is half the total
  49. # number of rows.
  50. #
  51. do_test analyze4-1.2 {
  52. db eval {
  53. UPDATE t1 SET b='x' WHERE a%2;
  54. ANALYZE;
  55. SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
  56. }
  57. } {t1a {128 1} t1b {128 64}}
  58. # Change the t1.b values all back to NULL. Add columns t1.c and t1.d.
  59. # Create a multi-column indices using t1.b and verify that ANALYZE
  60. # processes them correctly.
  61. #
  62. do_test analyze4-1.3 {
  63. db eval {
  64. UPDATE t1 SET b=NULL;
  65. ALTER TABLE t1 ADD COLUMN c;
  66. ALTER TABLE t1 ADD COLUMN d;
  67. UPDATE t1 SET c=a/4, d=a/2;
  68. CREATE INDEX t1bcd ON t1(b,c,d);
  69. CREATE INDEX t1cdb ON t1(c,d,b);
  70. CREATE INDEX t1cbd ON t1(c,b,d);
  71. ANALYZE;
  72. SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;
  73. }
  74. } {t1a {128 1} t1b {128 128} t1bcd {128 128 4 2} t1cbd {128 4 4 2} t1cdb {128 4 2 2}}
  75. # Verify that collating sequences are taken into account when computing
  76. # ANALYZE statistics.
  77. #
  78. do_test analyze4-2.0 {
  79. db eval {
  80. CREATE TABLE t2(
  81. x INTEGER PRIMARY KEY,
  82. a TEXT COLLATE nocase,
  83. b TEXT COLLATE rtrim,
  84. c TEXT COLLATE binary
  85. );
  86. CREATE INDEX t2a ON t2(a);
  87. CREATE INDEX t2b ON t2(b);
  88. CREATE INDEX t2c ON t2(c);
  89. CREATE INDEX t2c2 ON t2(c COLLATE nocase);
  90. CREATE INDEX t2c3 ON t2(c COLLATE rtrim);
  91. INSERT INTO t2 VALUES(1, 'abc', 'abc', 'abc');
  92. INSERT INTO t2 VALUES(2, 'abC', 'abC', 'abC');
  93. INSERT INTO t2 VALUES(3, 'abc ', 'abc ', 'abc ');
  94. INSERT INTO t2 VALUES(4, 'abC ', 'abC ', 'abC ');
  95. INSERT INTO t2 VALUES(5, 'aBc', 'aBc', 'aBc');
  96. INSERT INTO t2 VALUES(6, 'aBC', 'aBC', 'aBC');
  97. INSERT INTO t2 VALUES(7, 'aBc ', 'aBc ', 'aBc ');
  98. INSERT INTO t2 VALUES(8, 'aBC ', 'aBC ', 'aBC ');
  99. ANALYZE;
  100. SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t2' ORDER BY idx;
  101. }
  102. } {t2a {8 4} t2b {8 2} t2c {8 1} t2c2 {8 4} t2c3 {8 2}}
  103. finish_test