]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
When choosing between two indexes with the same cost, pick the one with the consider-idx-width
authordrh <>
Wed, 15 Jan 2025 20:23:22 +0000 (20:23 +0000)
committerdrh <>
Wed, 15 Jan 2025 20:23:22 +0000 (20:23 +0000)
smaller predicted number of bytes per row.

FossilOrigin-Name: d4bd0d4214551f88f248698fefc821575b722ce5c194d0b3796f572e4704f641

manifest
manifest.uuid
src/where.c

index c560adf18af2c63a360930bb1ceaeb2cacf94486..6da9fb0a2b44e3faadb7d38249068721cbe9362d 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Variable\snames\sand\sconditional\slogic\ssimplified\sin\swhere.c.\s\sThese\nchanges\sare\scosmetic\sonly\sand\sdo\snot\saffect\sthe\sresuling\smachine\scode.
-D 2025-01-15T19:30:10.144
+C When\schoosing\sbetween\stwo\sindexes\swith\sthe\ssame\scost,\spick\sthe\sone\swith\sthe\nsmaller\spredicted\snumber\sof\sbytes\sper\srow.
+D 2025-01-15T20:23:22.162
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d
@@ -862,7 +862,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c 4e6181d8780ab0af2e1388d0754cbe6f2f04593d2b1ab6c41699a89942fd8997
 F src/wal.h ba252daaa94f889f4b2c17c027e823d9be47ce39da1d3799886bbd51f0490452
 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014
-F src/where.c 066374c086ded30e6829a909df532c22f6fb03b1a29ef1bc0f34f45bd7895687
+F src/where.c f081a371086f48201948431832f5e9bb1bf6c930397b8d7bcf8aaa9e21d819da
 F src/whereInt.h 2b0804f300c7f65de4046a1d81c65f01b208d6c08950ccd1fa6b8c16162a8af7
 F src/wherecode.c 0c3d3199a2b769a5e2bb70feb5003dc85b3d86842ecaf903a47f2b4205ca5dab
 F src/whereexpr.c 0f93a29cabd3a338d09a1f5c6770620a1ac51ec1157f3229502a7e7767c60b6f
@@ -2205,8 +2205,11 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350
 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 7cfc75a625095e41c3e952d4a209fdbb344df0d3dd9b0103e9e6a986dfe53c89
-R d91733ae99ec500ae8110e1fa74b02f9
+P dbc2d6a244fdafd208239894dbdd8f139db6ca20dd8f1ed00d87028e0cb60570
+R b4fed608ae80dc9fc4a5de4904cc7901
+T *branch * consider-idx-width
+T *sym-consider-idx-width *
+T -sym-trunk *
 U drh
-Z 7b31834eebc351dc793c0a6c7bc753c6
+Z dfdb92c016b02704a64b8a1056c8f180
 # Remove this line to create a well-formed Fossil manifest.
index 14ca5452553bb95223632569f5bd93a2c7839488..a279b0e281e50acf04eaa56dddaa7476b7140301 100644 (file)
@@ -1 +1 @@
-dbc2d6a244fdafd208239894dbdd8f139db6ca20dd8f1ed00d87028e0cb60570
+d4bd0d4214551f88f248698fefc821575b722ce5c194d0b3796f572e4704f641
index 25b64bcaad1d16265752b92b84eb426688863b08..05dab8fdd501378269e7d6f118582ead247604d4 100644 (file)
@@ -5490,6 +5490,28 @@ static int computeMxChoice(WhereInfo *pWInfo, LogEst nRowEst){
   return pWInfo->nOutStarDelta>0 ? 18 : 12;
 }
 
+/*
+** Two WhereLoop objects, pCandidate and pBaseline, are known to have the
+** same cost.  Look deep into each to see if pCandidate is even slightly
+** better than pBaseline.  Return false if it is, if pCandidate is is preferred.
+** Return true if pBaseline is preferred or if we cannot tell the difference.
+**
+**    Result       Meaning
+**    --------     ----------------------------------------------------------
+**    true         We cannot tell the difference in pCandidate and pBaseline
+**    false        pCandidate seems like a better choice than pBaseline
+*/
+static SQLITE_NOINLINE int whereLoopIsNoBetter(
+  const WhereLoop *pCandidate,
+  const WhereLoop *pBaseline
+){
+  if( (pCandidate->wsFlags & WHERE_INDEXED)==0 ) return 1;
+  if( (pBaseline->wsFlags & WHERE_INDEXED)==0 ) return 1;
+  if( pCandidate->u.btree.pIndex->szIdxRow <
+        pBaseline->u.btree.pIndex->szIdxRow ) return 0;
+  return 1;
+}
+
 /*
 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
 ** attempts to find the lowest cost path that visits each WhereLoop
@@ -5728,7 +5750,9 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
           */
           if( (pTo->rCost<rCost)
            || (pTo->rCost==rCost && pTo->nRow<nOut)
-           || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort<=rUnsort)
+           || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort<rUnsort)
+           || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort==rUnsort
+                  && whereLoopIsNoBetter(pWLoop, pTo->aLoop[iLoop]) )
           ){
 #ifdef WHERETRACE_ENABLED /* 0x4 */
             if( sqlite3WhereTrace&0x4 ){