]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Change the cost estimator in the query planner to take into account the
authordrh <drh@noemail.net>
Mon, 24 Jan 2011 15:11:23 +0000 (15:11 +0000)
committerdrh <drh@noemail.net>
Mon, 24 Jan 2011 15:11:23 +0000 (15:11 +0000)
logN rowid lookup cost when going from an index to a table.

FossilOrigin-Name: b442525b0ba642bb8d57b87b7b9e373b6046454a

manifest
manifest.uuid
src/where.c

index 2e9f7b38fcc2c8042bbc4985e87de338a83c42dc..6b94cf8473bbcab7c055fb536a0070243d7debc6 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Add\sthe\sability\sto\suse\sindices\sfor\sconstraints\sof\sthe\sform\s"x\sIS\sNOT\sNULL"\nwhen\ssqlite_stat2\sis\savailable\sand\smost\sentries\sfor\scolumn\sx\sare\sNULL.
-D 2011-01-22T00:10:45.721
+C Change\sthe\scost\sestimator\sin\sthe\squery\splanner\sto\stake\sinto\saccount\sthe\nlogN\srowid\slookup\scost\swhen\sgoing\sfrom\san\sindex\sto\sa\stable.
+D 2011-01-24T15:11:23.443
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in de6498556d536ae60bb8bb10e8c1ba011448658c
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -243,7 +243,7 @@ F src/vtab.c b297e8fa656ab5e66244ab15680d68db0adbec30
 F src/wal.c dbca424f71678f663a286ab2a98f947af1d412a7
 F src/wal.h c1aac6593a0b02b15dc625987e619edeab39292e
 F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
-F src/where.c 99a9ea77114b649d68d01127331119f6785a80f1
+F src/where.c 87de2616150606fd1b61d7c88afdb90089ddd53d
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87
 F test/all.test 51756962d522e474338e9b2ebb26e7364d4aa125
@@ -900,14 +900,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P c82cb9c028b3ba5463ae50c30196dbf157a7a305
-R 8c710a35ac2f95522b4422902520d5c8
+P 5d5bddd290e71a7b03bcc23ff29881c23233cbff
+R 803835303ce0b4a50432effc77dd4645
 U drh
-Z 5feaab9c960a4232f37e5b9d507f4c5a
+Z 6e3a0f2d24eebc65cc3b5cda90be2fe9
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFNOiCJoxKgR168RlERAvjuAKCFxe3Zz4WQnNCqaR5BtD/txHvS9QCePp1G
-iZQ2yz7nxUFtZ+UwOppTLQo=
-=91DY
+iD4DBQFNPZaeoxKgR168RlERAhweAJipv5VYvhXPpe7yt5oZmj9HR78gAJ9JRIoC
+ox/GIojaWZ/2rYPgDsVVnQ==
+=pp6G
 -----END PGP SIGNATURE-----
index 372d8e6a1e28b7f55d6b21f52b1c217a2a33106e..78fe186878f5a2181202781ce366ffb209f9143d 100644 (file)
@@ -1 +1 @@
-5d5bddd290e71a7b03bcc23ff29881c23233cbff
\ No newline at end of file
+b442525b0ba642bb8d57b87b7b9e373b6046454a
\ No newline at end of file
index 8a7e258bd9f35a208f9aaa0887f9e26ff8c7560d..d0540a4903f99f89cf7b96603c1044b6490af262 100644 (file)
@@ -2919,31 +2919,29 @@ static void bestBtreeIndex(
     }
 #endif /* SQLITE_ENABLE_STAT2 */
 
-    /* Assume constant cost to access a row and logarithmic cost to
-    ** do a binary search.  Hence, the initial cost is the number of output
-    ** rows plus log2(table-size) times the number of binary searches.
-    */
-    cost = nRow + nInMul*estLog(aiRowEst[0]);
-
     /* Adjust the number of rows and the cost downward to reflect rows
     ** that are excluded by range constraints.
     */
     nRow = (nRow * (double)estBound) / (double)100;
-    cost = (cost * (double)estBound) / (double)100;
 
-    /* Add in the estimated cost of sorting the result
+    /* Assume constant cost to access a row and logarithmic cost to
+    ** do a binary search.  Hence, the initial cost is the number of output
+    ** rows plus log2(table-size) times the number of binary searches.
     */
-    if( bSort ){
-      cost += cost*estLog(cost);
+    if( pIdx && bLookup ){
+      cost = nRow + (nInMul+nRow)*estLog(aiRowEst[0]);
+    }else{
+      cost = nRow + nInMul*estLog(aiRowEst[0]);
     }
 
-    /* If all information can be taken directly from the index, we avoid
-    ** doing table lookups.  This reduces the cost by half.  (Not really -
-    ** this needs to be fixed.)
+    /* 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( pIdx && bLookup==0 ){
-      cost /= (double)2;
+    if( bSort ){
+      cost += nRow*estLog(nRow)*(double)3;
     }
+
     /**** Cost of using this index has now been computed ****/
 
     /* If there are additional constraints on this table that cannot