From 451e76d5b594d48d3450057ac542c7635b74b021 Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 21 Jan 2017 16:54:19 +0000 Subject: [PATCH 1/2] B-tree optimization: When seeking on a rowid table that has already been positioned, check to see if the new row happens to be the next row on the same leaf page. That is a reasonably common case, and if it is true it avoids a full binary search. FossilOrigin-Name: 8e5cfb2039126da7689c4b1c88760f10e1234eaf --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 334c7a3604..cbd159d22a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\ssqlite3_blob_reopen()\sto\scall\ssqlite3VdbeExec()\sdirectly\srather\sthan\ngoing\sthrough\ssqlite3_step().\s\sPerformance\senhancement. -D 2017-01-21T16:27:56.517 +C B-tree\soptimization:\s\sWhen\sseeking\son\sa\srowid\stable\sthat\shas\salready\sbeen\npositioned,\scheck\sto\ssee\sif\sthe\snew\srow\shappens\sto\sbe\sthe\snext\srow\son\sthe\nsame\sleaf\spage.\s\sThat\sis\sa\sreasonably\scommon\scase,\sand\sif\sit\sis\strue\sit\navoids\sa\sfull\sbinary\ssearch. +D 2017-01-21T16:54:19.338 F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da @@ -333,7 +333,7 @@ F src/auth.c 930b376a9c56998557367e6f7f8aaeac82a2a792 F src/backup.c faf17e60b43233c214aae6a8179d24503a61e83b F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 0e9ce2d56159b89b9bc8e197e023ee11e39ff8ca -F src/btree.c e6b8f39d7dd1ad65f1747956de36f2595172a23d +F src/btree.c 972b7e1346eb2f4369cf8cc43117c728e2bd9cd4 F src/btree.h e6d352808956ec163a17f832193a3e198b3fb0ac F src/btreeInt.h 10c4b77c2fb399580babbcc7cf652ac10dba796e F src/build.c 9e799f1edd910dfa8a0bc29bd390d35d310596af @@ -1547,7 +1547,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 0d1ad13a296b22d6fe36879b56f99bd6af1acd3a -R 6bebf342d641742884692400aca75850 +P 347df3c1fd7322e7aacaf1e9f8be81830947c482 +R 3888200d85fb0a6e771ec0a3c9f2e5a8 U drh -Z fb104ae24cf906c93b7fa008d273f235 +Z 4b0826cf6fb92296cc5aa9f45d953141 diff --git a/manifest.uuid b/manifest.uuid index 56e58c0ea3..ac357ef297 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -347df3c1fd7322e7aacaf1e9f8be81830947c482 \ No newline at end of file +8e5cfb2039126da7689c4b1c88760f10e1234eaf \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index fb05655912..8b095b57b0 100644 --- a/src/btree.c +++ b/src/btree.c @@ -5091,9 +5091,21 @@ int sqlite3BtreeMovetoUnpacked( *pRes = 0; return SQLITE_OK; } - if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKeyinfo.nKeycurFlags & BTCF_AtLast)!=0 ){ + *pRes = -1; + return SQLITE_OK; + } + if( pCur->aiIdx[pCur->iPage]+1apPage[pCur->iPage]->nCell ){ + pCur->aiIdx[pCur->iPage]++; + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + getCellInfo(pCur); + if( pCur->info.nKey==intKey ){ + *pRes = 0; + return SQLITE_OK; + } + } } } From 7f11afaba17fdc24ef01775613e4052a4100bbcf Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 21 Jan 2017 21:47:54 +0000 Subject: [PATCH 2/2] A better implementation of the moveto-neighbor optimization that checks for nearby rows on adjacent pages. FossilOrigin-Name: 2c4ecb85a475b9063aa8a3bb517ac181a7ded649 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index cbd159d22a..95352ee96a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C B-tree\soptimization:\s\sWhen\sseeking\son\sa\srowid\stable\sthat\shas\salready\sbeen\npositioned,\scheck\sto\ssee\sif\sthe\snew\srow\shappens\sto\sbe\sthe\snext\srow\son\sthe\nsame\sleaf\spage.\s\sThat\sis\sa\sreasonably\scommon\scase,\sand\sif\sit\sis\strue\sit\navoids\sa\sfull\sbinary\ssearch. -D 2017-01-21T16:54:19.338 +C A\sbetter\simplementation\sof\sthe\smoveto-neighbor\soptimization\sthat\schecks\sfor\nnearby\srows\son\sadjacent\spages. +D 2017-01-21T21:47:54.621 F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da @@ -333,7 +333,7 @@ F src/auth.c 930b376a9c56998557367e6f7f8aaeac82a2a792 F src/backup.c faf17e60b43233c214aae6a8179d24503a61e83b F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 0e9ce2d56159b89b9bc8e197e023ee11e39ff8ca -F src/btree.c 972b7e1346eb2f4369cf8cc43117c728e2bd9cd4 +F src/btree.c f1b36bcfb1f9532ce64fe534153f11b8e2595d8b F src/btree.h e6d352808956ec163a17f832193a3e198b3fb0ac F src/btreeInt.h 10c4b77c2fb399580babbcc7cf652ac10dba796e F src/build.c 9e799f1edd910dfa8a0bc29bd390d35d310596af @@ -1547,7 +1547,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 347df3c1fd7322e7aacaf1e9f8be81830947c482 -R 3888200d85fb0a6e771ec0a3c9f2e5a8 +P 8e5cfb2039126da7689c4b1c88760f10e1234eaf +R 9f1865839aca85688e5829c506350f09 U drh -Z 4b0826cf6fb92296cc5aa9f45d953141 +Z fbf63ba541a6f5ec3e59c684016e63a0 diff --git a/manifest.uuid b/manifest.uuid index ac357ef297..478c310b27 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8e5cfb2039126da7689c4b1c88760f10e1234eaf \ No newline at end of file +2c4ecb85a475b9063aa8a3bb517ac181a7ded649 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 8b095b57b0..b6bc66ea85 100644 --- a/src/btree.c +++ b/src/btree.c @@ -5096,14 +5096,19 @@ int sqlite3BtreeMovetoUnpacked( *pRes = -1; return SQLITE_OK; } - if( pCur->aiIdx[pCur->iPage]+1apPage[pCur->iPage]->nCell ){ - pCur->aiIdx[pCur->iPage]++; - pCur->info.nSize = 0; - pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); - getCellInfo(pCur); - if( pCur->info.nKey==intKey ){ - *pRes = 0; - return SQLITE_OK; + /* If the requested key is one more than the previous key, then + ** try to get there using sqlite3BtreeNext() rather than a full + ** binary search. This is an optimization only. The correct answer + ** is still obtained without this ase, only a little more slowely */ + if( pCur->info.nKey+1==intKey && !pCur->skipNext ){ + *pRes = 0; + rc = sqlite3BtreeNext(pCur, pRes); + if( rc ) return rc; + if( *pRes==0 ){ + getCellInfo(pCur); + if( pCur->info.nKey==intKey ){ + return SQLITE_OK; + } } } }