1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-10 01:02:56 +03:00

Avoid storing redundant fields in sorter records when the sort-key and data have

fields in common (as in "SELECT a FROM t1 ORDER BY 1").

FossilOrigin-Name: 0af62fdbd8e2aab14718ff8bcb5934f05463c176
This commit is contained in:
dan
2016-11-10 20:14:06 +00:00
parent 58282f68d8
commit 257c13fa00
5 changed files with 54 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
C Fix\stypo\sin\sthe\sCSV\sextension. C Avoid\sstoring\sredundant\sfields\sin\ssorter\srecords\swhen\sthe\ssort-key\sand\sdata\shave\nfields\sin\scommon\s(as\sin\s"SELECT\sa\sFROM\st1\sORDER\sBY\s1").
D 2016-11-09T01:46:13.326 D 2016-11-10T20:14:06.787
F Makefile.in 6fd48ffcf7c2deea7499062d1f3747f986c19678 F Makefile.in 6fd48ffcf7c2deea7499062d1f3747f986c19678
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc e0217f2d35a0448abbe4b066132ae20136e8b408 F Makefile.msc e0217f2d35a0448abbe4b066132ae20136e8b408
@@ -340,7 +340,7 @@ F src/ctime.c a2a52d6e353f459d8ab0f07321f60fafa47d5421
F src/date.c 95c9a8d00767e7221a8e9a31f4e913fc8029bf6b F src/date.c 95c9a8d00767e7221a8e9a31f4e913fc8029bf6b
F src/dbstat.c 19ee7a4e89979d4df8e44cfac7a8f905ec89b77d F src/dbstat.c 19ee7a4e89979d4df8e44cfac7a8f905ec89b77d
F src/delete.c cb3f6300df24c26c609778b2731f82644b5532ec F src/delete.c cb3f6300df24c26c609778b2731f82644b5532ec
F src/expr.c ce7110980fac6dfdfbe1e393443bdb79bad29339 F src/expr.c 1fd53148e575de810ad10a9eb15f16f2e44ff1dc
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
F src/fkey.c b9ca262f6ad4d030a3cab737ebf9b0b3c8b4ac80 F src/fkey.c b9ca262f6ad4d030a3cab737ebf9b0b3c8b4ac80
F src/func.c 7057bc2c105b82faa668d8e2ec85fad4540e5c51 F src/func.c 7057bc2c105b82faa668d8e2ec85fad4540e5c51
@@ -387,12 +387,12 @@ F src/printf.c a5f0ca08ddede803c241266abb46356ec748ded1
F src/random.c ba2679f80ec82c4190062d756f22d0c358180696 F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
F src/resolve.c 3fac1b2737ea5a724f20b921ac7e259c9be2100b F src/resolve.c 3fac1b2737ea5a724f20b921ac7e259c9be2100b
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
F src/select.c ea3af83e2d0f245fef81ea4cf04cb730ce67f722 F src/select.c 64b2273747c6485638bfeb6790e3e774e0605d02
F src/shell.c 63e54cfa1c7ec5b70a4c9a86502bc10280c3d5a3 F src/shell.c 63e54cfa1c7ec5b70a4c9a86502bc10280c3d5a3
F src/sqlite.h.in 803f7050f69b2eea573fac219f3c92582c096027 F src/sqlite.h.in 803f7050f69b2eea573fac219f3c92582c096027
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 8648034aa702469afb553231677306cc6492a1ae F src/sqlite3ext.h 8648034aa702469afb553231677306cc6492a1ae
F src/sqliteInt.h 37628fe30c464dc790bcee3bfd3d0caa8f222ed1 F src/sqliteInt.h 603953faca895386d4f3a8b7046f3e4e6c071c53
F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247 F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247
F src/status.c a9e66593dfb28a9e746cba7153f84d49c1ddc4b1 F src/status.c a9e66593dfb28a9e746cba7153f84d49c1ddc4b1
F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9 F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9
@@ -1530,7 +1530,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 754ee844958bdc3b96acfd1f5395da5796e54a82 P b4889588246c33374ff3758e21ccc4ce246380b6
R eb905f0421ecb200099f22c1d4910f11 R e8a5368c67459f65dc206ad3df15a284
U mistachkin T *branch * sorter-opt
Z a09941219c710c6bfaf84cdaf6c10994 T *sym-sorter-opt *
T -sym-trunk *
U dan
Z 220ba0a8da96dcdf18555b62136e6fa1

View File

@@ -1 +1 @@
b4889588246c33374ff3758e21ccc4ce246380b6 0af62fdbd8e2aab14718ff8bcb5934f05463c176

View File

@@ -4086,8 +4086,13 @@ int sqlite3ExprCodeExprList(
if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
for(pItem=pList->a, i=0; i<n; i++, pItem++){ for(pItem=pList->a, i=0; i<n; i++, pItem++){
Expr *pExpr = pItem->pExpr; Expr *pExpr = pItem->pExpr;
if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); if( flags & SQLITE_ECEL_OMITREF ){
i--;
n--;
}else{
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
}
}else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
}else{ }else{

View File

@@ -533,11 +533,11 @@ static void pushOntoSorter(
iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
pSort->labelDone = sqlite3VdbeMakeLabel(v); pSort->labelDone = sqlite3VdbeMakeLabel(v);
sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
SQLITE_ECEL_DUP|SQLITE_ECEL_REF); SQLITE_ECEL_DUP);
if( bSeq ){ if( bSeq ){
sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
} }
if( nPrefixReg==0 ){ if( nPrefixReg==0 && nData>0 ){
sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
} }
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
@@ -666,7 +666,7 @@ static void codeDistinct(
** If srcTab is negative, then the pEList expressions ** If srcTab is negative, then the pEList expressions
** are evaluated in order to get the data for this row. If srcTab is ** are evaluated in order to get the data for this row. If srcTab is
** zero or more, then data is pulled from srcTab and pEList is used only ** zero or more, then data is pulled from srcTab and pEList is used only
** to get number columns and the datatype for each column. ** to get the number of columns and the collation sequence for each column.
*/ */
static void selectInnerLoop( static void selectInnerLoop(
Parse *pParse, /* The parser context */ Parse *pParse, /* The parser context */
@@ -734,7 +734,25 @@ static void selectInnerLoop(
}else{ }else{
ecelFlags = 0; ecelFlags = 0;
} }
sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags); if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
/* For each expression in pEList that is a copy of an expression in
** the ORDER BY clause (pSort->pOrderBy), set the associated
** iOrderByCol value to one more than the index of the ORDER BY
** expression within the sort-key that pushOntoSorter() will generate.
** This allows the pEList field to be omitted from the sorted record,
** saving space and CPU cycles. */
ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
int j;
if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
}
}
assert( eDest==SRT_Set || eDest==SRT_Mem
|| eDest==SRT_Coroutine || eDest==SRT_Output );
}
nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags);
} }
/* If the DISTINCT keyword was present on the SELECT statement /* If the DISTINCT keyword was present on the SELECT statement
@@ -900,11 +918,12 @@ static void selectInnerLoop(
** memory cells and break out of the scan loop. ** memory cells and break out of the scan loop.
*/ */
case SRT_Mem: { case SRT_Mem: {
assert( nResultCol==pDest->nSdst );
if( pSort ){ if( pSort ){
assert( nResultCol<=pDest->nSdst );
pushOntoSorter( pushOntoSorter(
pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg); pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg);
}else{ }else{
assert( nResultCol==pDest->nSdst );
assert( regResult==iParm ); assert( regResult==iParm );
/* The LIMIT clause will jump out of the loop for us */ /* The LIMIT clause will jump out of the loop for us */
} }
@@ -1202,14 +1221,13 @@ static void generateSortTail(
int iParm = pDest->iSDParm; int iParm = pDest->iSDParm;
int regRow; int regRow;
int regRowid; int regRowid;
int iCol;
int nKey; int nKey;
int iSortTab; /* Sorter cursor to read from */ int iSortTab; /* Sorter cursor to read from */
int nSortData; /* Trailing values to read from sorter */ int nSortData; /* Trailing values to read from sorter */
int i; int i;
int bSeq; /* True if sorter record includes seq. no. */ int bSeq; /* True if sorter record includes seq. no. */
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
struct ExprList_item *aOutEx = p->pEList->a; struct ExprList_item *aOutEx = p->pEList->a;
#endif
assert( addrBreak<0 ); assert( addrBreak<0 );
if( pSort->labelBkOut ){ if( pSort->labelBkOut ){
@@ -1247,8 +1265,14 @@ static void generateSortTail(
iSortTab = iTab; iSortTab = iTab;
bSeq = 1; bSeq = 1;
} }
for(i=0; i<nSortData; i++){ for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); int iRead;
if( aOutEx[i].u.x.iOrderByCol ){
iRead = aOutEx[i].u.x.iOrderByCol-1;
}else{
iRead = iCol++;
}
sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
} }
switch( eDest ){ switch( eDest ){

View File

@@ -3706,6 +3706,7 @@ int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */ #define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
void sqlite3ExprIfTrue(Parse*, Expr*, int, int); void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
void sqlite3ExprIfFalse(Parse*, Expr*, int, int); void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);