]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a performance problem in queries that use "ORDER BY rowid DESC" and one or more...
authordan <dan@noemail.net>
Wed, 4 May 2011 15:41:18 +0000 (15:41 +0000)
committerdan <dan@noemail.net>
Wed, 4 May 2011 15:41:18 +0000 (15:41 +0000)
FossilOrigin-Name: 95e09b20e9aad28f829c8950f3632debe473070a

ext/fts3/fts3.c
manifest
manifest.uuid
test/fts3sort.test

index faf497d9a0a37a55bf8becc057abc3682070d6ba..5aba8491378d065111b06274042a27573fa8c402 100644 (file)
@@ -3321,6 +3321,25 @@ int sqlite3Fts3ExprLoadFtDoclist(
   return rc;
 }
 
+
+/*
+** When called, *ppPoslist must point to the byte immediately following the
+** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
+** moves *ppPoslist so that it instead points to the first byte of the
+** position list.
+*/
+static void fts3ReversePoslist(char *pStart, char **ppPoslist){
+  char *p = &(*ppPoslist)[-3];
+  char c = p[1];
+  while( p>=pStart && (*p & 0x80) | c ){ 
+    c = *p--; 
+  }
+  if( p>pStart ){ p = &p[2]; }
+  while( *p++&0x80 );
+  *ppPoslist = p;
+}
+
+
 /*
 ** After ExprLoadDoclist() (see above) has been called, this function is
 ** used to iterate/search through the position lists that make up the doclist
@@ -3337,21 +3356,37 @@ char *sqlite3Fts3FindPositions(
     char *pEnd = &pExpr->aDoclist[pExpr->nDoclist];
     char *pCsr;
 
-    if( pExpr->pCurrent==0 || pCursor->desc ){
-      pExpr->pCurrent = pExpr->aDoclist;
-      pExpr->iCurrent = 0;
-      pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent);
+    if( pExpr->pCurrent==0 ){
+      if( pCursor->desc==0 ){
+        pExpr->pCurrent = pExpr->aDoclist;
+        pExpr->iCurrent = 0;
+        fts3GetDeltaVarint(&pExpr->pCurrent, &pExpr->iCurrent);
+      }else{
+        pCsr = pExpr->aDoclist;
+        while( pCsr<pEnd ){
+          fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
+          fts3PoslistCopy(0, &pCsr);
+        }
+        fts3ReversePoslist(pExpr->aDoclist, &pCsr);
+        pExpr->pCurrent = pCsr;
+      }
     }
     pCsr = pExpr->pCurrent;
     assert( pCsr );
 
-    while( pCsr<pEnd ){
-      if( pExpr->iCurrent<iDocid ){
+    while( (pCursor->desc==0 && pCsr<pEnd) 
+        || (pCursor->desc && pCsr>pExpr->aDoclist) 
+    ){
+      if( pCursor->desc==0 && pExpr->iCurrent<iDocid ){
         fts3PoslistCopy(0, &pCsr);
         if( pCsr<pEnd ){
           fts3GetDeltaVarint(&pCsr, &pExpr->iCurrent);
         }
         pExpr->pCurrent = pCsr;
+      }else if( pCursor->desc && pExpr->iCurrent>iDocid ){
+        fts3GetReverseDeltaVarint(&pCsr, pExpr->aDoclist, &pExpr->iCurrent);
+        fts3ReversePoslist(pExpr->aDoclist, &pCsr);
+        pExpr->pCurrent = pCsr;
       }else{
         if( pExpr->iCurrent==iDocid ){
           int iThis = 0;
index fde6974ea36e1d79b770f159d576d942f199798e..6850c6a534ab69b763c6ea1e703bacb706818f5a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Optimize\s"ORDER\sBY\srowid/docid\sDESC/ASC"\sclauses\son\sFTS\stables.
-D 2011-05-04T12:52:59.896
+C Fix\sa\sperformance\sproblem\sin\squeries\sthat\suse\s"ORDER\sBY\srowid\sDESC"\sand\sone\sor\smore\sFTS\sauxiliary\sfunctions.
+D 2011-05-04T15:41:18.367
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 7a4d9524721d40ef9ee26f93f9bd6a51dba106f2
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -61,7 +61,7 @@ F ext/fts2/mkfts2amal.tcl 974d5d438cb3f7c4a652639262f82418c1e4cff0
 F ext/fts3/README.syntax a19711dc5458c20734b8e485e75fb1981ec2427a
 F ext/fts3/README.tokenizers 998756696647400de63d5ba60e9655036cb966e9
 F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts3/fts3.c 47e4f4da599e0ccd7b7fea08aaf2c77544e278e3
+F ext/fts3/fts3.c 18bf7e4eca7dcea86300ee4ead2427067c3b04c3
 F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe
 F ext/fts3/fts3Int.h 8c2ac39ee17362571c58ab2c4f0667324c31f738
 F ext/fts3/fts3_aux.c 9e931f55eed8498dafe7bc1160f10cbb1a652fdf
@@ -475,7 +475,7 @@ F test/fts3query.test ef79d31fdb355d094baec1c1b24b60439a1fb8a2
 F test/fts3rnd.test 2b1a579be557ab8ac54a51b39caa4aa8043cc4ad
 F test/fts3shared.test 8bb266521d7c5495c0ae522bb4d376ad5387d4a2
 F test/fts3snippet.test a12f22a3ba4dd59751a57c79b031d07ab5f51ddd
-F test/fts3sort.test b33d4650e8d4bff2dc00d14359a29cd1c25769f8
+F test/fts3sort.test e6f24e9cffc46484bcc9fe63d3c2ce41afcaa6c9
 F test/fts4aa.test eadf85621c0a113d4c7ad3ccbf8441130e007b8f
 F test/func.test 6c5ce11e3a0021ca3c0649234e2d4454c89110ca
 F test/func2.test 772d66227e4e6684b86053302e2d74a2500e1e0f
@@ -933,7 +933,7 @@ F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 822ab52f1023b1c4973c806cc75454acd4e95fd0
-R bb599d547c2452f152daafa893566415
+P 13395121e3d17ab6581dc5f6736ea324321a374c
+R b72c70132342e56f35c0ae4fa3c076bd
 U dan
-Z 8f8ca68f3fd8a51bab340e5fb86e9e14
+Z f70b0240fad2f490b24e91d4b974b53a
index 12102a8f2723ca5c1e394da619953da5bbe065a4..ce4b5a06e5e621e743f22a87a878261f4422723f 100644 (file)
@@ -1 +1 @@
-13395121e3d17ab6581dc5f6736ea324321a374c
\ No newline at end of file
+95e09b20e9aad28f829c8950f3632debe473070a
\ No newline at end of file
index 3f833a65cb7ac8dc60607a41c1fc363952617fb3..001bef113533ecc80c99a918eae1b09012ba9785 100644 (file)
@@ -58,6 +58,7 @@ foreach {tn query} {
   7   "SELECT docid, * FROM t1 WHERE t1 MATCH 'aa OR da'"
   8   "SELECT docid, * FROM t1 WHERE t1 MATCH 'nosuchtoken'"
   9   "SELECT docid, snippet(t1) FROM t1 WHERE t1 MATCH 'aa OR da'"
+  10  "SELECT docid, snippet(t1) FROM t1 WHERE t1 MATCH 'aa OR nosuchtoken'"
 } {
 
   unset -nocomplain A B C D