]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Use macros to define the relative costs of search and seek operations when
authordrh <drh@noemail.net>
Wed, 9 Feb 2011 03:04:27 +0000 (03:04 +0000)
committerdrh <drh@noemail.net>
Wed, 9 Feb 2011 03:04:27 +0000 (03:04 +0000)
computing costs in the query planner.  Current constants seems wrong and
need to be fixed, but doing so will alter test results.  Need more
experimentation to determine accurate relative costs.

FossilOrigin-Name: 5f2ec44b22062ee9d31e20806fcec0101675aced

manifest
manifest.uuid
src/where.c

index 48381bfa0b4b54d31a77f2a515d032a129d565a3..81c8e9f0a4865e018b542728f7887377c1698459 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Simplifications\sto\sthe\ssqlite3_wal_checkpoint_v2()\slogic.
-D 2011-02-09T03:03:16.067
+C Use\smacros\sto\sdefine\sthe\srelative\scosts\sof\ssearch\sand\sseek\soperations\swhen\ncomputing\scosts\sin\sthe\squery\splanner.\s\sCurrent\sconstants\sseems\swrong\sand\nneed\sto\sbe\sfixed,\sbut\sdoing\sso\swill\salter\stest\sresults.\s\sNeed\smore\nexperimentation\sto\sdetermine\saccurate\srelative\scosts.
+D 2011-02-09T03:04:27.847
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in de6498556d536ae60bb8bb10e8c1ba011448658c
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -244,7 +244,7 @@ F src/vtab.c b297e8fa656ab5e66244ab15680d68db0adbec30
 F src/wal.c aca10a60655e103fc8630a75345000f43c6d47ca
 F src/wal.h 7a5fbb00114b7f2cd40c7e1003d4c41ce9d26840
 F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
-F src/where.c f4915ac03e5e42c8416b35ca3ba34af841c00d12
+F src/where.c 0ff78ba4787cfc6895be1faed5b4ea98b7af7cfe
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87
 F test/all.test 51756962d522e474338e9b2ebb26e7364d4aa125
@@ -909,14 +909,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P f611a5a879b7dec1ed1d8bf32413c8a6b81c3172
-R 9eb1d838d1c9a6fbbece39c189fe6c11
+P 652b8835c58fc9d474c9837fc966d8857bec4a91
+R dc10e138c11d2df9e091fafd8b2305be
 U drh
-Z e85349cf8286d1975032a9f080ac1cff
+Z c827e0483e5251e34ceea49900655182
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFNUgP3oxKgR168RlERArjhAJ96GiP4XsmJ4dEmAmrcql1CFdWpdgCfQCol
-GiaXt0mATDvm3Lt3VJZa5pQ=
-=b8rA
+iD8DBQFNUgQ/oxKgR168RlERAsnhAKCNq6HCp8DfmhZXl1Ls31douSQsIgCeNpoe
+cLzGC6C2mFonzZxCo59TByY=
+=JI+r
 -----END PGP SIGNATURE-----
index bf29f7d33f2e92e5b381d3eb9a3ca5f1e3dbda63..ede0fae8f19e62c82603cec0cdddf4d9320faf2e 100644 (file)
@@ -1 +1 @@
-652b8835c58fc9d474c9837fc966d8857bec4a91
\ No newline at end of file
+5f2ec44b22062ee9d31e20806fcec0101675aced
\ No newline at end of file
index 6d660e8cc0e791fd1a9103512ff65b97c0ea1c5c..5f892813de3c92fb487f50e12d0b767c8db75e8f 100644 (file)
 */
 #include "sqliteInt.h"
 
+
+/*
+** The following parameter define the relative cost of various
+** search operations.  These parameters are used to estimate query
+** plan costs and to select the query plan with the lowest estimated
+** cost.
+**
+** Let the cost of moving from one row in a table or index to the next
+** or previous row be SEEK_COST.
+**
+** Let the base-10 logarithm of the number of rows in a table or
+** index be L.  The estLog() function below will estimate this
+** numbmer given the number of rows in the table.
+**
+** The cost of doing a lookup of an index will be IDX_LKUP_COST*L.
+**
+** The cost of doing a lookup on a table is TBL_LKUP_COST*L.
+**
+** The cost of sorting a result set of N rows is assumed to be
+** N*log10(N)*SORT_COST.
+*/
+#if defined(SEEK_COST)
+  /* Assume that IDX_LKUP_COST, TBL_LKUP_COST, and SORT_COST are also defined */
+#elif !defined(SQLITE_OMIT_FLOATING_POINT)
+#  define SEEK_COST     1.0
+#  define IDX_LKUP_COST 1.0
+#  define TBL_LKUP_COST 0.1
+#  define SORT_COST     3.0
+#else
+#  define SEEK_COST     10
+#  define IDX_LKUP_COST 10
+#  define TBL_LKUP_COST 1
+#  define SORT_COST     30
+#endif
+
 /*
 ** Trace output macros
 */
@@ -1760,7 +1795,7 @@ static void bestAutomaticIndex(
   pWCEnd = &pWC->a[pWC->nTerm];
   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
-      WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
+      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
                     pCost->rCost, costTempIdx));
       pCost->rCost = costTempIdx;
       pCost->plan.nRow = logN + 1;
@@ -2736,8 +2771,9 @@ static void bestBtreeIndex(
     const unsigned int * const aiRowEst = pProbe->aiRowEst;
     double cost;                /* Cost of using pProbe */
     double nRow;                /* Estimated number of rows in result set */
+    double nTabSrch;            /* Est number of table searches */
+    double nIdxSrch;            /* Est number of index searches */
     int rev;                    /* True to scan in reverse order */
-    double nSearch;             /* Estimated number of binary searches */
     int wsFlags = 0;
     Bitmask used = 0;
 
@@ -2954,29 +2990,33 @@ static void bestBtreeIndex(
         **  + nRow steps through the index
         **  + nRow table searches to lookup the table entry using the rowid
         */
-        nSearch = nInMul + nRow/10;
+        nIdxSrch = nInMul;
+        nTabSrch = nRow;
       }else{
         /* For a covering index:
-        **     nInMul binary searches to find the initial entry 
+        **     nInMul index searches to find the initial entry 
         **   + nRow steps through the index
         */
-        nSearch = nInMul;
+        nIdxSrch = nInMul;
+        nTabSrch = 0;
       }
     }else{
       /* For a rowid primary key lookup:
-      **    nInMult binary searches to find the initial entry scaled by 1/10th
+      **    nInMult table searches to find the initial entry for each range
       **  + nRow steps through the table
       */
-      nSearch = nInMul/10;
+      nIdxSrch = 0;
+      nTabSrch = nInMul;
     }
-    cost = nRow + nSearch*estLog(aiRowEst[0]);
+    cost = nRow + (nIdxSrch*IDX_LKUP_COST + nTabSrch*TBL_LKUP_COST)
+                      *estLog(aiRowEst[0])/SEEK_COST;
 
     /* Add in the estimated cost of sorting the result.  This cost is expanded
     ** by a fudge factor of 3.0 to account for the fact that a sorting step 
     ** involves a write and is thus more expensive than a lookup step.
     */
     if( bSort ){
-      cost += nRow*estLog(nRow)*(double)3;
+      cost += nRow*estLog(nRow)*SORT_COST/SEEK_COST;
     }
 
     /**** Cost of using this index has now been computed ****/
@@ -3042,10 +3082,11 @@ static void bestBtreeIndex(
 
     WHERETRACE((
       "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
-      "         notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
+      "         notReady=0x%llx nTSrch=%.1f nISrch=%.1f nRow=%.1f\n"
+      "         estLog=%.1f cost=%.1f used=0x%llx\n",
       pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
       nEq, nInMul, estBound, bSort, bLookup, wsFlags,
-      notReady, nRow, cost, used
+      notReady, nTabSrch, nIdxSrch, nRow, estLog(aiRowEst[0]), cost, used
     ));
 
     /* If this index is the best we have seen so far, then record this