Index: ext/rtree/rtreeC.test ================================================================== --- ext/rtree/rtreeC.test +++ ext/rtree/rtreeC.test @@ -175,11 +175,12 @@ #-------------------------------------------------------------------- # Test that the sqlite_stat1 data is used correctly. # reset_db do_execsql_test 5.1 { - CREATE TABLE t1(x PRIMARY KEY, y); + CREATE TABLE t1(x, y); + CREATE INDEX t1x ON t1(x); CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2, +d1); INSERT INTO t1(x) VALUES(1); INSERT INTO t1(x) SELECT x+1 FROM t1; -- 2 INSERT INTO t1(x) SELECT x+2 FROM t1; -- 4 @@ -219,11 +220,11 @@ do_eqp_test 5.4 { SELECT * FROM t1, rt WHERE x==id; } { QUERY PLAN |--SCAN TABLE rt VIRTUAL TABLE INDEX 2: - `--SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (x=?) + `--SEARCH TABLE t1 USING INDEX t1x (x=?) } # Delete the ANALYZE data. "t1" should be the outer loop again. # do_execsql_test 5.5 { DROP TABLE sqlite_stat1; } Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -2474,10 +2474,23 @@ continue; } if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){ pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE; + if( pProbe->nKeyCol==1 + && pTerm->truthProb>=0 + && (pTerm->eOperator & (WO_EQ|WO_IS|WO_IN))!=0 + ){ + /* If the LHS of an == or IS or IN operator is unique, then + ** make the guess that the truth probability of the expression is 50%. + ** This is probably an overestimate, but we want to be safe. Without + ** this guess, the truth probability would be 93.75%, which is usually + ** a little too high for such a constraint, resulting in an output row + ** count that is too large, and throwing off the calcualations on the + ** cost of an external sort. */ + pTerm->truthProb = -10; + } }else{ pBuilder->bldFlags |= SQLITE_BLDF_INDEXED; } pNew->wsFlags = saved_wsFlags; pNew->u.btree.nEq = saved_nEq; Index: test/whereF.test ================================================================== --- test/whereF.test +++ test/whereF.test @@ -50,12 +50,12 @@ do_execsql_test 1.0 { PRAGMA automatic_index = 0; CREATE TABLE t1(a, b, c); CREATE TABLE t2(d, e, f); - CREATE UNIQUE INDEX i1 ON t1(a); - CREATE UNIQUE INDEX i2 ON t2(d); + CREATE INDEX i1 ON t1(a); + CREATE INDEX i2 ON t2(d); } {} foreach {tn sql} { 1 "SELECT * FROM t1, t2 WHERE t1.a=t2.e AND t2.d? AND t2.d>t1.c AND t1.b=t2.e" 2 "SELECT * FROM t2, t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e" Index: test/whereG.test ================================================================== --- test/whereG.test +++ test/whereG.test @@ -149,12 +149,12 @@ } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}} # Commuting a term of the WHERE clause should not change the query plan # do_execsql_test whereG-3.0 { - CREATE TABLE a(a1 PRIMARY KEY, a2); - CREATE TABLE b(b1 PRIMARY KEY, b2); + CREATE TABLE a(a1, a2); CREATE INDEX a_1 ON a(a1); + CREATE TABLE b(b1, b2); CREATE INDEX b_1 ON b(b1); } {} do_eqp_test whereG-3.1 { SELECT * FROM a, b WHERE b1=a1 AND a2=5; } {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/} do_eqp_test whereG-3.2 { Index: test/whereL.test ================================================================== --- test/whereL.test +++ test/whereL.test @@ -45,12 +45,12 @@ WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=5 ORDER BY t1.a; } { QUERY PLAN |--SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (a=?) - |--SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (a=?) - `--SCAN TABLE t3 + |--SEARCH TABLE t2 USING AUTOMATIC PARTIAL COVERING INDEX (a=?) + `--SEARCH TABLE t3 USING AUTOMATIC PARTIAL COVERING INDEX (j=?) } # Constant propagation in the face of collating sequences: # do_execsql_test 200 { Index: test/with3.test ================================================================== --- test/with3.test +++ test/with3.test @@ -124,10 +124,10 @@ | | `--SCAN TABLE w2 | `--RECURSIVE STEP | |--SCAN TABLE w1 | `--SCAN TABLE c |--SCAN SUBQUERY xxxxxx - |--SEARCH TABLE w2 USING INTEGER PRIMARY KEY (rowid=?) - `--SEARCH TABLE w1 USING INTEGER PRIMARY KEY (rowid=?) + |--SEARCH TABLE w1 USING INTEGER PRIMARY KEY (rowid=?) + `--SEARCH TABLE w2 USING INTEGER PRIMARY KEY (rowid=?) } finish_test