tkt3757.test 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # 2009 March 28
  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. # Ticket #3757: The cost functions on the query optimizer for the
  13. # IN operator can be improved.
  14. #
  15. # $Id: tkt3757.test,v 1.1 2009/03/29 00:13:04 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Evaluate SQL. Return the result set followed by the
  19. # and the number of full-scan steps.
  20. #
  21. proc count_steps {sql} {
  22. set r [db eval $sql]
  23. lappend r scan [db status step] sort [db status sort]
  24. }
  25. # Construct tables
  26. #
  27. do_test tkt3757-1.1 {
  28. db eval {
  29. CREATE TABLE t1(x INTEGER, y INTEGER, z TEXT);
  30. CREATE INDEX t1i1 ON t1(y,z);
  31. INSERT INTO t1 VALUES(1,2,'three');
  32. CREATE TABLE t2(a INTEGER, b TEXT);
  33. INSERT INTO t2 VALUES(2, 'two');
  34. ANALYZE;
  35. SELECT * FROM sqlite_stat1 ORDER BY 1, 2;
  36. }
  37. } {t1 t1i1 {1 1 1} t2 {} 1}
  38. # Modify statistics in order to make the optimizer then that:
  39. #
  40. # (1) Table T1 has about 250K entries
  41. # (2) There are only about 5 distinct values of T1.
  42. #
  43. # Then run a query with "t1.y IN (SELECT ..)" in the WHERE clause.
  44. # Make sure the index is used.
  45. #
  46. do_test tkt3757-1.2 {
  47. db eval {
  48. DELETE FROM sqlite_stat1;
  49. INSERT INTO sqlite_stat1 VALUES('t1','t1i1','250000 50000 30');
  50. }
  51. count_steps {
  52. SELECT * FROM t1 WHERE y IN (SELECT a FROM t2)
  53. }
  54. } {1 2 three scan 0 sort 0}
  55. finish_test