From: drh Date: Mon, 8 Oct 2012 19:41:38 +0000 (+0000) Subject: Bug fixes in the ORDER BY optimizer. X-Git-Tag: version-3.7.15~84^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0a4c741cab66da8d599e68e660718adeb50f7d24;p=thirdparty%2Fsqlite.git Bug fixes in the ORDER BY optimizer. FossilOrigin-Name: 301bbee4045aa169e29fb4fb75743b71eb4760a1 --- diff --git a/manifest b/manifest index ff4e46d0cb..a505a09dff 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Continued\srefactoring\sof\sthe\sORDER\sBY\soptimization\slogic.\s\sThis\scheck-in\nis\sclose\sto\sworking,\sbut\sit\sstill\shas\sissues.\s\sA\sfew\stest\scases\sfail. -D 2012-10-08T18:23:51.612 +C Bug\sfixes\sin\sthe\sORDER\sBY\soptimizer. +D 2012-10-08T19:41:38.447 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 5f4f26109f9d80829122e0e09f9cda008fa065fb F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -249,7 +249,7 @@ F src/vtab.c d8020c0a0e8ccc490ca449d7e665311b6e9f3ba9 F src/wal.c e1fe8f92a0ea0fef8faa87ec43a127a478589d22 F src/wal.h 29c197540b19044e6cd73487017e5e47a1d3dac6 F src/walker.c 3d75ba73de15e0f8cd0737643badbeb0e002f07b -F src/where.c 968bea256016bdc32de4051fc2e921771ed945e6 +F src/where.c d4a39bc7ea96ac2580b662df73c16d5ddacd9723 F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/aggnested.test 0be144b453e0622a085fae8665c32f5676708e00 @@ -1018,7 +1018,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9 -P 8f4487450be1a2b0371f8251a967cbe341b2dea1 -R b898c44fee2dd1d4ef226b8a2707fa76 +P adbdc663f3d22ff03f21040a811d585cf2218626 +R 3f2fb8221173011002c406e4b1569bb1 U drh -Z cd5853ba0dbcfea98f0be88905f219b5 +Z 5a939ce30180c1cb84b0892ff09f4ab1 diff --git a/manifest.uuid b/manifest.uuid index 9a42175bcf..1996a19ef2 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -adbdc663f3d22ff03f21040a811d585cf2218626 \ No newline at end of file +301bbee4045aa169e29fb4fb75743b71eb4760a1 \ No newline at end of file diff --git a/src/where.c b/src/where.c index c309b456f0..2701bbda08 100644 --- a/src/where.c +++ b/src/where.c @@ -2803,17 +2803,30 @@ static int isSortingIndex( sqlite3 *db = pParse->db; /* Database connection */ int nPriorSat; /* ORDER BY terms satisfied by outer loops */ int seenRowid = 0; /* True if an ORDER BY rowid term is seen */ - int uniqueNotNull = 1; /* pIdx is UNIQUE with all terms are NOT NULL */ + int uniqueNotNull; /* pIdx is UNIQUE with all terms are NOT NULL */ if( p->i==0 ){ nPriorSat = 0; }else{ nPriorSat = p->aLevel[p->i-1].plan.nOBSat; - if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return nPriorSat; + if( (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){ + /* This loop cannot be ordered unless the next outer loop is + ** also ordered */ + return nPriorSat; + } + if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){ + /* Only look at the outer-most loop if the OrderByIdxJoin + ** optimization is disabled */ + return nPriorSat; + } } pOrderBy = p->pOrderBy; assert( pOrderBy!=0 ); - if( pIdx->bUnordered ) return nPriorSat; + if( pIdx->bUnordered ){ + /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot + ** be used for sorting */ + return nPriorSat; + } nTerm = pOrderBy->nExpr; uniqueNotNull = pIdx->onError!=OE_None; assert( nTerm>0 ); @@ -2936,6 +2949,12 @@ static int isSortingIndex( uniqueNotNull = 0; } } + + /* If we have not found at least one ORDER BY term that matches the + ** index, then show no progress. */ + if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat; + + /* Return the necessary scan order back to the caller */ *pbRev = sortOrder & 1; /* If there was an "ORDER BY rowid" term that matched, or it is only