Much faster sorting when there are a large number of columns in the
result set. (CVS 3141)
FossilOrigin-Name: 6b3717aeb4ac45a433f2a30bdd0264ed728676e1
diff --git a/src/select.c b/src/select.c
index b8bd5f5..d1b6df2 100644
--- a/src/select.c
+++ b/src/select.c
@@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
-** $Id: select.c,v 1.307 2006/03/09 17:28:12 drh Exp $
+** $Id: select.c,v 1.308 2006/03/17 00:04:03 drh Exp $
*/
#include "sqliteInt.h"
@@ -680,6 +680,7 @@
** routine generates the code needed to do that.
*/
static void generateSortTail(
+ Parse *pParse, /* Parsing context */
Select *p, /* The SELECT statement */
Vdbe *v, /* Generate code into this VDBE */
int nColumn, /* Number of columns of data */
@@ -690,11 +691,20 @@
int cont = sqlite3VdbeMakeLabel(v);
int addr;
int iTab;
+ int pseudoTab;
ExprList *pOrderBy = p->pOrderBy;
iTab = pOrderBy->iECursor;
+ if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
+ pseudoTab = pParse->nTab++;
+ sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0);
+ sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn);
+ }
addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk);
codeOffset(v, p, cont, 0);
+ if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
+ sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
+ }
sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1);
switch( eDest ){
case SRT_Table:
@@ -724,17 +734,15 @@
case SRT_Callback:
case SRT_Subroutine: {
int i;
- sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0);
- sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
+ sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0);
for(i=0; i<nColumn; i++){
- sqlite3VdbeAddOp(v, OP_Column, -1-i, i);
+ sqlite3VdbeAddOp(v, OP_Column, pseudoTab, i);
}
if( eDest==SRT_Callback ){
sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
}else{
sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
}
- sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
break;
}
default: {
@@ -755,6 +763,10 @@
sqlite3VdbeResolveLabel(v, cont);
sqlite3VdbeAddOp(v, OP_Next, iTab, addr);
sqlite3VdbeResolveLabel(v, brk);
+ if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
+ sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0);
+ }
+
}
/*
@@ -1964,7 +1976,7 @@
pKeyInfo->nField = nOrderByExpr;
sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
pKeyInfo = 0;
- generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
+ generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
}
sqliteFree(pKeyInfo);
@@ -3253,7 +3265,7 @@
** and send them to the callback one by one.
*/
if( pOrderBy ){
- generateSortTail(p, v, pEList->nExpr, eDest, iParm);
+ generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
}
#ifndef SQLITE_OMIT_SUBQUERY
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index dd63745..93d7b9d 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
-** @(#) $Id: sqliteInt.h,v 1.489 2006/03/13 15:06:07 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.490 2006/03/17 00:04:04 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
@@ -1639,7 +1639,7 @@
#else
# define sqlite3TriggersExist(A,B,C,D,E,F) 0
# define sqlite3DeleteTrigger(A)
-# define sqlite3DropTriggerPtr(A,B,C)
+# define sqlite3DropTriggerPtr(A,B)
# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I) 0
#endif
diff --git a/src/vdbe.c b/src/vdbe.c
index 002c2df..c878de8 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
** in this file for details. If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
-** $Id: vdbe.c,v 1.545 2006/03/13 14:28:05 drh Exp $
+** $Id: vdbe.c,v 1.546 2006/03/17 00:04:04 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
@@ -1921,6 +1921,7 @@
** which is the number of records.
*/
assert( p1<0 || p->apCsr[p1]!=0 );
+#if 0
if( p1<0 ){
/* Take the record off of the stack */
Mem *pRec = &pTos[p1];
@@ -1933,7 +1934,9 @@
assert( pCnt->flags & MEM_Int );
nField = pCnt->i;
pCrsr = 0;
- }else if( (pC = p->apCsr[p1])->pCursor!=0 ){
+ }else
+#endif
+ if( (pC = p->apCsr[p1])->pCursor!=0 ){
/* The record is stored in a B-Tree */
rc = sqlite3VdbeCursorMoveto(pC);
if( rc ) goto abort_due_to_error;
@@ -1952,7 +1955,6 @@
sqlite3BtreeDataSize(pCrsr, &payloadSize);
}
nField = pC->nField;
-#ifndef SQLITE_OMIT_TRIGGER
}else if( pC->pseudoTable ){
/* The record is the sole entry of a pseudo-table */
payloadSize = pC->nData;
@@ -1961,7 +1963,6 @@
assert( payloadSize==0 || zRec!=0 );
nField = pC->nField;
pCrsr = 0;
-#endif
}else{
zRec = 0;
payloadSize = 0;
@@ -2111,7 +2112,7 @@
/* If we dynamically allocated space to hold the data (in the
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
- ** dynamically allocated space over to the pTos structure rather.
+ ** dynamically allocated space over to the pTos structure.
** This prevents a memory copy.
*/
if( (sMem.flags & MEM_Dyn)!=0 ){
@@ -2722,7 +2723,6 @@
break;
}
-#ifndef SQLITE_OMIT_TRIGGER
/* Opcode: OpenPseudo P1 * *
**
** Open a new cursor that points to a fake table that contains a single
@@ -2731,7 +2731,9 @@
** closed.
**
** A pseudo-table created by this opcode is useful for holding the
-** NEW or OLD tables in a trigger.
+** NEW or OLD tables in a trigger. Also used to hold the a single
+** row output from the sorter so that the row can be decomposed into
+** individual columns using the OP_Column opcode.
*/
case OP_OpenPseudo: { /* no-push */
int i = pOp->p1;
@@ -2746,7 +2748,6 @@
pCx->isIndex = 0;
break;
}
-#endif
/* Opcode: Close P1 * *
**
@@ -3320,7 +3321,6 @@
}else{
assert( pTos->flags & (MEM_Blob|MEM_Str) );
}
-#ifndef SQLITE_OMIT_TRIGGER
if( pC->pseudoTable ){
sqliteFree(pC->pData);
pC->iKey = iKey;
@@ -3337,11 +3337,8 @@
}
pC->nullRow = 0;
}else{
-#endif
rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, pTos->z, pTos->n);
-#ifndef SQLITE_OMIT_TRIGGER
}
-#endif
pC->rowidIsValid = 0;
pC->deferredMoveto = 0;
@@ -3498,12 +3495,10 @@
}else{
sqlite3BtreeData(pCrsr, 0, n, pTos->z);
}
-#ifndef SQLITE_OMIT_TRIGGER
}else if( pC->pseudoTable ){
pTos->n = pC->nData;
pTos->z = pC->pData;
pTos->flags = MEM_Blob|MEM_Ephem;
-#endif
}else{
pTos->flags = MEM_Null;
}