]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Experimental change to support the covering index optimization for queries with OR...
authordan <dan@noemail.net>
Fri, 24 Aug 2012 10:52:35 +0000 (10:52 +0000)
committerdan <dan@noemail.net>
Fri, 24 Aug 2012 10:52:35 +0000 (10:52 +0000)
FossilOrigin-Name: 1dc8c7c741a82bb98a07f3fdb8142d8bc8d8a98b

manifest
manifest.uuid
src/sqliteInt.h
src/where.c
test/whereD.test [new file with mode: 0644]

index 09414718bc0cf9ee7dc5bf0f0b25d8c4a7f104bb..cde938ca24d7549610830ac07aabda7e67e1ac2f 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sthe\snested\saggregate\squery\senhancements\sinto\strunk.
-D 2012-08-24T01:07:52.214
+C Experimental\schange\sto\ssupport\sthe\scovering\sindex\soptimization\sfor\squeries\swith\sOR\sterms\sin\sthe\sWHERE\sclause\sthat\ssearch\sa\ssingle\sindex\smore\sthan\sonce.
+D 2012-08-24T10:52:35.794
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in abd5c10d21d1395f140d9e50ea999df8fa4d6376
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -180,7 +180,7 @@ F src/select.c 2c0291db072924cace54aadbff1996297e9b8de0
 F src/shell.c 076e1c90d594644f36027c8ecff9a392cf2d3a06
 F src/sqlite.h.in f664797c68ced43c2ea2c541d4ec8e1e04ec68ac
 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
-F src/sqliteInt.h f4748d18114510f61e6de8bac0d513dc76e8f21c
+F src/sqliteInt.h 66b3bc97e63dcb65c71ceddd668c1c7ca30f78f7
 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
 F src/status.c 35939e7e03abf1b7577ce311f48f682c40de3208
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -250,7 +250,7 @@ F src/vtab.c bb8ea3a26608bb1357538a5d2fc72beba6638998
 F src/wal.c 9294df6f96aae5909ae1a9b733fd1e1b4736978b
 F src/wal.h 29c197540b19044e6cd73487017e5e47a1d3dac6
 F src/walker.c 3d75ba73de15e0f8cd0737643badbeb0e002f07b
-F src/where.c 24c7494d8875ead994b4dfe5461340c27fd424ca
+F src/where.c b4d98014de3ce6e0e9a63ba16018fff89dc72d73
 F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 0be144b453e0622a085fae8665c32f5676708e00
@@ -965,6 +965,7 @@ F test/where9.test ae98dc22ef9b6f2bc81e9f164e41b38faa9bda06
 F test/whereA.test 24c234263c8fe358f079d5e57d884fb569d2da0a
 F test/whereB.test 0def95db3bdec220a731c7e4bec5930327c1d8c5
 F test/whereC.test 13ff5ec0dba407c0e0c075980c75b3275a6774e5
+F test/whereD.test 54e11307e85c2ae987744f0fefd34214f752ba49
 F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
 F test/win32lock.test b2a539e85ae6b2d78475e016a9636b4451dc7fb9
 F test/zeroblob.test caaecfb4f908f7bc086ed238668049f96774d688
@@ -1012,7 +1013,10 @@ F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9
-P 20f184f2d5908badd9d44d4fe2ad7c9e182c8803 00b1dc71be4c3420730b5f7840af824ea86165e7
-R c1fab8e17aa8935c76d7626b4d7fe139
-U drh
-Z 3e9a3890be18a0a197379668b716e1a3
+P d4cd6017c9875947a05b1dc36538d4272fb18739
+R 0e14b06eb4293757e84906b2ad3f48bd
+T *branch * multi-or-covering-index
+T *sym-multi-or-covering-index *
+T -sym-trunk *
+U dan
+Z a3dcd5ab6d90b9bc7bd57494f6da388a
index 78d216df97d750a6266e9cb207f59b368acebba1..af0b97af79cbfa491a974bea872a19ed315e2d20 100644 (file)
@@ -1 +1 @@
-d4cd6017c9875947a05b1dc36538d4272fb18739
\ No newline at end of file
+1dc8c7c741a82bb98a07f3fdb8142d8bc8d8a98b
\ No newline at end of file
index 443eaa403560597e05682ced70656c3ebf05f551..5cb10ca920327d5a5da3498731942879219fb61d 100644 (file)
@@ -1948,6 +1948,7 @@ struct WhereLevel {
       } *aInLoop;           /* Information about each nested IN operator */
     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
   } u;
+  Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR levels */
 
   /* The following field is really not part of the current level.  But
   ** we need a place to cache virtual table index information for each
index d324228c9fa374fde8959d7c3150158f095c3ef5..cbe94b561dbf9e678e47af59d1bfe4dd261abd97 100644 (file)
@@ -4297,6 +4297,8 @@ static Bitmask codeOneLoopStart(
     */
     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
+    Index *pCov = 0;       /* Potential covering index (or NULL) */
+    int iCovCur = 0;       /* Cursor used to open pCov */
 
     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
     int regRowset = 0;                        /* Register for RowSet object */
@@ -4315,7 +4317,7 @@ static Bitmask codeOneLoopStart(
     pLevel->op = OP_Return;
     pLevel->p1 = regReturn;
 
-    /* Set up a new SrcList ni pOrTab containing the table being scanned
+    /* Set up a new SrcList in pOrTab containing the table being scanned
     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
     */
@@ -4393,7 +4395,9 @@ static Bitmask codeOneLoopStart(
         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
+        assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
         if( pSubWInfo ){
+          WhereLevel *pLvl;
           explainOneScan(
               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
           );
@@ -4414,11 +4418,45 @@ static Bitmask codeOneLoopStart(
           */
           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
 
+          /* If all of the OR-connected terms are optimized using the same
+          ** index, and the index is opened using the same cursor number
+          ** by each call to sqlite3WhereBegin() made by this loop, it may
+          ** be possible to use that index as a covering index.
+          **
+          ** If the call to sqlite3WhereBegin() above resulted in a scan that
+          ** uses an index, and this is either the first OR-connected term
+          ** processed or the index is the same as that used by all previous
+          ** terms, set pCov to the candidate covering index and iCovCur to
+          ** the cursor number used to open it. Otherwise, set pCov to NULL
+          ** to indicate that no candidate covering index will be available.
+          */
+          pLvl = &pSubWInfo->a[0];
+          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
+           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
+           && (pLvl->iIdxCur==pParse->nTab-1)
+           && (ii==0 || (pLvl->plan.u.pIdx==pCov && pLvl->iIdxCur==iCovCur))
+          ){
+            pCov = pLvl->plan.u.pIdx;
+            iCovCur = pLvl->iIdxCur;
+            /* Decrement pParse->nTab in order to make cursor number iCovCur
+            ** available to the next OR-connected term.  */
+            pParse->nTab--;
+          }else{
+            pCov = 0;
+          }
+
           /* Finish the loop through table entries that match term pOrTerm. */
           sqlite3WhereEnd(pSubWInfo);
         }
       }
     }
+    /* Increment pParse->nTab here to ensure that it really is larger than
+    ** the largest cursor number used by the VM (this may not be the case
+    ** if pParse->nTab was decremented by the final iteration of the loop
+    ** above).  */
+    pParse->nTab++;
+    pLevel->pCovidx = pCov;
+    pLevel->iIdxCur = iCovCur;
     if( pAndExpr ){
       pAndExpr->pLeft = 0;
       sqlite3ExprDelete(pParse->db, pAndExpr);
@@ -5208,6 +5246,7 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){
   */
   assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
+    Index *pIdx = 0;
     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
     Table *pTab = pTabItem->pTab;
     assert( pTab!=0 );
@@ -5237,12 +5276,15 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){
     ** that reference the table and converts them into opcodes that
     ** reference the index.
     */
-    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
+    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
+      pIdx = pLevel->plan.u.pIdx;
+    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
+      pIdx = pLevel->pCovidx;
+    }
+    if( pIdx && !db->mallocFailed){
       int k, j, last;
       VdbeOp *pOp;
-      Index *pIdx = pLevel->plan.u.pIdx;
 
-      assert( pIdx!=0 );
       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
       last = sqlite3VdbeCurrentAddr(v);
       for(k=pWInfo->iTop; k<last; k++, pOp++){
diff --git a/test/whereD.test b/test/whereD.test
new file mode 100644 (file)
index 0000000..10ccbb7
--- /dev/null
@@ -0,0 +1,36 @@
+# 2012 August 24
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements regression tests for SQLite library.  The
+# focus of this file is testing that an index may be used as a covering
+# index when there are OR expressions in the WHERE clause. 
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix whereD
+
+do_execsql_test 1.1 {
+  CREATE TABLE t(i,j,k);
+  CREATE INDEX i ON t(i,j,k);
+
+  INSERT INTO t VALUES(3, 3, 'three');
+  INSERT INTO t VALUES(2, 2, 'two');
+  INSERT INTO t VALUES(1, 1, 'one');
+  INSERT INTO t VALUES(4, 4, 'four');
+}
+
+do_execsql_test 1.2 {
+  SELECT k FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2);
+} {one two}
+
+finish_test
+