]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Make sure that an index that covers a proper superset of the WHERE clause
authordrh <drh@noemail.net>
Mon, 31 Mar 2014 18:24:18 +0000 (18:24 +0000)
committerdrh <drh@noemail.net>
Mon, 31 Mar 2014 18:24:18 +0000 (18:24 +0000)
terms of some other index has a lower cost than the other index.

FossilOrigin-Name: ea8b0910040198751551b0b960e6b783913607df

manifest
manifest.uuid
src/where.c

index 9ef65fa6b92c01b3883bac303c8a7294be84844a..ac1faf182f8aacc06af5b18de67473909146e9d2 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Experiments\sin\spicking\sbetter\squery\splans,\sespecially\swhen\sthe\susage\sof\sone\nindex\sis\sa\ssubset\sof\sanother.
-D 2014-03-29T21:16:07.103
+C Make\ssure\sthat\san\sindex\sthat\scovers\sa\sproper\ssuperset\sof\sthe\sWHERE\sclause\nterms\sof\ssome\sother\sindex\shas\sa\slower\scost\sthan\sthe\sother\sindex.
+D 2014-03-31T18:24:18.867
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -291,7 +291,7 @@ F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd
 F src/wal.c 76e7fc6de229bea8b30bb2539110f03a494dc3a8
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 11edb74d587bc87b33ca96a5173e3ec1b8389e45
-F src/where.c 26c6f6260b0cc2b0038dcc68ace0d7db20c7dcee
+F src/where.c 50ac3154473b5c8df15c7b8dbd19da385fa859e1
 F src/whereInt.h 2564055b440e44ebec8b47f237bbccae6719b7af
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
@@ -1159,10 +1159,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P 27deb6e49bcc76714dbdc61b34748603155ac770
-R 1df7985395668b26c614cc4e641f29c8
-T *branch * query-plan-experiments
-T *sym-query-plan-experiments *
-T -sym-trunk *
+P 8f869ca7a6eaa9ca7a08102290e6c606735f9090
+R 76b8d8088ba83ec014dc8bf476fa6749
 U drh
-Z 1c5ff3f986da6bbe350c60cf72c963dd
+Z ecebe13d1c9f7ea1da970c5f6118dc52
index 05916c40aa607837461cf3a14a87a35f6932396e..649fc9530b1e3df279949a7e6d0c12b73d9339b9 100644 (file)
@@ -1 +1 @@
-8f869ca7a6eaa9ca7a08102290e6c606735f9090
\ No newline at end of file
+ea8b0910040198751551b0b960e6b783913607df
\ No newline at end of file
index c2b2cf2111b714cf6b20d21f269d2435f47cc540..38c3712977c09c50637ccc2220e0a096a63b3a39 100644 (file)
@@ -3711,6 +3711,51 @@ static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
   }
 }
 
+/*
+** Compare every WhereLoop X on the list p against pTemplate.  If:
+**
+**    (1) both X and pTemplate refer to the same table, and
+**    (2) both X and pTemplate use a single index, and
+**    (3) pTemplate uses all the same WHERE clause terms as X plus
+**        at least one more term,
+**
+** then make sure the cost pTemplate is less than the cost of X, adjusting
+** the cost of pTemplate downward if necessary.
+**
+** Example: When computing the query plan for the SELECT below:
+**
+**     CREATE TABLE t1(a,b,c,d);
+**     CREATE INDEX t1abc ON t1(a,b,c);
+**     CREATE INDEX t1bc ON t1(b,c);
+**     SELECT d FROM t1 WHERE a=? AND b=? AND c>=? ORDER BY c;
+** 
+** Make sure the cost of using three three columns of index t1abc is less
+** than the cost of using both columns of t1bc.
+*/
+static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
+  if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
+  if( pTemplate->nLTerm==0 ) return;
+  for(; p; p=p->pNextLoop){
+    if( p->iTab==pTemplate->iTab
+     && (p->wsFlags & WHERE_INDEXED)!=0
+     && p->nLTerm<pTemplate->nLTerm
+     && (p->rRun<pTemplate->rRun || (p->rRun==pTemplate->rRun &&
+                                     p->nOut<=pTemplate->nOut))
+    ){
+      int i, j;
+      for(j=0, i=p->nLTerm-1; i>=0 && j>=0; i--){
+        for(j=pTemplate->nLTerm-1; j>=0; j--){
+          if( pTemplate->aLTerm[j]==p->aLTerm[i] ) break;
+        }
+      }
+      if( j>=0 ){
+        pTemplate->rRun = p->rRun;
+        pTemplate->nOut = p->nOut - 1;
+      }
+    }
+  }
+}
+
 /*
 ** Search the list of WhereLoops in *ppPrev looking for one that can be
 ** supplanted by pTemplate.
@@ -3747,39 +3792,30 @@ static WhereLoop **whereLoopFindLesser(
     ** rSetup. Call this SETUP-INVARIANT */
     assert( p->rSetup>=pTemplate->rSetup );
 
-    if( (p->prereq & pTemplate->prereq)==p->prereq
-     && p->rSetup<=pTemplate->rSetup
-     && p->rRun<=pTemplate->rRun
-     && p->nOut<=pTemplate->nOut
+    /* If existing WhereLoop p is better than pTemplate, pTemplate can be
+    ** discarded.  WhereLoop p is better if:
+    **   (1)  p has no more dependencies than pTemplate, and
+    **   (2)  p has an equal or lower cost than pTemplate
+    */
+    if( (p->prereq & pTemplate->prereq)==p->prereq    /* (1)  */
+     && p->rSetup<=pTemplate->rSetup                  /* (2a) */
+     && p->rRun<=pTemplate->rRun                      /* (2b) */
+     && p->nOut<=pTemplate->nOut                      /* (2c) */
     ){
-      /* This branch taken when p is equal or better than pTemplate in 
-      ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
-      ** (4) number of output rows. */
-      assert( p->rSetup==pTemplate->rSetup );
-      if( p->prereq==pTemplate->prereq
-       && p->nLTerm<pTemplate->nLTerm
-       && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
-       && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
-          || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
-      ){
-        /* Overwrite an existing WhereLoop with an similar one that uses
-        ** more terms of the index */
-        break;
-      }else{
-        /* There is an existing WhereLoop that is better than pTemplate */
-        return 0;
-      }
+      return 0;  /* Discard pTemplate */
     }
-    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
-     && p->rRun>=pTemplate->rRun
-     && p->nOut>=pTemplate->nOut
+
+    /* If pTemplate is always better than p, then cause p to be overwritten
+    ** with pTemplate.  pTemplate is better than p if:
+    **   (1)  pTemplate has no more dependences than p, and
+    **   (2)  pTemplate has an equal or lower cost than p.
+    */
+    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq   /* (1)  */
+     && p->rRun>=pTemplate->rRun                             /* (2a) */
+     && p->nOut>=pTemplate->nOut                             /* (2b) */
     ){
-      /* Overwrite an existing WhereLoop with a better one: one that is
-      ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
-      ** or (4) number of output rows, and is no worse in any of those
-      ** categories. */
       assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
-      break;
+      break;   /* Cause p to be overwritten by pTemplate */
     }
   }
   return ppPrev;
@@ -3801,15 +3837,13 @@ static WhereLoop **whereLoopFindLesser(
 **
 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
 ** still might overwrite similar loops with the new template if the
-** template is better.  Loops may be overwritten if the following 
+** new template is better.  Loops may be overwritten if the following 
 ** conditions are met:
 **
 **    (1)  They have the same iTab.
 **    (2)  They have the same iSortIdx.
 **    (3)  The template has same or fewer dependencies than the current loop
 **    (4)  The template has the same or lower cost than the current loop
-**    (5)  The template uses more terms of the same index but has no additional
-**         dependencies          
 */
 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
   WhereLoop **ppPrev, *p;
@@ -3837,6 +3871,7 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
 
   /* Look for an existing WhereLoop to replace with pTemplate
   */
+  whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
   ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
 
   if( ppPrev==0 ){