From: drh Date: Mon, 24 Jan 2011 15:11:23 +0000 (+0000) Subject: Change the cost estimator in the query planner to take into account the X-Git-Tag: version-3.7.6~166^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b966194a63d2dac670a22c40098803a6c2d1e761;p=thirdparty%2Fsqlite.git Change the cost estimator in the query planner to take into account the logN rowid lookup cost when going from an index to a table. FossilOrigin-Name: b442525b0ba642bb8d57b87b7b9e373b6046454a --- diff --git a/manifest b/manifest index 2e9f7b38fc..6b94cf8473 100644 --- 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----- diff --git a/manifest.uuid b/manifest.uuid index 372d8e6a1e..78fe186878 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5d5bddd290e71a7b03bcc23ff29881c23233cbff \ No newline at end of file +b442525b0ba642bb8d57b87b7b9e373b6046454a \ No newline at end of file diff --git a/src/where.c b/src/where.c index 8a7e258bd9..d0540a4903 100644 --- a/src/where.c +++ b/src/where.c @@ -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