xref: /sqlite-3.40.0/test/analyzeD.test (revision 8210233c)
14fb48e4eSdrh# 2014-10-04
24ee3eb0aSdan#
34ee3eb0aSdan# The author disclaims copyright to this source code.  In place of
44ee3eb0aSdan# a legal notice, here is a blessing:
54ee3eb0aSdan#
64ee3eb0aSdan#    May you do good and not evil.
74ee3eb0aSdan#    May you find forgiveness for yourself and forgive others.
84ee3eb0aSdan#    May you share freely, never taking more than you give.
94ee3eb0aSdan#
104ee3eb0aSdan#***********************************************************************
114ee3eb0aSdan# This file implements regression tests for SQLite library.
124ee3eb0aSdan# This file implements tests for the ANALYZE command.
134ee3eb0aSdan#
144ee3eb0aSdan
154ee3eb0aSdanset testdir [file dirname $argv0]
164ee3eb0aSdansource $testdir/tester.tcl
174ee3eb0aSdanset ::testprefix analyzeD
184ee3eb0aSdan
194ee3eb0aSdanifcapable {!stat4} {
204ee3eb0aSdan  finish_test
214ee3eb0aSdan  return
224ee3eb0aSdan}
234ee3eb0aSdan
244ee3eb0aSdan
254ee3eb0aSdan# Set up a table with the following properties:
264ee3eb0aSdan#
274ee3eb0aSdan#    * Contains 1000 rows.
284ee3eb0aSdan#    * Column a contains even integers between 0 and 18, inclusive (so that
294ee3eb0aSdan#      a=? for any such integer matches 100 rows).
304ee3eb0aSdan#    * Column b contains integers between 0 and 9, inclusive.
314ee3eb0aSdan#    * Column c contains integers between 0 and 199, inclusive (so that
324ee3eb0aSdan#      for any such integer, c=? matches 5 rows).
334ee3eb0aSdan#    * Then add 7 rows with a new value for "a" - 3001. The stat4 table will
344ee3eb0aSdan#      not contain any samples with a=3001.
354ee3eb0aSdan#
364ee3eb0aSdando_execsql_test 1.0 {
374ee3eb0aSdan  CREATE TABLE t1(a, b, c);
384ee3eb0aSdan}
394ee3eb0aSdando_test 1.1 {
404ee3eb0aSdan  for {set i 1} {$i < 1000} {incr i} {
414ee3eb0aSdan    set c [expr $i % 200]
424ee3eb0aSdan    execsql { INSERT INTO t1(a, b, c) VALUES( 2*($i/100), $i%10, $c ) }
434ee3eb0aSdan  }
444ee3eb0aSdan
454ee3eb0aSdan  execsql {
464ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3001);
474ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3002);
484ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3003);
494ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3004);
504ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3005);
514ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3006);
524ee3eb0aSdan    INSERT INTO t1 VALUES(3001, 3001, 3007);
534ee3eb0aSdan
544ee3eb0aSdan    CREATE INDEX t1_ab ON t1(a, b);
554ee3eb0aSdan    CREATE INDEX t1_c ON t1(c);
564ee3eb0aSdan
574ee3eb0aSdan    ANALYZE;
584ee3eb0aSdan  }
594ee3eb0aSdan} {}
604ee3eb0aSdan
614ee3eb0aSdan# With full ANALYZE data, SQLite sees that c=150 (5 rows) is better than
624ee3eb0aSdan# a=3001 (7 rows).
634ee3eb0aSdan#
644ee3eb0aSdando_eqp_test 1.2 {
654ee3eb0aSdan  SELECT * FROM t1 WHERE a=3001 AND c=150;
66*8210233cSdrh} {SEARCH t1 USING INDEX t1_c (c=?)}
674ee3eb0aSdan
684ee3eb0aSdando_test 1.3 {
694ee3eb0aSdan  execsql { DELETE FROM sqlite_stat1 }
704ee3eb0aSdan  db close
714ee3eb0aSdan  sqlite3 db test.db
724ee3eb0aSdan} {}
734ee3eb0aSdan
744ee3eb0aSdan# Without stat1, because 3001 is larger than all samples in the stat4
754fb48e4eSdrh# table, SQLite thinks that a=3001 matches just 1 row. So it (incorrectly)
764ee3eb0aSdan# chooses it over the c=150 index (5 rows). Even with stat1 data, things
774ee3eb0aSdan# worked this way before commit [e6f7f97dbc].
784ee3eb0aSdan#
794ee3eb0aSdando_eqp_test 1.4 {
804ee3eb0aSdan  SELECT * FROM t1 WHERE a=3001 AND c=150;
81*8210233cSdrh} {SEARCH t1 USING INDEX t1_ab (a=?)}
824ee3eb0aSdan
834ee3eb0aSdando_test 1.5 {
844ee3eb0aSdan  execsql {
854ee3eb0aSdan    UPDATE t1 SET a=13 WHERE a = 3001;
864ee3eb0aSdan    ANALYZE;
874ee3eb0aSdan  }
884ee3eb0aSdan} {}
894ee3eb0aSdan
904ee3eb0aSdando_eqp_test 1.6 {
914ee3eb0aSdan  SELECT * FROM t1 WHERE a=13 AND c=150;
92*8210233cSdrh} {SEARCH t1 USING INDEX t1_c (c=?)}
934ee3eb0aSdan
944ee3eb0aSdando_test 1.7 {
954ee3eb0aSdan  execsql { DELETE FROM sqlite_stat1 }
964ee3eb0aSdan  db close
974ee3eb0aSdan  sqlite3 db test.db
984ee3eb0aSdan} {}
994ee3eb0aSdan
1004ee3eb0aSdan# Same test as 1.4, except this time the 7 rows that match the a=? condition
1014ee3eb0aSdan# do not feature larger values than all rows in the stat4 table. So SQLite
1024ee3eb0aSdan# gets this right, even without stat1 data.
1034ee3eb0aSdando_eqp_test 1.8 {
1044ee3eb0aSdan  SELECT * FROM t1 WHERE a=13 AND c=150;
105*8210233cSdrh} {SEARCH t1 USING INDEX t1_c (c=?)}
1064ee3eb0aSdan
1074ee3eb0aSdanfinish_test
108