]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Try to run all Bloom filters before any Seeks. This gives a small performance
authordrh <>
Sun, 5 Dec 2021 00:45:55 +0000 (00:45 +0000)
committerdrh <>
Sun, 5 Dec 2021 00:45:55 +0000 (00:45 +0000)
gain on the Star-Schema Benchmark.

FossilOrigin-Name: 5be2470f8755ef454f813c880e659bdbf82f2396be9320cf3079cd4ca8e81a19

manifest
manifest.uuid
src/where.c
src/whereInt.h
src/wherecode.c

index e1808eea5b714b97e495d74f621760dd2effacfe..1a9a4b84ffe572c2642e4222e82a0871110bd3e8 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Miscellaneous\scleanup\sof\sthe\snew\sBloom-filter\scode.
-D 2021-12-04T21:11:18.037
+C Try\sto\srun\sall\sBloom\sfilters\sbefore\sany\sSeeks.\s\sThis\sgives\sa\ssmall\sperformance\ngain\son\sthe\sStar-Schema\sBenchmark.
+D 2021-12-05T00:45:55.528
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -637,9 +637,9 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c ed0398a7adf02c31e34aada42cc86c58f413a7afe5f741a5d373ad087abde028
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
-F src/where.c 957543456a7d1683d21ed8c890bcdaa6414716082dfb076bb43decdc0f87909e
-F src/whereInt.h 2c9d149b1b41c59f977deb1882e95632800946fcf15c83eaffb227afece04e51
-F src/wherecode.c 2253f91bcded8932a4211238fa7dcf6ab40ff68dbd4c2d88eb4411f12b19552c
+F src/where.c 3d29b27e345a28f76ecb20b0c789a224e9dc74534363c3ddbc44c283eb4cb708
+F src/whereInt.h 3c634e184de128d99c2b017f899f2309c77330de54193ff970597e9acdeaff3b
+F src/wherecode.c 48b456c910d552e1edf098071546c523837356ba0e3703d7b8fd9fc819da1c9b
 F src/whereexpr.c 19394cb463003e9cc9305730b1508b8817a22bb7247170d81234b691a7f05b89
 F src/window.c 5d3b397b0c026d0ff5890244ac41359e524c01ae31e78782e1ff418c3e271a9e
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
@@ -1933,7 +1933,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 629ee2e3e3125bfd2af435c6713d49e46691213ad15db0a5e93a63a77f1130c2
-R 5366235cd370e2a6adf5aabc2d25d6ed
+P 201b6dd875b0ae2bbc9969b098e88abfc09e37b59e857decd41f2dcbeeb13e01
+R 8ad2d75d69a2ffc06bdf1b52eb7537e1
 U drh
-Z ab3ac181064c99ca5c8ac656f604bf60
+Z 42400fd1219d022ab29b577e6d964b89
index 5fe0ded6ae3971c19d3f8ea8aaed9faebef9a6df..cca08a44ebd99bcbe573f99f5a333a302fb8787c 100644 (file)
@@ -1 +1 @@
-201b6dd875b0ae2bbc9969b098e88abfc09e37b59e857decd41f2dcbeeb13e01
\ No newline at end of file
+5be2470f8755ef454f813c880e659bdbf82f2396be9320cf3079cd4ca8e81a19
\ No newline at end of file
index c1a538dcc9300a3fbe953357b565218eec0c89f2..2f7eb060971175ad9a5edcb385d52d075bf28fe4 100644 (file)
@@ -968,7 +968,7 @@ end_auto_index_create:
 /*
 ** Create a Bloom filter for the WhereLevel in the parameter.
 */
-static SQLITE_NOINLINE void constructBloomFilter(
+SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
   const WhereInfo *pWInfo,    /* The WHERE clause */
   WhereLevel *pLevel          /* Make a Bloom filter for this FROM term */
 ){
@@ -1003,16 +1003,18 @@ static SQLITE_NOINLINE void constructBloomFilter(
     int r1 = sqlite3GetTempReg(pParse);
     sqlite3VdbeAddOp2(v, OP_Rowid, iCur, r1);
     sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
+    sqlite3ReleaseTempReg(pParse, r1);
   }else{
     Index *pIdx = pLoop->u.btree.pIndex;
-    int r1 = sqlite3GetTempRange(pParse, pIdx->nKeyCol);
     int n = pLoop->u.btree.nEq;
+    int r1 = sqlite3GetTempRange(pParse, n);
     int jj;
     for(jj=0; jj<n; jj++){
       int iCol = pIdx->aiColumn[jj];
       sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
     }
     sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
+    sqlite3ReleaseTempRange(pParse, r1, n);
   }
   sqlite3VdbeResolveLabel(v, addrCont);
   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+3);
@@ -5533,7 +5535,7 @@ WhereInfo *sqlite3WhereBegin(
                   &pTabList->a[pLevel->iFrom], notReady, pLevel);
 #endif
       }else{
-        constructBloomFilter(pWInfo, pLevel);
+        sqlite3ConstructBloomFilter(pWInfo, pLevel);
       }
       if( db->mallocFailed ) goto whereBeginError;
     }
index fbe84787f9bb46e9d933dd78d10a060e9e9774ab..19261ca397d9afd9796e2a869bae145036adb508 100644 (file)
@@ -484,6 +484,7 @@ struct WhereInfo {
 ** where.c:
 */
 Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
+void sqlite3ConstructBloomFilter(const WhereInfo*, WhereLevel*);
 #ifdef WHERETRACE_ENABLED
 void sqlite3WhereClausePrint(WhereClause *pWC);
 void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
index e02a736428e7d4e12a537020e5721ab8e0be56fe..adc7d5b8e11feade76ded74ea026e036d3e7342f 100644 (file)
@@ -1304,6 +1304,60 @@ static void whereApplyPartialIndexConstraints(
   }
 }
 
+#if 1
+/*
+** An OP_Filter has just been generated, but the corresponding
+** index search has not yet been performed.  This routine
+** checks to see if there are additional WHERE_BLOOMFILTER in
+** inner loops that can be evaluated right away, and if there are,
+** it evaluates those filters as well, and removes the WHERE_BLOOMFILTER
+** tag.
+*/
+static SQLITE_NOINLINE void filterPullDown(
+  Parse *pParse,       /* Parsing context */
+  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
+  int iLevel,          /* Which level of pWInfo->a[] should be coded */
+  int addrNxt,         /* Jump here to bypass inner loops */
+  Bitmask notReady     /* Loops that are not ready */
+){
+  while( ++iLevel < pWInfo->nLevel ){
+    WhereLevel *pLevel = &pWInfo->a[iLevel];
+    WhereLoop *pLoop = pLevel->pWLoop;
+    if( (pLoop->wsFlags & WHERE_BLOOMFILTER)==0 ) continue;
+    if( pLoop->prereq & notReady ) continue;
+    sqlite3ConstructBloomFilter(pWInfo, &pWInfo->a[iLevel]);
+    if( pLoop->wsFlags & WHERE_IPK ){
+      WhereTerm *pTerm = pLoop->aLTerm[0];
+      int r1, regRowid;
+      assert( pTerm!=0 );
+      assert( pTerm->pExpr!=0 );
+      testcase( pTerm->wtFlags & TERM_VIRTUAL );
+      r1 = sqlite3GetTempReg(pParse);
+      regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, r1);
+      if( regRowid!=r1 ) sqlite3ReleaseTempReg(pParse, r1);
+      sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
+                           addrNxt, regRowid, 1);
+      VdbeCoverage(pParse->pVdbe);
+    }else{
+      u16 nEq = pLoop->u.btree.nEq;
+      int r1;
+      char *zStartAff;
+
+      assert( pLoop->wsFlags & WHERE_INDEXED );
+      r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
+      codeApplyAffinity(pParse, r1, nEq, zStartAff);
+      sqlite3DbFree(pParse->db, zStartAff);
+      sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
+                           addrNxt, r1, nEq);
+      VdbeCoverage(pParse->pVdbe);
+    }
+    pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
+  }
+}
+#else
+#define filterPullDown(A,B,C,D,E)
+#endif
+
 /*
 ** Generate code for the start of the iLevel-th loop in the WHERE clause
 ** implementation described by pWInfo.
@@ -1518,6 +1572,7 @@ Bitmask sqlite3WhereCodeOneLoopStart(
       sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
                            iRowidReg, 1);
       VdbeCoverage(v);
+      filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
     }
     sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
     VdbeCoverage(v);
@@ -1848,6 +1903,7 @@ Bitmask sqlite3WhereCodeOneLoopStart(
         sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
                              regBase, nEq);
         VdbeCoverage(v);
+        filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
       }
 
       op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];