Fossil SCM

Import the latest SQLite core from upstream.

drh 2012-10-03 14:58 trunk
Commit 7f3379f3a9926e20551803e57d955bf02c8b0bd0
2 files changed +408 -302 +13 -1
+408 -302
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673673
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674674
** [sqlite_version()] and [sqlite_source_id()].
675675
*/
676676
#define SQLITE_VERSION "3.7.15"
677677
#define SQLITE_VERSION_NUMBER 3007015
678
-#define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
678
+#define SQLITE_SOURCE_ID "2012-10-03 12:56:18 956e4d7f8958e7065ff2d61cd71519d6f4113d4a"
679679
680680
/*
681681
** CAPI3REF: Run-Time Library Version Numbers
682682
** KEYWORDS: sqlite3_version, sqlite3_sourceid
683683
**
@@ -1421,10 +1421,21 @@
14211421
** that the VFS encountered an error while handling the [PRAGMA] and the
14221422
** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
14231423
** file control occurs at the beginning of pragma statement analysis and so
14241424
** it is able to override built-in [PRAGMA] statements.
14251425
** </ul>
1426
+**
1427
+** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1428
+** ^This file-control may be invoked by SQLite on the database file handle
1429
+** shortly after it is opened in order to provide a custom VFS with access
1430
+** to the connections busy-handler callback. The argument is of type (void **)
1431
+** - an array of two (void *) values. The first (void *) actually points
1432
+** to a function of type (int (*)(void *)). In order to invoke the connections
1433
+** busy-handler, this function should be invoked with the second (void *) in
1434
+** the array as the only argument. If it returns non-zero, then the operation
1435
+** should be retried. If it returns zero, the custom VFS should abandon the
1436
+** current operation.
14261437
*/
14271438
#define SQLITE_FCNTL_LOCKSTATE 1
14281439
#define SQLITE_GET_LOCKPROXYFILE 2
14291440
#define SQLITE_SET_LOCKPROXYFILE 3
14301441
#define SQLITE_LAST_ERRNO 4
@@ -1436,10 +1447,11 @@
14361447
#define SQLITE_FCNTL_PERSIST_WAL 10
14371448
#define SQLITE_FCNTL_OVERWRITE 11
14381449
#define SQLITE_FCNTL_VFSNAME 12
14391450
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
14401451
#define SQLITE_FCNTL_PRAGMA 14
1452
+#define SQLITE_FCNTL_BUSYHANDLER 15
14411453
14421454
/*
14431455
** CAPI3REF: Mutex Handle
14441456
**
14451457
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -8381,10 +8393,13 @@
83818393
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
83828394
SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
83838395
SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
83848396
SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
83858397
SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
8398
+#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
8399
+SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
8400
+#endif
83868401
SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
83878402
SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
83888403
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
83898404
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
83908405
SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
@@ -9150,10 +9165,11 @@
91509165
SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
91519166
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
91529167
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
91539168
SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
91549169
SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
9170
+SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
91559171
91569172
/* Functions used to truncate the database file. */
91579173
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
91589174
91599175
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -25825,10 +25841,12 @@
2582525841
int prior = 0;
2582625842
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2582725843
i64 newOffset;
2582825844
#endif
2582925845
TIMER_START;
25846
+ assert( cnt==(cnt&0x1ffff) );
25847
+ cnt &= 0x1ffff;
2583025848
do{
2583125849
#if defined(USE_PREAD)
2583225850
got = osPread(id->h, pBuf, cnt, offset);
2583325851
SimulateIOError( got = -1 );
2583425852
#elif defined(USE_PREAD64)
@@ -25914,10 +25932,12 @@
2591425932
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
2591525933
int got;
2591625934
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2591725935
i64 newOffset;
2591825936
#endif
25937
+ assert( cnt==(cnt&0x1ffff) );
25938
+ cnt &= 0x1ffff;
2591925939
TIMER_START;
2592025940
#if defined(USE_PREAD)
2592125941
do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
2592225942
#elif defined(USE_PREAD64)
2592325943
do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
@@ -39558,10 +39578,25 @@
3955839578
}
3955939579
}
3956039580
}
3956139581
return rc;
3956239582
}
39583
+
39584
+/*
39585
+** Return a sanitized version of the sector-size of OS file pFile. The
39586
+** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
39587
+*/
39588
+SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
39589
+ int iRet = sqlite3OsSectorSize(pFile);
39590
+ if( iRet<32 ){
39591
+ iRet = 512;
39592
+ }else if( iRet>MAX_SECTOR_SIZE ){
39593
+ assert( MAX_SECTOR_SIZE>=512 );
39594
+ iRet = MAX_SECTOR_SIZE;
39595
+ }
39596
+ return iRet;
39597
+}
3956339598
3956439599
/*
3956539600
** Set the value of the Pager.sectorSize variable for the given
3956639601
** pager based on the value returned by the xSectorSize method
3956739602
** of the open database file. The sector size will be used used
@@ -39594,18 +39629,11 @@
3959439629
/* Sector size doesn't matter for temporary files. Also, the file
3959539630
** may not have been opened yet, in which case the OsSectorSize()
3959639631
** call will segfault. */
3959739632
pPager->sectorSize = 512;
3959839633
}else{
39599
- pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
39600
- if( pPager->sectorSize<32 ){
39601
- pPager->sectorSize = 512;
39602
- }
39603
- if( pPager->sectorSize>MAX_SECTOR_SIZE ){
39604
- assert( MAX_SECTOR_SIZE>=512 );
39605
- pPager->sectorSize = MAX_SECTOR_SIZE;
39606
- }
39634
+ pPager->sectorSize = sqlite3SectorSize(pPager->fd);
3960739635
}
3960839636
}
3960939637
3961039638
/*
3961139639
** Playback the journal and thus restore the database file to
@@ -40518,13 +40546,20 @@
4051840546
*/
4051940547
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
4052040548
Pager *pPager, /* Pager object */
4052140549
int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
4052240550
void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
40523
-){
40551
+){
4052440552
pPager->xBusyHandler = xBusyHandler;
4052540553
pPager->pBusyHandlerArg = pBusyHandlerArg;
40554
+
40555
+ if( isOpen(pPager->fd) ){
40556
+ void **ap = (void **)&pPager->xBusyHandler;
40557
+ assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
40558
+ assert( ap[1]==pBusyHandlerArg );
40559
+ sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
40560
+ }
4052640561
}
4052740562
4052840563
/*
4052940564
** Change the page size used by the Pager object. The new page size
4053040565
** is passed in *pPageSize.
@@ -46815,11 +46850,11 @@
4681546850
** sector boundary is synced; the part of the last frame that extends
4681646851
** past the sector boundary is written after the sync.
4681746852
*/
4681846853
if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
4681946854
if( pWal->padToSectorBoundary ){
46820
- int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
46855
+ int sectorSize = sqlite3SectorSize(pWal->pWalFd);
4682146856
w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
4682246857
while( iOffset<w.iSyncPoint ){
4682346858
rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
4682446859
if( rc ) return rc;
4682546860
iOffset += szFrame;
@@ -50227,10 +50262,28 @@
5022750262
** Return the currently defined page size
5022850263
*/
5022950264
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
5023050265
return p->pBt->pageSize;
5023150266
}
50267
+
50268
+#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
50269
+/*
50270
+** This function is similar to sqlite3BtreeGetReserve(), except that it
50271
+** may only be called if it is guaranteed that the b-tree mutex is already
50272
+** held.
50273
+**
50274
+** This is useful in one special case in the backup API code where it is
50275
+** known that the shared b-tree mutex is held, but the mutex on the
50276
+** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
50277
+** were to be called, it might collide with some other operation on the
50278
+** database handle that owns *p, causing undefined behaviour.
50279
+*/
50280
+SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
50281
+ assert( sqlite3_mutex_held(p->pBt->mutex) );
50282
+ return p->pBt->pageSize - p->pBt->usableSize;
50283
+}
50284
+#endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
5023250285
5023350286
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
5023450287
/*
5023550288
** Return the number of bytes of space at the end of every page that
5023650289
** are intentually left unused. This is the "reserved" space that is
@@ -53284,11 +53337,11 @@
5328453337
btreeParseCellPtr(pPage, pCell, &info);
5328553338
if( info.iOverflow==0 ){
5328653339
return SQLITE_OK; /* No overflow pages. Return without doing anything */
5328753340
}
5328853341
if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53289
- return SQLITE_CORRUPT; /* Cell extends past end of page */
53342
+ return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
5329053343
}
5329153344
ovflPgno = get4byte(&pCell[info.iOverflow]);
5329253345
assert( pBt->usableSize > 4 );
5329353346
ovflPageSize = pBt->usableSize - 4;
5329453347
nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
@@ -53950,10 +54003,13 @@
5395054003
** enough for all overflow cells.
5395154004
**
5395254005
** If aOvflSpace is set to a null pointer, this function returns
5395354006
** SQLITE_NOMEM.
5395454007
*/
54008
+#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
54009
+#pragma optimize("", off)
54010
+#endif
5395554011
static int balance_nonroot(
5395654012
MemPage *pParent, /* Parent page of siblings being balanced */
5395754013
int iParentIdx, /* Index of "the page" in pParent */
5395854014
u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
5395954015
int isRoot, /* True if pParent is a root-page */
@@ -54580,10 +54636,13 @@
5458054636
releasePage(apNew[i]);
5458154637
}
5458254638
5458354639
return rc;
5458454640
}
54641
+#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
54642
+#pragma optimize("", on)
54643
+#endif
5458554644
5458654645
5458754646
/*
5458854647
** This function is called when the root page of a b-tree structure is
5458954648
** overfull (has one or more overflow pages).
@@ -56558,17 +56617,20 @@
5655856617
const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
5655956618
int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
5656056619
const int nCopy = MIN(nSrcPgsz, nDestPgsz);
5656156620
const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
5656256621
#ifdef SQLITE_HAS_CODEC
56563
- int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
56622
+ /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
56623
+ ** guaranteed that the shared-mutex is held by this thread, handle
56624
+ ** p->pSrc may not actually be the owner. */
56625
+ int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
5656456626
int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
5656556627
#endif
56566
-
5656756628
int rc = SQLITE_OK;
5656856629
i64 iOff;
5656956630
56631
+ assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
5657056632
assert( p->bDestLocked );
5657156633
assert( !isFatalError(p->rc) );
5657256634
assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
5657356635
assert( zSrcData );
5657456636
@@ -91600,10 +91662,11 @@
9160091662
*/
9160191663
aFcntl[0] = 0;
9160291664
aFcntl[1] = zLeft;
9160391665
aFcntl[2] = zRight;
9160491666
aFcntl[3] = 0;
91667
+ db->busyHandler.nBusy = 0;
9160591668
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
9160691669
if( rc==SQLITE_OK ){
9160791670
if( aFcntl[0] ){
9160891671
int mem = ++pParse->nMem;
9160991672
sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
@@ -102123,14 +102186,15 @@
102123102186
#define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
102124102187
#define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
102125102188
#define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
102126102189
#define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
102127102190
#define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
102128
-#define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
102129
-#define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
102130
-#define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
102131
-#define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
102191
+#define WHERE_IDX_ONLY 0x00400000 /* Use index only - omit table */
102192
+#define WHERE_ORDERED 0x00800000 /* Output will appear in correct order */
102193
+#define WHERE_REVERSE 0x01000000 /* Scan in reverse order */
102194
+#define WHERE_UNIQUE 0x02000000 /* Selects no more than one row */
102195
+#define WHERE_ALL_UNIQUE 0x04000000 /* This and all prior have one row */
102132102196
#define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102133102197
#define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102134102198
#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102135102199
#define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102136102200
#define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
@@ -102154,10 +102218,21 @@
102154102218
sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
102155102219
int i, n; /* Which loop is being coded; # of loops */
102156102220
WhereLevel *aLevel; /* Info about outer loops */
102157102221
WhereCost cost; /* Lowest cost query plan */
102158102222
};
102223
+
102224
+/*
102225
+** Return TRUE if the probe cost is less than the baseline cost
102226
+*/
102227
+static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
102228
+ if( pProbe->rCost<pBaseline->rCost ) return 1;
102229
+ if( pProbe->rCost>pBaseline->rCost ) return 0;
102230
+ if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
102231
+ if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
102232
+ return 0;
102233
+}
102159102234
102160102235
/*
102161102236
** Initialize a preallocated WhereClause structure.
102162102237
*/
102163102238
static void whereClauseInit(
@@ -103306,11 +103381,12 @@
103306103381
Table *pTab = pIdx->pTable;
103307103382
int i;
103308103383
if( pIdx->onError==OE_None ) return 0;
103309103384
for(i=nSkip; i<pIdx->nColumn; i++){
103310103385
int j = pIdx->aiColumn[i];
103311
- if( j>=0 && pTab->aCol[j].notNull==0 ) return 0;
103386
+ assert( j>=0 && j<pTab->nCol );
103387
+ if( pTab->aCol[j].notNull==0 ) return 0;
103312103388
}
103313103389
return 1;
103314103390
}
103315103391
103316103392
/*
@@ -103370,11 +103446,12 @@
103370103446
int nEqCol /* Number of index columns with == */
103371103447
){
103372103448
Bitmask mask = 0; /* Mask of unaccounted for pDistinct exprs */
103373103449
int i; /* Iterator variable */
103374103450
103375
- if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
103451
+ assert( pDistinct!=0 );
103452
+ if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
103376103453
testcase( pDistinct->nExpr==BMS-1 );
103377103454
103378103455
/* Loop through all the expressions in the distinct list. If any of them
103379103456
** are not simple column references, return early. Otherwise, test if the
103380103457
** WHERE clause contains a "col=X" clause. If it does, the expression
@@ -103472,170 +103549,10 @@
103472103549
}
103473103550
103474103551
return 0;
103475103552
}
103476103553
103477
-/*
103478
-** This routine decides if pIdx can be used to satisfy the ORDER BY
103479
-** clause, either in whole or in part. The return value is the
103480
-** cumulative number of terms in the ORDER BY clause that are satisfied
103481
-** by the index pIdx and other indices in outer loops.
103482
-**
103483
-** The table being queried has a cursor number of "base". pIdx is the
103484
-** index that is postulated for use to access the table.
103485
-**
103486
-** nEqCol is the number of columns of pIdx that are used as equality
103487
-** constraints and where the other side of the == is an ordered column
103488
-** or constant. An "order column" in the previous sentence means a column
103489
-** in table from an outer loop whose values will always appear in the
103490
-** correct order due to othre index, or because the outer loop generates
103491
-** a unique result. Any of the first nEqCol columns of pIdx may be missing
103492
-** from the ORDER BY clause and the match can still be a success.
103493
-**
103494
-** The *pbRev value is set to 0 order 1 depending on whether or not
103495
-** pIdx should be run in the forward order or in reverse order.
103496
-*/
103497
-static int isSortingIndex(
103498
- WhereBestIdx *p, /* Best index search context */
103499
- Index *pIdx, /* The index we are testing */
103500
- int base, /* Cursor number for the table to be sorted */
103501
- int nEqCol, /* Number of index columns with ordered == constraints */
103502
- int wsFlags, /* Index usages flags */
103503
- int bOuterRev, /* True if outer loops scan in reverse order */
103504
- int *pbRev /* Set to 1 for reverse-order scan of pIdx */
103505
-){
103506
- int i; /* Number of pIdx terms used */
103507
- int j; /* Number of ORDER BY terms satisfied */
103508
- int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103509
- int nTerm; /* Number of ORDER BY terms */
103510
- struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103511
- ExprList *pOrderBy; /* The ORDER BY clause */
103512
- Parse *pParse = p->pParse; /* Parser context */
103513
- sqlite3 *db = pParse->db; /* Database connection */
103514
- int nPriorSat; /* ORDER BY terms satisfied by outer loops */
103515
- int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
103516
- int nEqOneRow; /* Idx columns that ref unique values */
103517
-
103518
- if( p->i==0 ){
103519
- nPriorSat = 0;
103520
- nEqOneRow = nEqCol;
103521
- }else{
103522
- if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
103523
- nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
103524
- sortOrder = bOuterRev;
103525
- nEqOneRow = 0;
103526
- }
103527
- if( p->i>0 && nEqCol==0 /*&& !allOuterLoopsUnique(p)*/ ) return nPriorSat;
103528
- pOrderBy = p->pOrderBy;
103529
- if( !pOrderBy ) return nPriorSat;
103530
- if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
103531
- if( pIdx->bUnordered ) return nPriorSat;
103532
- nTerm = pOrderBy->nExpr;
103533
- assert( nTerm>0 );
103534
-
103535
- /* Argument pIdx must either point to a 'real' named index structure,
103536
- ** or an index structure allocated on the stack by bestBtreeIndex() to
103537
- ** represent the rowid index that is part of every table. */
103538
- assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
103539
-
103540
- /* Match terms of the ORDER BY clause against columns of
103541
- ** the index.
103542
- **
103543
- ** Note that indices have pIdx->nColumn regular columns plus
103544
- ** one additional column containing the rowid. The rowid column
103545
- ** of the index is also allowed to match against the ORDER BY
103546
- ** clause.
103547
- */
103548
- for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
103549
- Expr *pExpr; /* The expression of the ORDER BY pTerm */
103550
- CollSeq *pColl; /* The collating sequence of pExpr */
103551
- int termSortOrder; /* Sort order for this term */
103552
- int iColumn; /* The i-th column of the index. -1 for rowid */
103553
- int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
103554
- const char *zColl; /* Name of the collating sequence for i-th index term */
103555
-
103556
- pExpr = pTerm->pExpr;
103557
- if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
103558
- /* Can not use an index sort on anything that is not a column in the
103559
- ** left-most table of the FROM clause */
103560
- break;
103561
- }
103562
- pColl = sqlite3ExprCollSeq(pParse, pExpr);
103563
- if( !pColl ){
103564
- pColl = db->pDfltColl;
103565
- }
103566
- if( pIdx->zName && i<pIdx->nColumn ){
103567
- iColumn = pIdx->aiColumn[i];
103568
- if( iColumn==pIdx->pTable->iPKey ){
103569
- iColumn = -1;
103570
- }
103571
- iSortOrder = pIdx->aSortOrder[i];
103572
- zColl = pIdx->azColl[i];
103573
- }else{
103574
- iColumn = -1;
103575
- iSortOrder = 0;
103576
- zColl = pColl->zName;
103577
- }
103578
- if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
103579
- /* Term j of the ORDER BY clause does not match column i of the index */
103580
- if( i<nEqCol ){
103581
- /* If an index column that is constrained by == fails to match an
103582
- ** ORDER BY term, that is OK. Just ignore that column of the index
103583
- */
103584
- continue;
103585
- }else if( i==pIdx->nColumn ){
103586
- /* Index column i is the rowid. All other terms match. */
103587
- break;
103588
- }else{
103589
- /* If an index column fails to match and is not constrained by ==
103590
- ** then the index cannot satisfy the ORDER BY constraint.
103591
- */
103592
- return nPriorSat;
103593
- }
103594
- }
103595
- assert( pIdx->aSortOrder!=0 || iColumn==-1 );
103596
- assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
103597
- assert( iSortOrder==0 || iSortOrder==1 );
103598
- termSortOrder = iSortOrder ^ pTerm->sortOrder;
103599
- if( i>nEqOneRow ){
103600
- if( termSortOrder!=sortOrder ){
103601
- /* Indices can only be used if all ORDER BY terms past the
103602
- ** equality constraints are all either DESC or ASC. */
103603
- break;
103604
- }
103605
- }else{
103606
- sortOrder = termSortOrder;
103607
- }
103608
- j++;
103609
- pTerm++;
103610
- if( iColumn<0 ){
103611
- seenRowid = 1;
103612
- break;
103613
- }
103614
- }
103615
- *pbRev = sortOrder;
103616
-
103617
- /* If there was an "ORDER BY rowid" term that matched, or it is only
103618
- ** possible for a single row from this table to match, then skip over
103619
- ** any additional ORDER BY terms dealing with this table.
103620
- */
103621
- if( seenRowid ||
103622
- ( (wsFlags & WHERE_COLUMN_NULL)==0
103623
- && i>=pIdx->nColumn
103624
- && indexIsUniqueNotNull(pIdx, nEqCol)
103625
- )
103626
- ){
103627
- /* Advance j over additional ORDER BY terms associated with base */
103628
- WhereMaskSet *pMS = p->pWC->pMaskSet;
103629
- Bitmask m = ~getMask(pMS, base);
103630
- while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
103631
- j++;
103632
- }
103633
- }
103634
- return j;
103635
-}
103636
-
103637103554
/*
103638103555
** Prepare a crude estimate of the logarithm of the input value.
103639103556
** The results need not be exact. This is only used for estimating
103640103557
** the total cost of performing operations with O(logN) or O(NlogN)
103641103558
** complexity. Because N is just a guess, it is no great tragedy if
@@ -103785,10 +103702,11 @@
103785103702
WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103786103703
if( rTotal<p->cost.rCost ){
103787103704
p->cost.rCost = rTotal;
103788103705
p->cost.used = used;
103789103706
p->cost.plan.nRow = nRow;
103707
+ p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
103790103708
p->cost.plan.wsFlags = flags;
103791103709
p->cost.plan.u.pTerm = pTerm;
103792103710
}
103793103711
}
103794103712
}
@@ -104327,11 +104245,14 @@
104327104245
}else{
104328104246
p->cost.rCost = rCost;
104329104247
}
104330104248
p->cost.plan.u.pVtabIdx = pIdxInfo;
104331104249
if( pIdxInfo->orderByConsumed ){
104332
- p->cost.plan.wsFlags |= WHERE_ORDERBY;
104250
+ p->cost.plan.wsFlags |= WHERE_ORDERED;
104251
+ p->cost.plan.nOBSat = nOrderBy;
104252
+ }else{
104253
+ p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
104333104254
}
104334104255
p->cost.plan.nEq = 0;
104335104256
pIdxInfo->nOrderBy = nOrderBy;
104336104257
104337104258
/* Try to find a more efficient access pattern by using multiple indexes
@@ -104750,20 +104671,26 @@
104750104671
WhereLevel *pLevel = &p->aLevel[p->i-1];
104751104672
Index *pIdx;
104752104673
u8 sortOrder;
104753104674
for(i=p->i-1; i>=0; i--, pLevel--){
104754104675
if( pLevel->iTabCur!=iTab ) continue;
104755
- if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
104756
- pIdx = pLevel->plan.u.pIdx;
104676
+ if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
104677
+ return 1;
104678
+ }
104679
+ if( (pLevel->plan.wsFlags & WHERE_ORDERED)==0 ){
104680
+ return 0;
104681
+ }
104682
+ if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
104757104683
if( iCol<0 ){
104758104684
sortOrder = 0;
104759104685
testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104760104686
}else{
104761
- for(j=0; j<pIdx->nColumn; j++){
104687
+ int n = pIdx->nColumn;
104688
+ for(j=0; j<n; j++){
104762104689
if( iCol==pIdx->aiColumn[j] ) break;
104763104690
}
104764
- if( j>=pIdx->nColumn ) return 0;
104691
+ if( j>=n ) return 0;
104765104692
sortOrder = pIdx->aSortOrder[j];
104766104693
testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104767104694
}
104768104695
}else{
104769104696
if( iCol!=(-1) ) return 0;
@@ -104792,13 +104719,10 @@
104792104719
static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
104793104720
Expr *pExpr = pTerm->pExpr;
104794104721
assert( pExpr->op==TK_EQ );
104795104722
assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
104796104723
assert( pExpr->pRight!=0 );
104797
- if( p->i==0 ){
104798
- return 1; /* All == are ordered in the outer loop */
104799
- }
104800104724
if( pTerm->prereqRight==0 ){
104801104725
return 1; /* RHS of the == is a constant */
104802104726
}
104803104727
if( pExpr->pRight->op==TK_COLUMN
104804104728
&& isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
@@ -104808,10 +104732,177 @@
104808104732
104809104733
/* If we cannot prove that the constraint is ordered, assume it is not */
104810104734
return 0;
104811104735
}
104812104736
104737
+/*
104738
+** This routine decides if pIdx can be used to satisfy the ORDER BY
104739
+** clause, either in whole or in part. The return value is the
104740
+** cumulative number of terms in the ORDER BY clause that are satisfied
104741
+** by the index pIdx and other indices in outer loops.
104742
+**
104743
+** The table being queried has a cursor number of "base". pIdx is the
104744
+** index that is postulated for use to access the table.
104745
+**
104746
+** nEqCol is the number of columns of pIdx that are used as equality
104747
+** constraints and where the other side of the == is an ordered column
104748
+** or constant. An "order column" in the previous sentence means a column
104749
+** in table from an outer loop whose values will always appear in the
104750
+** correct order due to othre index, or because the outer loop generates
104751
+** a unique result. Any of the first nEqCol columns of pIdx may be missing
104752
+** from the ORDER BY clause and the match can still be a success.
104753
+**
104754
+** The *pbRev value is set to 0 order 1 depending on whether or not
104755
+** pIdx should be run in the forward order or in reverse order.
104756
+*/
104757
+static int isSortingIndex(
104758
+ WhereBestIdx *p, /* Best index search context */
104759
+ Index *pIdx, /* The index we are testing */
104760
+ int base, /* Cursor number for the table to be sorted */
104761
+ int nEqCol, /* Number of index columns with ordered == constraints */
104762
+ int wsFlags, /* Index usages flags */
104763
+ int bOuterRev, /* True if outer loops scan in reverse order */
104764
+ int *pbRev /* Set to 1 for reverse-order scan of pIdx */
104765
+){
104766
+ int i; /* Number of pIdx terms used */
104767
+ int j; /* Number of ORDER BY terms satisfied */
104768
+ int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
104769
+ int nTerm; /* Number of ORDER BY terms */
104770
+ struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
104771
+ ExprList *pOrderBy; /* The ORDER BY clause */
104772
+ Parse *pParse = p->pParse; /* Parser context */
104773
+ sqlite3 *db = pParse->db; /* Database connection */
104774
+ int nPriorSat; /* ORDER BY terms satisfied by outer loops */
104775
+ int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
104776
+ int nEqOneRow; /* Idx columns that ref unique values */
104777
+
104778
+ if( p->i==0 ){
104779
+ nPriorSat = 0;
104780
+ }else{
104781
+ nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
104782
+ if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return nPriorSat;
104783
+ }
104784
+ if( nEqCol==0 ){
104785
+ if( p->i && (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
104786
+ return nPriorSat;
104787
+ }
104788
+ nEqOneRow = 0;
104789
+ }else if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
104790
+ nEqOneRow = nEqCol;
104791
+ }else{
104792
+ sortOrder = bOuterRev;
104793
+ nEqOneRow = -1;
104794
+ }
104795
+ pOrderBy = p->pOrderBy;
104796
+ assert( pOrderBy!=0 );
104797
+ if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
104798
+ if( pIdx->bUnordered ) return nPriorSat;
104799
+ nTerm = pOrderBy->nExpr;
104800
+ assert( nTerm>0 );
104801
+
104802
+ /* Argument pIdx must either point to a 'real' named index structure,
104803
+ ** or an index structure allocated on the stack by bestBtreeIndex() to
104804
+ ** represent the rowid index that is part of every table. */
104805
+ assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
104806
+
104807
+ /* Match terms of the ORDER BY clause against columns of
104808
+ ** the index.
104809
+ **
104810
+ ** Note that indices have pIdx->nColumn regular columns plus
104811
+ ** one additional column containing the rowid. The rowid column
104812
+ ** of the index is also allowed to match against the ORDER BY
104813
+ ** clause.
104814
+ */
104815
+ for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm; i++){
104816
+ Expr *pExpr; /* The expression of the ORDER BY pTerm */
104817
+ CollSeq *pColl; /* The collating sequence of pExpr */
104818
+ int termSortOrder; /* Sort order for this term */
104819
+ int iColumn; /* The i-th column of the index. -1 for rowid */
104820
+ int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
104821
+ const char *zColl; /* Name of the collating sequence for i-th index term */
104822
+
104823
+ assert( i<=pIdx->nColumn );
104824
+ pExpr = pTerm->pExpr;
104825
+ if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
104826
+ /* Can not use an index sort on anything that is not a column in the
104827
+ ** left-most table of the FROM clause */
104828
+ break;
104829
+ }
104830
+ pColl = sqlite3ExprCollSeq(pParse, pExpr);
104831
+ if( !pColl ){
104832
+ pColl = db->pDfltColl;
104833
+ }
104834
+ if( pIdx->zName && i<pIdx->nColumn ){
104835
+ iColumn = pIdx->aiColumn[i];
104836
+ if( iColumn==pIdx->pTable->iPKey ){
104837
+ iColumn = -1;
104838
+ }
104839
+ iSortOrder = pIdx->aSortOrder[i];
104840
+ zColl = pIdx->azColl[i];
104841
+ }else{
104842
+ iColumn = -1;
104843
+ iSortOrder = 0;
104844
+ zColl = pColl->zName;
104845
+ }
104846
+ if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
104847
+ /* Term j of the ORDER BY clause does not match column i of the index */
104848
+ if( i<nEqCol ){
104849
+ /* If an index column that is constrained by == fails to match an
104850
+ ** ORDER BY term, that is OK. Just ignore that column of the index
104851
+ */
104852
+ continue;
104853
+ }else if( i==pIdx->nColumn ){
104854
+ /* Index column i is the rowid. All other terms match. */
104855
+ break;
104856
+ }else{
104857
+ /* If an index column fails to match and is not constrained by ==
104858
+ ** then the index cannot satisfy the ORDER BY constraint.
104859
+ */
104860
+ return nPriorSat;
104861
+ }
104862
+ }
104863
+ assert( pIdx->aSortOrder!=0 || iColumn==-1 );
104864
+ assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
104865
+ assert( iSortOrder==0 || iSortOrder==1 );
104866
+ termSortOrder = iSortOrder ^ pTerm->sortOrder;
104867
+ if( i>nEqOneRow ){
104868
+ if( termSortOrder!=sortOrder ){
104869
+ /* Indices can only be used if all ORDER BY terms past the
104870
+ ** equality constraints have the correct DESC or ASC. */
104871
+ break;
104872
+ }
104873
+ }else{
104874
+ sortOrder = termSortOrder;
104875
+ }
104876
+ j++;
104877
+ pTerm++;
104878
+ if( iColumn<0 ){
104879
+ seenRowid = 1;
104880
+ break;
104881
+ }
104882
+ }
104883
+ *pbRev = sortOrder;
104884
+
104885
+ /* If there was an "ORDER BY rowid" term that matched, or it is only
104886
+ ** possible for a single row from this table to match, then skip over
104887
+ ** any additional ORDER BY terms dealing with this table.
104888
+ */
104889
+ if( seenRowid ||
104890
+ ( (wsFlags & WHERE_COLUMN_NULL)==0
104891
+ && i>=pIdx->nColumn
104892
+ && indexIsUniqueNotNull(pIdx, nEqCol)
104893
+ )
104894
+ ){
104895
+ /* Advance j over additional ORDER BY terms associated with base */
104896
+ WhereMaskSet *pMS = p->pWC->pMaskSet;
104897
+ Bitmask m = ~getMask(pMS, base);
104898
+ while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
104899
+ j++;
104900
+ }
104901
+ }
104902
+ return j;
104903
+}
104813104904
104814104905
/*
104815104906
** Find the best query plan for accessing a particular table. Write the
104816104907
** best query plan and its cost into the p->cost.
104817104908
**
@@ -104902,22 +104993,20 @@
104902104993
104903104994
/* Loop over all indices looking for the best one to use
104904104995
*/
104905104996
for(; pProbe; pIdx=pProbe=pProbe->pNext){
104906104997
const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104907
- double cost; /* Cost of using pProbe */
104908
- double nRow; /* Estimated number of rows in result set */
104998
+ WhereCost pc; /* Cost of using pProbe */
104909104999
double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
104910105000
int bRev = 2; /* 0=forward scan. 1=reverse. 2=undecided */
104911
- int wsFlags = 0;
104912
- Bitmask used = 0;
105001
+ memset(&pc, 0, sizeof(pc));
104913105002
104914105003
/* The following variables are populated based on the properties of
104915105004
** index being evaluated. They are then used to determine the expected
104916105005
** cost and number of rows returned.
104917105006
**
104918
- ** nEq:
105007
+ ** pc.plan.nEq:
104919105008
** Number of equality terms that can be implemented using the index.
104920105009
** In other words, the number of initial fields in the index that
104921105010
** are used in == or IN or NOT NULL constraints of the WHERE clause.
104922105011
**
104923105012
** nInMul:
@@ -104960,11 +105049,11 @@
104960105049
** bSort:
104961105050
** Boolean. True if there is an ORDER BY clause that will require an
104962105051
** external sort (i.e. scanning the index being evaluated will not
104963105052
** correctly order records).
104964105053
**
104965
- ** bDistinct:
105054
+ ** bDist:
104966105055
** Boolean. True if there is a DISTINCT clause that will require an
104967105056
** external btree.
104968105057
**
104969105058
** bLookup:
104970105059
** Boolean. True if a table lookup is required for each index entry
@@ -104979,132 +105068,146 @@
104979105068
** both available in the index.
104980105069
**
104981105070
** SELECT a, b FROM tbl WHERE a = 1;
104982105071
** SELECT a, b, c FROM tbl WHERE a = 1;
104983105072
*/
104984
- int nEq; /* Number of == or IN terms matching index */
104985105073
int nOrdered; /* Number of ordered terms matching index */
104986105074
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
104987105075
int nInMul = 1; /* Number of distinct equalities to lookup */
104988105076
double rangeDiv = (double)1; /* Estimated reduction in search space */
104989105077
int nBound = 0; /* Number of range constraints seen */
104990105078
int bSort; /* True if external sort required */
104991105079
int bDist; /* True if index cannot help with DISTINCT */
104992105080
int bLookup = 0; /* True if not a covering index */
104993
- int nOBSat = 0; /* Number of ORDER BY terms satisfied */
105081
+ int nPriorSat; /* ORDER BY terms satisfied by outer loops */
104994105082
int nOrderBy; /* Number of ORDER BY terms */
104995105083
WhereTerm *pTerm; /* A single term of the WHERE clause */
104996105084
#ifdef SQLITE_ENABLE_STAT3
104997105085
WhereTerm *pFirstTerm = 0; /* First term matching the index */
104998105086
#endif
104999105087
105000105088
nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105001
- bSort = nOrderBy>0 && (p->i==0 || p->aLevel[p->i-1].plan.nOBSat<nOrderBy);
105002
- bDist = p->i==0 && p->pDistinct!=0;
105089
+ if( p->i ){
105090
+ nPriorSat = pc.plan.nOBSat = p->aLevel[p->i-1].plan.nOBSat;
105091
+ bSort = nPriorSat<nOrderBy;
105092
+ bDist = 0;
105093
+ }else{
105094
+ nPriorSat = pc.plan.nOBSat = 0;
105095
+ bSort = nOrderBy>0;
105096
+ bDist = p->pDistinct!=0;
105097
+ }
105003105098
105004
- /* Determine the values of nEq and nInMul */
105005
- for(nEq=nOrdered=0; nEq<pProbe->nColumn; nEq++){
105006
- int j = pProbe->aiColumn[nEq];
105099
+ /* Determine the values of pc.plan.nEq and nInMul */
105100
+ for(pc.plan.nEq=nOrdered=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
105101
+ int j = pProbe->aiColumn[pc.plan.nEq];
105007105102
pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
105008105103
if( pTerm==0 ) break;
105009
- wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
105104
+ pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
105010105105
testcase( pTerm->pWC!=pWC );
105011105106
if( pTerm->eOperator & WO_IN ){
105012105107
Expr *pExpr = pTerm->pExpr;
105013
- wsFlags |= WHERE_COLUMN_IN;
105108
+ pc.plan.wsFlags |= WHERE_COLUMN_IN;
105014105109
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
105015105110
/* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
105016105111
nInMul *= 25;
105017105112
bInEst = 1;
105018105113
}else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
105019105114
/* "x IN (value, value, ...)" */
105020105115
nInMul *= pExpr->x.pList->nExpr;
105021105116
}
105022105117
}else if( pTerm->eOperator & WO_ISNULL ){
105023
- wsFlags |= WHERE_COLUMN_NULL;
105024
- if( nEq==nOrdered ) nOrdered++;
105025
- }else if( bSort && nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105118
+ pc.plan.wsFlags |= WHERE_COLUMN_NULL;
105119
+ if( pc.plan.nEq==nOrdered ) nOrdered++;
105120
+ }else if( bSort && pc.plan.nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105026105121
nOrdered++;
105027105122
}
105028105123
#ifdef SQLITE_ENABLE_STAT3
105029
- if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105124
+ if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105030105125
#endif
105031
- used |= pTerm->prereqRight;
105126
+ pc.used |= pTerm->prereqRight;
105032105127
}
105033105128
105034105129
/* If the index being considered is UNIQUE, and there is an equality
105035105130
** constraint for all columns in the index, then this search will find
105036105131
** at most a single row. In this case set the WHERE_UNIQUE flag to
105037105132
** indicate this to the caller.
105038105133
**
105039105134
** Otherwise, if the search may find more than one row, test to see if
105040
- ** there is a range constraint on indexed column (nEq+1) that can be
105135
+ ** there is a range constraint on indexed column (pc.plan.nEq+1) that can be
105041105136
** optimized using the index.
105042105137
*/
105043
- if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105044
- testcase( wsFlags & WHERE_COLUMN_IN );
105045
- testcase( wsFlags & WHERE_COLUMN_NULL );
105046
- if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105047
- wsFlags |= WHERE_UNIQUE;
105138
+ if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105139
+ testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
105140
+ testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
105141
+ if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105142
+ pc.plan.wsFlags |= WHERE_UNIQUE;
105143
+ if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
105144
+ pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
105145
+ }
105048105146
}
105049105147
}else if( pProbe->bUnordered==0 ){
105050
- int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
105148
+ int j;
105149
+ j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
105051105150
if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105052105151
WhereTerm *pTop, *pBtm;
105053105152
pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
105054105153
pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
105055
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
105154
+ whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
105056105155
if( pTop ){
105057105156
nBound = 1;
105058
- wsFlags |= WHERE_TOP_LIMIT;
105059
- used |= pTop->prereqRight;
105157
+ pc.plan.wsFlags |= WHERE_TOP_LIMIT;
105158
+ pc.used |= pTop->prereqRight;
105060105159
testcase( pTop->pWC!=pWC );
105061105160
}
105062105161
if( pBtm ){
105063105162
nBound++;
105064
- wsFlags |= WHERE_BTM_LIMIT;
105065
- used |= pBtm->prereqRight;
105163
+ pc.plan.wsFlags |= WHERE_BTM_LIMIT;
105164
+ pc.used |= pBtm->prereqRight;
105066105165
testcase( pBtm->pWC!=pWC );
105067105166
}
105068
- wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
105167
+ pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
105069105168
}
105070105169
}
105071105170
105072105171
/* If there is an ORDER BY clause and the index being considered will
105073105172
** naturally scan rows in the required order, set the appropriate flags
105074
- ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
105075
- ** will scan rows in a different order, set the bSort variable. */
105173
+ ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
105174
+ ** the index will scan rows in a different order, set the bSort
105175
+ ** variable. */
105076105176
assert( bRev>=0 && bRev<=2 );
105077105177
if( bSort ){
105078105178
testcase( bRev==0 );
105079105179
testcase( bRev==1 );
105080105180
testcase( bRev==2 );
105081
- nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105082
- wsFlags, bRev&1, &bRev);
105083
- if( nOrderBy==nOBSat ){
105181
+ pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105182
+ pc.plan.wsFlags, bRev&1, &bRev);
105183
+ if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_UNIQUE)!=0 ){
105184
+ pc.plan.wsFlags |= WHERE_ORDERED;
105185
+ }
105186
+ if( nOrderBy==pc.plan.nOBSat ){
105084105187
bSort = 0;
105085
- wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
105188
+ pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
105086105189
}
105087
- if( bRev & 1 ) wsFlags |= WHERE_REVERSE;
105190
+ if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
105088105191
}
105089105192
105090105193
/* If there is a DISTINCT qualifier and this index will scan rows in
105091105194
** order of the DISTINCT expressions, clear bDist and set the appropriate
105092
- ** flags in wsFlags. */
105195
+ ** flags in pc.plan.wsFlags. */
105093105196
if( bDist
105094
- && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, nEq)
105095
- && (wsFlags & WHERE_COLUMN_IN)==0
105197
+ && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
105198
+ && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
105096105199
){
105097105200
bDist = 0;
105098
- wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
105201
+ pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
105099105202
}
105100105203
105101105204
/* If currently calculating the cost of using an index (not the IPK
105102105205
** index), determine if all required column data may be obtained without
105103105206
** using the main table (i.e. if the index is a covering
105104105207
** index for this query). If it is, set the WHERE_IDX_ONLY flag in
105105
- ** wsFlags. Otherwise, set the bLookup variable to true. */
105208
+ ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true. */
105106105209
if( pIdx ){
105107105210
Bitmask m = pSrc->colUsed;
105108105211
int j;
105109105212
for(j=0; j<pIdx->nColumn; j++){
105110105213
int x = pIdx->aiColumn[j];
@@ -105111,51 +105214,54 @@
105111105214
if( x<BMS-1 ){
105112105215
m &= ~(((Bitmask)1)<<x);
105113105216
}
105114105217
}
105115105218
if( m==0 ){
105116
- wsFlags |= WHERE_IDX_ONLY;
105219
+ pc.plan.wsFlags |= WHERE_IDX_ONLY;
105117105220
}else{
105118105221
bLookup = 1;
105119105222
}
105120105223
}
105121105224
105122105225
/*
105123105226
** Estimate the number of rows of output. For an "x IN (SELECT...)"
105124105227
** constraint, do not let the estimate exceed half the rows in the table.
105125105228
*/
105126
- nRow = (double)(aiRowEst[nEq] * nInMul);
105127
- if( bInEst && nRow*2>aiRowEst[0] ){
105128
- nRow = aiRowEst[0]/2;
105129
- nInMul = (int)(nRow / aiRowEst[nEq]);
105229
+ pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
105230
+ if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
105231
+ pc.plan.nRow = aiRowEst[0]/2;
105232
+ nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
105130105233
}
105131105234
105132105235
#ifdef SQLITE_ENABLE_STAT3
105133105236
/* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
105134105237
** and we do not think that values of x are unique and if histogram
105135105238
** data is available for column x, then it might be possible
105136105239
** to get a better estimate on the number of rows based on
105137105240
** VALUE and how common that value is according to the histogram.
105138105241
*/
105139
- if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
105242
+ if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
105243
+ && pFirstTerm!=0 && aiRowEst[1]>1 ){
105140105244
assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
105141105245
if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
105142105246
testcase( pFirstTerm->eOperator==WO_EQ );
105143105247
testcase( pFirstTerm->eOperator==WO_ISNULL );
105144
- whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
105248
+ whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
105249
+ &pc.plan.nRow);
105145105250
}else if( bInEst==0 ){
105146105251
assert( pFirstTerm->eOperator==WO_IN );
105147
- whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
105252
+ whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
105253
+ &pc.plan.nRow);
105148105254
}
105149105255
}
105150105256
#endif /* SQLITE_ENABLE_STAT3 */
105151105257
105152105258
/* Adjust the number of output rows and downward to reflect rows
105153105259
** that are excluded by range constraints.
105154105260
*/
105155
- nRow = nRow/rangeDiv;
105156
- if( nRow<1 ) nRow = 1;
105261
+ pc.plan.nRow = pc.plan.nRow/rangeDiv;
105262
+ if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
105157105263
105158105264
/* Experiments run on real SQLite databases show that the time needed
105159105265
** to do a binary search to locate a row in a table or index is roughly
105160105266
** log10(N) times the time to move from one row to the next row within
105161105267
** a table or index. The actual times can vary, with the size of
@@ -105166,57 +105272,58 @@
105166105272
** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
105167105273
** not give us data on the relative sizes of table and index records.
105168105274
** So this computation assumes table records are about twice as big
105169105275
** as index records
105170105276
*/
105171
- if( (wsFlags&~WHERE_REVERSE)==WHERE_IDX_ONLY
105277
+ if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED))==WHERE_IDX_ONLY
105172105278
&& (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
105173105279
&& sqlite3GlobalConfig.bUseCis
105174105280
&& OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
105175105281
){
105176105282
/* This index is not useful for indexing, but it is a covering index.
105177105283
** A full-scan of the index might be a little faster than a full-scan
105178105284
** of the table, so give this case a cost slightly less than a table
105179105285
** scan. */
105180
- cost = aiRowEst[0]*3 + pProbe->nColumn;
105181
- wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
105182
- }else if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
105286
+ pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
105287
+ pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
105288
+ }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
105183105289
/* The cost of a full table scan is a number of move operations equal
105184105290
** to the number of rows in the table.
105185105291
**
105186105292
** We add an additional 4x penalty to full table scans. This causes
105187105293
** the cost function to err on the side of choosing an index over
105188105294
** choosing a full scan. This 4x full-scan penalty is an arguable
105189105295
** decision and one which we expect to revisit in the future. But
105190105296
** it seems to be working well enough at the moment.
105191105297
*/
105192
- cost = aiRowEst[0]*4;
105193
- wsFlags &= ~WHERE_IDX_ONLY;
105298
+ pc.rCost = aiRowEst[0]*4;
105299
+ pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
105300
+ if( pIdx ) pc.plan.wsFlags &= ~WHERE_ORDERED;
105194105301
}else{
105195105302
log10N = estLog(aiRowEst[0]);
105196
- cost = nRow;
105303
+ pc.rCost = pc.plan.nRow;
105197105304
if( pIdx ){
105198105305
if( bLookup ){
105199105306
/* For an index lookup followed by a table lookup:
105200105307
** nInMul index searches to find the start of each index range
105201105308
** + nRow steps through the index
105202105309
** + nRow table searches to lookup the table entry using the rowid
105203105310
*/
105204
- cost += (nInMul + nRow)*log10N;
105311
+ pc.rCost += (nInMul + pc.plan.nRow)*log10N;
105205105312
}else{
105206105313
/* For a covering index:
105207105314
** nInMul index searches to find the initial entry
105208105315
** + nRow steps through the index
105209105316
*/
105210
- cost += nInMul*log10N;
105317
+ pc.rCost += nInMul*log10N;
105211105318
}
105212105319
}else{
105213105320
/* For a rowid primary key lookup:
105214105321
** nInMult table searches to find the initial entry for each range
105215105322
** + nRow steps through the table
105216105323
*/
105217
- cost += nInMul*log10N;
105324
+ pc.rCost += nInMul*log10N;
105218105325
}
105219105326
}
105220105327
105221105328
/* Add in the estimated cost of sorting the result. Actual experimental
105222105329
** measurements of sorting performance in SQLite show that sorting time
@@ -105223,14 +105330,16 @@
105223105330
** adds C*N*log10(N) to the cost, where N is the number of rows to be
105224105331
** sorted and C is a factor between 1.95 and 4.3. We will split the
105225105332
** difference and select C of 3.0.
105226105333
*/
105227105334
if( bSort ){
105228
- cost += nRow*estLog(nRow*(nOrderBy - nOBSat)/nOrderBy)*3;
105335
+ double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
105336
+ m *= (double)(pc.plan.nOBSat ? 2 : 3);
105337
+ pc.rCost += pc.plan.nRow*m;
105229105338
}
105230105339
if( bDist ){
105231
- cost += nRow*estLog(nRow)*3;
105340
+ pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
105232105341
}
105233105342
105234105343
/**** Cost of using this index has now been computed ****/
105235105344
105236105345
/* If there are additional constraints on this table that cannot
@@ -105247,29 +105356,29 @@
105247105356
** tables that are not in outer loops. If notReady is used here instead
105248105357
** of notValid, then a optimal index that depends on inner joins loops
105249105358
** might be selected even when there exists an optimal index that has
105250105359
** no such dependency.
105251105360
*/
105252
- if( nRow>2 && cost<=p->cost.rCost ){
105361
+ if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
105253105362
int k; /* Loop counter */
105254
- int nSkipEq = nEq; /* Number of == constraints to skip */
105363
+ int nSkipEq = pc.plan.nEq; /* Number of == constraints to skip */
105255105364
int nSkipRange = nBound; /* Number of < constraints to skip */
105256105365
Bitmask thisTab; /* Bitmap for pSrc */
105257105366
105258105367
thisTab = getMask(pWC->pMaskSet, iCur);
105259
- for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
105368
+ for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
105260105369
if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105261105370
if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
105262105371
if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105263105372
if( nSkipEq ){
105264
- /* Ignore the first nEq equality matches since the index
105373
+ /* Ignore the first pc.plan.nEq equality matches since the index
105265105374
** has already accounted for these */
105266105375
nSkipEq--;
105267105376
}else{
105268105377
/* Assume each additional equality match reduces the result
105269105378
** set size by a factor of 10 */
105270
- nRow /= 10;
105379
+ pc.plan.nRow /= 10;
105271105380
}
105272105381
}else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
105273105382
if( nSkipRange ){
105274105383
/* Ignore the first nSkipRange range constraints since the index
105275105384
** has already accounted for these */
@@ -105279,43 +105388,38 @@
105279105388
** set size by a factor of 3. Indexed range constraints reduce
105280105389
** the search space by a larger factor: 4. We make indexed range
105281105390
** more selective intentionally because of the subjective
105282105391
** observation that indexed range constraints really are more
105283105392
** selective in practice, on average. */
105284
- nRow /= 3;
105393
+ pc.plan.nRow /= 3;
105285105394
}
105286105395
}else if( pTerm->eOperator!=WO_NOOP ){
105287105396
/* Any other expression lowers the output row count by half */
105288
- nRow /= 2;
105397
+ pc.plan.nRow /= 2;
105289105398
}
105290105399
}
105291
- if( nRow<2 ) nRow = 2;
105400
+ if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
105292105401
}
105293105402
105294105403
105295105404
WHERETRACE((
105296105405
"%s(%s):\n"
105297105406
" nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
105298105407
" notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
105299105408
" used=0x%llx nOrdered=%d nOBSat=%d\n",
105300105409
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105301
- nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
105302
- p->notReady, log10N, nRow, cost, used, nOrdered, nOBSat
105410
+ pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
105411
+ p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used, nOrdered,
105412
+ pc.plan.nOBSat
105303105413
));
105304105414
105305105415
/* If this index is the best we have seen so far, then record this
105306
- ** index and its cost in the pCost structure.
105416
+ ** index and its cost in the p->cost structure.
105307105417
*/
105308
- if( (!pIdx || wsFlags)
105309
- && (cost<p->cost.rCost || (cost<=p->cost.rCost && nRow<p->cost.plan.nRow))
105310
- ){
105311
- p->cost.rCost = cost;
105312
- p->cost.used = used;
105313
- p->cost.plan.nRow = nRow;
105314
- p->cost.plan.wsFlags = (wsFlags&wsFlagMask);
105315
- p->cost.plan.nEq = nEq;
105316
- p->cost.plan.nOBSat = nOBSat;
105418
+ if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
105419
+ p->cost = pc;
105420
+ p->cost.plan.wsFlags &= wsFlagMask;
105317105421
p->cost.plan.u.pIdx = pIdx;
105318105422
}
105319105423
105320105424
/* If there was an INDEXED BY clause, then only that one index is
105321105425
** considered. */
@@ -105333,21 +105437,19 @@
105333105437
** SQLite outputs rows in in the absence of an ORDER BY clause. */
105334105438
if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105335105439
p->cost.plan.wsFlags |= WHERE_REVERSE;
105336105440
}
105337105441
105338
- assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERBY)==0 );
105442
+ assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
105339105443
assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105340105444
assert( pSrc->pIndex==0
105341105445
|| p->cost.plan.u.pIdx==0
105342105446
|| p->cost.plan.u.pIdx==pSrc->pIndex
105343105447
);
105344105448
105345
- WHERETRACE(("best index is: %s\n",
105346
- ((p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105347
- p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk")
105348
- ));
105449
+ WHERETRACE(("best index is: %s\n",
105450
+ p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
105349105451
105350105452
bestOrClauseIndex(p);
105351105453
bestAutomaticIndex(p);
105352105454
p->cost.plan.wsFlags |= eqTermMask;
105353105455
}
@@ -106071,11 +106173,11 @@
106071106173
** should not have a NULL value stored in 'x'. If column 'x' is
106072106174
** the first one after the nEq equality constraints in the index,
106073106175
** this requires some special handling.
106074106176
*/
106075106177
if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
106076
- && (pLevel->plan.wsFlags&WHERE_ORDERBY)
106178
+ && (pLevel->plan.wsFlags&WHERE_ORDERED)
106077106179
&& (pIdx->nColumn>nEq)
106078106180
){
106079106181
/* assert( pOrderBy->nExpr==1 ); */
106080106182
/* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
106081106183
isMinQuery = 1;
@@ -106892,12 +106994,12 @@
106892106994
continue;
106893106995
}
106894106996
sWBI.notReady = (isOptimal ? m : sWBI.notValid);
106895106997
if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
106896106998
106897
- WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
106898
- j, isOptimal));
106999
+ WHERETRACE(("=== trying table %d (%s) with isOptimal=%d ===\n",
107000
+ j, sWBI.pSrc->pTab->zName, isOptimal));
106899107001
assert( sWBI.pSrc->pTab );
106900107002
#ifndef SQLITE_OMIT_VIRTUALTABLE
106901107003
if( IsVirtual(sWBI.pSrc->pTab) ){
106902107004
sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
106903107005
bestVirtualIndex(&sWBI);
@@ -106934,48 +107036,46 @@
106934107036
** combination of INDEXED BY clauses are given. The error
106935107037
** will be detected and relayed back to the application later.
106936107038
** The NEVER() comes about because rule (2) above prevents
106937107039
** An indexable full-table-scan from reaching rule (3).
106938107040
**
106939
- ** (4) The plan cost must be lower than prior plans or else the
106940
- ** cost must be the same and the number of rows must be lower.
107041
+ ** (4) The plan cost must be lower than prior plans, where "cost"
107042
+ ** is defined by the compareCost() function above.
106941107043
*/
106942107044
if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
106943107045
&& (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106944107046
|| (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106945107047
|| (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106946107048
&& (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
106947107049
|| NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106948
- && (bestJ<0 || sWBI.cost.rCost<bestPlan.rCost /* (4) */
106949
- || (sWBI.cost.rCost<=bestPlan.rCost
106950
- && sWBI.cost.plan.nRow<bestPlan.plan.nRow))
107050
+ && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan)) /* (4) */
106951107051
){
106952
- WHERETRACE(("=== table %d is best so far"
106953
- " with cost=%.1f, nRow=%.1f, nOBSat=%d\n",
106954
- j, sWBI.cost.rCost, sWBI.cost.plan.nRow,
106955
- sWBI.cost.plan.nOBSat));
107052
+ WHERETRACE(("=== table %d (%s) is best so far\n"
107053
+ " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
107054
+ j, sWBI.pSrc->pTab->zName,
107055
+ sWBI.cost.rCost, sWBI.cost.plan.nRow,
107056
+ sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
106956107057
bestPlan = sWBI.cost;
106957107058
bestJ = j;
106958107059
}
106959107060
if( doNotReorder ) break;
106960107061
}
106961107062
}
106962107063
assert( bestJ>=0 );
106963107064
assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106964
- WHERETRACE(("*** Optimizer selects table %d for loop %d with:\n"
106965
- " cost=%.1f, nRow=%.1f, nOBSat=%d wsFlags=0x%08x\n",
106966
- bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
107065
+ WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
107066
+ " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
107067
+ bestJ, pTabList->a[bestJ].pTab->zName,
107068
+ pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
106967107069
bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
106968
- if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
106969
- pWInfo->nOBSat = pOrderBy->nExpr;
106970
- }
106971107070
if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
106972107071
assert( pWInfo->eDistinct==0 );
106973107072
pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
106974107073
}
106975107074
andFlags &= bestPlan.plan.wsFlags;
106976107075
pLevel->plan = bestPlan.plan;
107076
+ pLevel->iTabCur = pTabList->a[bestJ].iCursor;
106977107077
testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
106978107078
testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
106979107079
if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
106980107080
if( (wctrlFlags & WHERE_ONETABLE_ONLY)
106981107081
&& (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0
@@ -107013,15 +107113,22 @@
107013107113
}
107014107114
WHERETRACE(("*** Optimizer Finished ***\n"));
107015107115
if( pParse->nErr || db->mallocFailed ){
107016107116
goto whereBeginError;
107017107117
}
107118
+ if( nTabList ){
107119
+ pLevel--;
107120
+ pWInfo->nOBSat = pLevel->plan.nOBSat;
107121
+ }else{
107122
+ pWInfo->nOBSat = 0;
107123
+ }
107018107124
107019107125
/* If the total query only selects a single row, then the ORDER BY
107020107126
** clause is irrelevant.
107021107127
*/
107022107128
if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
107129
+ assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
107023107130
pWInfo->nOBSat = pOrderBy->nExpr;
107024107131
}
107025107132
107026107133
/* If the caller is an UPDATE or DELETE statement that is requesting
107027107134
** to use a one-pass algorithm, determine if this is appropriate.
@@ -107045,11 +107152,10 @@
107045107152
int iDb; /* Index of database containing table/index */
107046107153
struct SrcList_item *pTabItem;
107047107154
107048107155
pTabItem = &pTabList->a[pLevel->iFrom];
107049107156
pTab = pTabItem->pTab;
107050
- pLevel->iTabCur = pTabItem->iCursor;
107051107157
pWInfo->nRowOut *= pLevel->plan.nRow;
107052107158
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107053107159
if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
107054107160
/* Do nothing */
107055107161
}else
@@ -114978,11 +115084,11 @@
114978115084
** with various optimizations disabled to verify that the same answer
114979115085
** is obtained in every case.
114980115086
*/
114981115087
case SQLITE_TESTCTRL_OPTIMIZATIONS: {
114982115088
sqlite3 *db = va_arg(ap, sqlite3*);
114983
- db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
115089
+ db->dbOptFlags = (u8)(va_arg(ap, int) & 0xff);
114984115090
break;
114985115091
}
114986115092
114987115093
#ifdef SQLITE_N_KEYWORD
114988115094
/* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
@@ -135232,11 +135338,11 @@
135232135338
/*
135233135339
** Remove the entry with rowid=iDelete from the r-tree structure.
135234135340
*/
135235135341
static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
135236135342
int rc; /* Return code */
135237
- RtreeNode *pLeaf; /* Leaf node containing record iDelete */
135343
+ RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
135238135344
int iCell; /* Index of iDelete cell in pLeaf */
135239135345
RtreeNode *pRoot; /* Root node of rtree structure */
135240135346
135241135347
135242135348
/* Obtain a reference to the root node to initialise Rtree.iDepth */
@@ -135435,11 +135541,11 @@
135435135541
** (azData[2]..azData[argc-1]) contain a new record to insert into
135436135542
** the r-tree structure.
135437135543
*/
135438135544
if( rc==SQLITE_OK && nData>1 ){
135439135545
/* Insert the new record into the r-tree */
135440
- RtreeNode *pLeaf;
135546
+ RtreeNode *pLeaf = 0;
135441135547
135442135548
/* Figure out the rowid of the new row. */
135443135549
if( bHaveRowid==0 ){
135444135550
rc = newRowid(pRtree, &cell.iRowid);
135445135551
}
135446135552
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674 ** [sqlite_version()] and [sqlite_source_id()].
675 */
676 #define SQLITE_VERSION "3.7.15"
677 #define SQLITE_VERSION_NUMBER 3007015
678 #define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
679
680 /*
681 ** CAPI3REF: Run-Time Library Version Numbers
682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
683 **
@@ -1421,10 +1421,21 @@
1421 ** that the VFS encountered an error while handling the [PRAGMA] and the
1422 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1423 ** file control occurs at the beginning of pragma statement analysis and so
1424 ** it is able to override built-in [PRAGMA] statements.
1425 ** </ul>
 
 
 
 
 
 
 
 
 
 
 
1426 */
1427 #define SQLITE_FCNTL_LOCKSTATE 1
1428 #define SQLITE_GET_LOCKPROXYFILE 2
1429 #define SQLITE_SET_LOCKPROXYFILE 3
1430 #define SQLITE_LAST_ERRNO 4
@@ -1436,10 +1447,11 @@
1436 #define SQLITE_FCNTL_PERSIST_WAL 10
1437 #define SQLITE_FCNTL_OVERWRITE 11
1438 #define SQLITE_FCNTL_VFSNAME 12
1439 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1440 #define SQLITE_FCNTL_PRAGMA 14
 
1441
1442 /*
1443 ** CAPI3REF: Mutex Handle
1444 **
1445 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -8381,10 +8393,13 @@
8381 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8382 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8383 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
8384 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
8385 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
 
 
 
8386 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
8387 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
8388 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
8389 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
8390 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
@@ -9150,10 +9165,11 @@
9150 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
9151 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
9152 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
9153 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
9154 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
 
9155
9156 /* Functions used to truncate the database file. */
9157 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9158
9159 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -25825,10 +25841,12 @@
25825 int prior = 0;
25826 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
25827 i64 newOffset;
25828 #endif
25829 TIMER_START;
 
 
25830 do{
25831 #if defined(USE_PREAD)
25832 got = osPread(id->h, pBuf, cnt, offset);
25833 SimulateIOError( got = -1 );
25834 #elif defined(USE_PREAD64)
@@ -25914,10 +25932,12 @@
25914 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
25915 int got;
25916 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
25917 i64 newOffset;
25918 #endif
 
 
25919 TIMER_START;
25920 #if defined(USE_PREAD)
25921 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
25922 #elif defined(USE_PREAD64)
25923 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
@@ -39558,10 +39578,25 @@
39558 }
39559 }
39560 }
39561 return rc;
39562 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39563
39564 /*
39565 ** Set the value of the Pager.sectorSize variable for the given
39566 ** pager based on the value returned by the xSectorSize method
39567 ** of the open database file. The sector size will be used used
@@ -39594,18 +39629,11 @@
39594 /* Sector size doesn't matter for temporary files. Also, the file
39595 ** may not have been opened yet, in which case the OsSectorSize()
39596 ** call will segfault. */
39597 pPager->sectorSize = 512;
39598 }else{
39599 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
39600 if( pPager->sectorSize<32 ){
39601 pPager->sectorSize = 512;
39602 }
39603 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
39604 assert( MAX_SECTOR_SIZE>=512 );
39605 pPager->sectorSize = MAX_SECTOR_SIZE;
39606 }
39607 }
39608 }
39609
39610 /*
39611 ** Playback the journal and thus restore the database file to
@@ -40518,13 +40546,20 @@
40518 */
40519 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
40520 Pager *pPager, /* Pager object */
40521 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
40522 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
40523 ){
40524 pPager->xBusyHandler = xBusyHandler;
40525 pPager->pBusyHandlerArg = pBusyHandlerArg;
 
 
 
 
 
 
 
40526 }
40527
40528 /*
40529 ** Change the page size used by the Pager object. The new page size
40530 ** is passed in *pPageSize.
@@ -46815,11 +46850,11 @@
46815 ** sector boundary is synced; the part of the last frame that extends
46816 ** past the sector boundary is written after the sync.
46817 */
46818 if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
46819 if( pWal->padToSectorBoundary ){
46820 int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
46821 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
46822 while( iOffset<w.iSyncPoint ){
46823 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
46824 if( rc ) return rc;
46825 iOffset += szFrame;
@@ -50227,10 +50262,28 @@
50227 ** Return the currently defined page size
50228 */
50229 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
50230 return p->pBt->pageSize;
50231 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50232
50233 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
50234 /*
50235 ** Return the number of bytes of space at the end of every page that
50236 ** are intentually left unused. This is the "reserved" space that is
@@ -53284,11 +53337,11 @@
53284 btreeParseCellPtr(pPage, pCell, &info);
53285 if( info.iOverflow==0 ){
53286 return SQLITE_OK; /* No overflow pages. Return without doing anything */
53287 }
53288 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53289 return SQLITE_CORRUPT; /* Cell extends past end of page */
53290 }
53291 ovflPgno = get4byte(&pCell[info.iOverflow]);
53292 assert( pBt->usableSize > 4 );
53293 ovflPageSize = pBt->usableSize - 4;
53294 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
@@ -53950,10 +54003,13 @@
53950 ** enough for all overflow cells.
53951 **
53952 ** If aOvflSpace is set to a null pointer, this function returns
53953 ** SQLITE_NOMEM.
53954 */
 
 
 
53955 static int balance_nonroot(
53956 MemPage *pParent, /* Parent page of siblings being balanced */
53957 int iParentIdx, /* Index of "the page" in pParent */
53958 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
53959 int isRoot, /* True if pParent is a root-page */
@@ -54580,10 +54636,13 @@
54580 releasePage(apNew[i]);
54581 }
54582
54583 return rc;
54584 }
 
 
 
54585
54586
54587 /*
54588 ** This function is called when the root page of a b-tree structure is
54589 ** overfull (has one or more overflow pages).
@@ -56558,17 +56617,20 @@
56558 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
56559 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
56560 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
56561 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
56562 #ifdef SQLITE_HAS_CODEC
56563 int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
 
 
 
56564 int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
56565 #endif
56566
56567 int rc = SQLITE_OK;
56568 i64 iOff;
56569
 
56570 assert( p->bDestLocked );
56571 assert( !isFatalError(p->rc) );
56572 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
56573 assert( zSrcData );
56574
@@ -91600,10 +91662,11 @@
91600 */
91601 aFcntl[0] = 0;
91602 aFcntl[1] = zLeft;
91603 aFcntl[2] = zRight;
91604 aFcntl[3] = 0;
 
91605 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
91606 if( rc==SQLITE_OK ){
91607 if( aFcntl[0] ){
91608 int mem = ++pParse->nMem;
91609 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
@@ -102123,14 +102186,15 @@
102123 #define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
102124 #define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
102125 #define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
102126 #define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
102127 #define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
102128 #define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
102129 #define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
102130 #define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
102131 #define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
 
102132 #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102133 #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102134 #define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102135 #define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102136 #define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
@@ -102154,10 +102218,21 @@
102154 sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
102155 int i, n; /* Which loop is being coded; # of loops */
102156 WhereLevel *aLevel; /* Info about outer loops */
102157 WhereCost cost; /* Lowest cost query plan */
102158 };
 
 
 
 
 
 
 
 
 
 
 
102159
102160 /*
102161 ** Initialize a preallocated WhereClause structure.
102162 */
102163 static void whereClauseInit(
@@ -103306,11 +103381,12 @@
103306 Table *pTab = pIdx->pTable;
103307 int i;
103308 if( pIdx->onError==OE_None ) return 0;
103309 for(i=nSkip; i<pIdx->nColumn; i++){
103310 int j = pIdx->aiColumn[i];
103311 if( j>=0 && pTab->aCol[j].notNull==0 ) return 0;
 
103312 }
103313 return 1;
103314 }
103315
103316 /*
@@ -103370,11 +103446,12 @@
103370 int nEqCol /* Number of index columns with == */
103371 ){
103372 Bitmask mask = 0; /* Mask of unaccounted for pDistinct exprs */
103373 int i; /* Iterator variable */
103374
103375 if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
 
103376 testcase( pDistinct->nExpr==BMS-1 );
103377
103378 /* Loop through all the expressions in the distinct list. If any of them
103379 ** are not simple column references, return early. Otherwise, test if the
103380 ** WHERE clause contains a "col=X" clause. If it does, the expression
@@ -103472,170 +103549,10 @@
103472 }
103473
103474 return 0;
103475 }
103476
103477 /*
103478 ** This routine decides if pIdx can be used to satisfy the ORDER BY
103479 ** clause, either in whole or in part. The return value is the
103480 ** cumulative number of terms in the ORDER BY clause that are satisfied
103481 ** by the index pIdx and other indices in outer loops.
103482 **
103483 ** The table being queried has a cursor number of "base". pIdx is the
103484 ** index that is postulated for use to access the table.
103485 **
103486 ** nEqCol is the number of columns of pIdx that are used as equality
103487 ** constraints and where the other side of the == is an ordered column
103488 ** or constant. An "order column" in the previous sentence means a column
103489 ** in table from an outer loop whose values will always appear in the
103490 ** correct order due to othre index, or because the outer loop generates
103491 ** a unique result. Any of the first nEqCol columns of pIdx may be missing
103492 ** from the ORDER BY clause and the match can still be a success.
103493 **
103494 ** The *pbRev value is set to 0 order 1 depending on whether or not
103495 ** pIdx should be run in the forward order or in reverse order.
103496 */
103497 static int isSortingIndex(
103498 WhereBestIdx *p, /* Best index search context */
103499 Index *pIdx, /* The index we are testing */
103500 int base, /* Cursor number for the table to be sorted */
103501 int nEqCol, /* Number of index columns with ordered == constraints */
103502 int wsFlags, /* Index usages flags */
103503 int bOuterRev, /* True if outer loops scan in reverse order */
103504 int *pbRev /* Set to 1 for reverse-order scan of pIdx */
103505 ){
103506 int i; /* Number of pIdx terms used */
103507 int j; /* Number of ORDER BY terms satisfied */
103508 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103509 int nTerm; /* Number of ORDER BY terms */
103510 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103511 ExprList *pOrderBy; /* The ORDER BY clause */
103512 Parse *pParse = p->pParse; /* Parser context */
103513 sqlite3 *db = pParse->db; /* Database connection */
103514 int nPriorSat; /* ORDER BY terms satisfied by outer loops */
103515 int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
103516 int nEqOneRow; /* Idx columns that ref unique values */
103517
103518 if( p->i==0 ){
103519 nPriorSat = 0;
103520 nEqOneRow = nEqCol;
103521 }else{
103522 if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
103523 nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
103524 sortOrder = bOuterRev;
103525 nEqOneRow = 0;
103526 }
103527 if( p->i>0 && nEqCol==0 /*&& !allOuterLoopsUnique(p)*/ ) return nPriorSat;
103528 pOrderBy = p->pOrderBy;
103529 if( !pOrderBy ) return nPriorSat;
103530 if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
103531 if( pIdx->bUnordered ) return nPriorSat;
103532 nTerm = pOrderBy->nExpr;
103533 assert( nTerm>0 );
103534
103535 /* Argument pIdx must either point to a 'real' named index structure,
103536 ** or an index structure allocated on the stack by bestBtreeIndex() to
103537 ** represent the rowid index that is part of every table. */
103538 assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
103539
103540 /* Match terms of the ORDER BY clause against columns of
103541 ** the index.
103542 **
103543 ** Note that indices have pIdx->nColumn regular columns plus
103544 ** one additional column containing the rowid. The rowid column
103545 ** of the index is also allowed to match against the ORDER BY
103546 ** clause.
103547 */
103548 for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
103549 Expr *pExpr; /* The expression of the ORDER BY pTerm */
103550 CollSeq *pColl; /* The collating sequence of pExpr */
103551 int termSortOrder; /* Sort order for this term */
103552 int iColumn; /* The i-th column of the index. -1 for rowid */
103553 int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
103554 const char *zColl; /* Name of the collating sequence for i-th index term */
103555
103556 pExpr = pTerm->pExpr;
103557 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
103558 /* Can not use an index sort on anything that is not a column in the
103559 ** left-most table of the FROM clause */
103560 break;
103561 }
103562 pColl = sqlite3ExprCollSeq(pParse, pExpr);
103563 if( !pColl ){
103564 pColl = db->pDfltColl;
103565 }
103566 if( pIdx->zName && i<pIdx->nColumn ){
103567 iColumn = pIdx->aiColumn[i];
103568 if( iColumn==pIdx->pTable->iPKey ){
103569 iColumn = -1;
103570 }
103571 iSortOrder = pIdx->aSortOrder[i];
103572 zColl = pIdx->azColl[i];
103573 }else{
103574 iColumn = -1;
103575 iSortOrder = 0;
103576 zColl = pColl->zName;
103577 }
103578 if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
103579 /* Term j of the ORDER BY clause does not match column i of the index */
103580 if( i<nEqCol ){
103581 /* If an index column that is constrained by == fails to match an
103582 ** ORDER BY term, that is OK. Just ignore that column of the index
103583 */
103584 continue;
103585 }else if( i==pIdx->nColumn ){
103586 /* Index column i is the rowid. All other terms match. */
103587 break;
103588 }else{
103589 /* If an index column fails to match and is not constrained by ==
103590 ** then the index cannot satisfy the ORDER BY constraint.
103591 */
103592 return nPriorSat;
103593 }
103594 }
103595 assert( pIdx->aSortOrder!=0 || iColumn==-1 );
103596 assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
103597 assert( iSortOrder==0 || iSortOrder==1 );
103598 termSortOrder = iSortOrder ^ pTerm->sortOrder;
103599 if( i>nEqOneRow ){
103600 if( termSortOrder!=sortOrder ){
103601 /* Indices can only be used if all ORDER BY terms past the
103602 ** equality constraints are all either DESC or ASC. */
103603 break;
103604 }
103605 }else{
103606 sortOrder = termSortOrder;
103607 }
103608 j++;
103609 pTerm++;
103610 if( iColumn<0 ){
103611 seenRowid = 1;
103612 break;
103613 }
103614 }
103615 *pbRev = sortOrder;
103616
103617 /* If there was an "ORDER BY rowid" term that matched, or it is only
103618 ** possible for a single row from this table to match, then skip over
103619 ** any additional ORDER BY terms dealing with this table.
103620 */
103621 if( seenRowid ||
103622 ( (wsFlags & WHERE_COLUMN_NULL)==0
103623 && i>=pIdx->nColumn
103624 && indexIsUniqueNotNull(pIdx, nEqCol)
103625 )
103626 ){
103627 /* Advance j over additional ORDER BY terms associated with base */
103628 WhereMaskSet *pMS = p->pWC->pMaskSet;
103629 Bitmask m = ~getMask(pMS, base);
103630 while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
103631 j++;
103632 }
103633 }
103634 return j;
103635 }
103636
103637 /*
103638 ** Prepare a crude estimate of the logarithm of the input value.
103639 ** The results need not be exact. This is only used for estimating
103640 ** the total cost of performing operations with O(logN) or O(NlogN)
103641 ** complexity. Because N is just a guess, it is no great tragedy if
@@ -103785,10 +103702,11 @@
103785 WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103786 if( rTotal<p->cost.rCost ){
103787 p->cost.rCost = rTotal;
103788 p->cost.used = used;
103789 p->cost.plan.nRow = nRow;
 
103790 p->cost.plan.wsFlags = flags;
103791 p->cost.plan.u.pTerm = pTerm;
103792 }
103793 }
103794 }
@@ -104327,11 +104245,14 @@
104327 }else{
104328 p->cost.rCost = rCost;
104329 }
104330 p->cost.plan.u.pVtabIdx = pIdxInfo;
104331 if( pIdxInfo->orderByConsumed ){
104332 p->cost.plan.wsFlags |= WHERE_ORDERBY;
 
 
 
104333 }
104334 p->cost.plan.nEq = 0;
104335 pIdxInfo->nOrderBy = nOrderBy;
104336
104337 /* Try to find a more efficient access pattern by using multiple indexes
@@ -104750,20 +104671,26 @@
104750 WhereLevel *pLevel = &p->aLevel[p->i-1];
104751 Index *pIdx;
104752 u8 sortOrder;
104753 for(i=p->i-1; i>=0; i--, pLevel--){
104754 if( pLevel->iTabCur!=iTab ) continue;
104755 if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
104756 pIdx = pLevel->plan.u.pIdx;
 
 
 
 
 
104757 if( iCol<0 ){
104758 sortOrder = 0;
104759 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104760 }else{
104761 for(j=0; j<pIdx->nColumn; j++){
 
104762 if( iCol==pIdx->aiColumn[j] ) break;
104763 }
104764 if( j>=pIdx->nColumn ) return 0;
104765 sortOrder = pIdx->aSortOrder[j];
104766 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104767 }
104768 }else{
104769 if( iCol!=(-1) ) return 0;
@@ -104792,13 +104719,10 @@
104792 static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
104793 Expr *pExpr = pTerm->pExpr;
104794 assert( pExpr->op==TK_EQ );
104795 assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
104796 assert( pExpr->pRight!=0 );
104797 if( p->i==0 ){
104798 return 1; /* All == are ordered in the outer loop */
104799 }
104800 if( pTerm->prereqRight==0 ){
104801 return 1; /* RHS of the == is a constant */
104802 }
104803 if( pExpr->pRight->op==TK_COLUMN
104804 && isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
@@ -104808,10 +104732,177 @@
104808
104809 /* If we cannot prove that the constraint is ordered, assume it is not */
104810 return 0;
104811 }
104812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104813
104814 /*
104815 ** Find the best query plan for accessing a particular table. Write the
104816 ** best query plan and its cost into the p->cost.
104817 **
@@ -104902,22 +104993,20 @@
104902
104903 /* Loop over all indices looking for the best one to use
104904 */
104905 for(; pProbe; pIdx=pProbe=pProbe->pNext){
104906 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104907 double cost; /* Cost of using pProbe */
104908 double nRow; /* Estimated number of rows in result set */
104909 double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
104910 int bRev = 2; /* 0=forward scan. 1=reverse. 2=undecided */
104911 int wsFlags = 0;
104912 Bitmask used = 0;
104913
104914 /* The following variables are populated based on the properties of
104915 ** index being evaluated. They are then used to determine the expected
104916 ** cost and number of rows returned.
104917 **
104918 ** nEq:
104919 ** Number of equality terms that can be implemented using the index.
104920 ** In other words, the number of initial fields in the index that
104921 ** are used in == or IN or NOT NULL constraints of the WHERE clause.
104922 **
104923 ** nInMul:
@@ -104960,11 +105049,11 @@
104960 ** bSort:
104961 ** Boolean. True if there is an ORDER BY clause that will require an
104962 ** external sort (i.e. scanning the index being evaluated will not
104963 ** correctly order records).
104964 **
104965 ** bDistinct:
104966 ** Boolean. True if there is a DISTINCT clause that will require an
104967 ** external btree.
104968 **
104969 ** bLookup:
104970 ** Boolean. True if a table lookup is required for each index entry
@@ -104979,132 +105068,146 @@
104979 ** both available in the index.
104980 **
104981 ** SELECT a, b FROM tbl WHERE a = 1;
104982 ** SELECT a, b, c FROM tbl WHERE a = 1;
104983 */
104984 int nEq; /* Number of == or IN terms matching index */
104985 int nOrdered; /* Number of ordered terms matching index */
104986 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
104987 int nInMul = 1; /* Number of distinct equalities to lookup */
104988 double rangeDiv = (double)1; /* Estimated reduction in search space */
104989 int nBound = 0; /* Number of range constraints seen */
104990 int bSort; /* True if external sort required */
104991 int bDist; /* True if index cannot help with DISTINCT */
104992 int bLookup = 0; /* True if not a covering index */
104993 int nOBSat = 0; /* Number of ORDER BY terms satisfied */
104994 int nOrderBy; /* Number of ORDER BY terms */
104995 WhereTerm *pTerm; /* A single term of the WHERE clause */
104996 #ifdef SQLITE_ENABLE_STAT3
104997 WhereTerm *pFirstTerm = 0; /* First term matching the index */
104998 #endif
104999
105000 nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105001 bSort = nOrderBy>0 && (p->i==0 || p->aLevel[p->i-1].plan.nOBSat<nOrderBy);
105002 bDist = p->i==0 && p->pDistinct!=0;
 
 
 
 
 
 
 
105003
105004 /* Determine the values of nEq and nInMul */
105005 for(nEq=nOrdered=0; nEq<pProbe->nColumn; nEq++){
105006 int j = pProbe->aiColumn[nEq];
105007 pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
105008 if( pTerm==0 ) break;
105009 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
105010 testcase( pTerm->pWC!=pWC );
105011 if( pTerm->eOperator & WO_IN ){
105012 Expr *pExpr = pTerm->pExpr;
105013 wsFlags |= WHERE_COLUMN_IN;
105014 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
105015 /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
105016 nInMul *= 25;
105017 bInEst = 1;
105018 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
105019 /* "x IN (value, value, ...)" */
105020 nInMul *= pExpr->x.pList->nExpr;
105021 }
105022 }else if( pTerm->eOperator & WO_ISNULL ){
105023 wsFlags |= WHERE_COLUMN_NULL;
105024 if( nEq==nOrdered ) nOrdered++;
105025 }else if( bSort && nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105026 nOrdered++;
105027 }
105028 #ifdef SQLITE_ENABLE_STAT3
105029 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105030 #endif
105031 used |= pTerm->prereqRight;
105032 }
105033
105034 /* If the index being considered is UNIQUE, and there is an equality
105035 ** constraint for all columns in the index, then this search will find
105036 ** at most a single row. In this case set the WHERE_UNIQUE flag to
105037 ** indicate this to the caller.
105038 **
105039 ** Otherwise, if the search may find more than one row, test to see if
105040 ** there is a range constraint on indexed column (nEq+1) that can be
105041 ** optimized using the index.
105042 */
105043 if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105044 testcase( wsFlags & WHERE_COLUMN_IN );
105045 testcase( wsFlags & WHERE_COLUMN_NULL );
105046 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105047 wsFlags |= WHERE_UNIQUE;
 
 
 
105048 }
105049 }else if( pProbe->bUnordered==0 ){
105050 int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
 
105051 if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105052 WhereTerm *pTop, *pBtm;
105053 pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
105054 pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
105055 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
105056 if( pTop ){
105057 nBound = 1;
105058 wsFlags |= WHERE_TOP_LIMIT;
105059 used |= pTop->prereqRight;
105060 testcase( pTop->pWC!=pWC );
105061 }
105062 if( pBtm ){
105063 nBound++;
105064 wsFlags |= WHERE_BTM_LIMIT;
105065 used |= pBtm->prereqRight;
105066 testcase( pBtm->pWC!=pWC );
105067 }
105068 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
105069 }
105070 }
105071
105072 /* If there is an ORDER BY clause and the index being considered will
105073 ** naturally scan rows in the required order, set the appropriate flags
105074 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
105075 ** will scan rows in a different order, set the bSort variable. */
 
105076 assert( bRev>=0 && bRev<=2 );
105077 if( bSort ){
105078 testcase( bRev==0 );
105079 testcase( bRev==1 );
105080 testcase( bRev==2 );
105081 nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105082 wsFlags, bRev&1, &bRev);
105083 if( nOrderBy==nOBSat ){
 
 
 
105084 bSort = 0;
105085 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
105086 }
105087 if( bRev & 1 ) wsFlags |= WHERE_REVERSE;
105088 }
105089
105090 /* If there is a DISTINCT qualifier and this index will scan rows in
105091 ** order of the DISTINCT expressions, clear bDist and set the appropriate
105092 ** flags in wsFlags. */
105093 if( bDist
105094 && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, nEq)
105095 && (wsFlags & WHERE_COLUMN_IN)==0
105096 ){
105097 bDist = 0;
105098 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
105099 }
105100
105101 /* If currently calculating the cost of using an index (not the IPK
105102 ** index), determine if all required column data may be obtained without
105103 ** using the main table (i.e. if the index is a covering
105104 ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
105105 ** wsFlags. Otherwise, set the bLookup variable to true. */
105106 if( pIdx ){
105107 Bitmask m = pSrc->colUsed;
105108 int j;
105109 for(j=0; j<pIdx->nColumn; j++){
105110 int x = pIdx->aiColumn[j];
@@ -105111,51 +105214,54 @@
105111 if( x<BMS-1 ){
105112 m &= ~(((Bitmask)1)<<x);
105113 }
105114 }
105115 if( m==0 ){
105116 wsFlags |= WHERE_IDX_ONLY;
105117 }else{
105118 bLookup = 1;
105119 }
105120 }
105121
105122 /*
105123 ** Estimate the number of rows of output. For an "x IN (SELECT...)"
105124 ** constraint, do not let the estimate exceed half the rows in the table.
105125 */
105126 nRow = (double)(aiRowEst[nEq] * nInMul);
105127 if( bInEst && nRow*2>aiRowEst[0] ){
105128 nRow = aiRowEst[0]/2;
105129 nInMul = (int)(nRow / aiRowEst[nEq]);
105130 }
105131
105132 #ifdef SQLITE_ENABLE_STAT3
105133 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
105134 ** and we do not think that values of x are unique and if histogram
105135 ** data is available for column x, then it might be possible
105136 ** to get a better estimate on the number of rows based on
105137 ** VALUE and how common that value is according to the histogram.
105138 */
105139 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
 
105140 assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
105141 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
105142 testcase( pFirstTerm->eOperator==WO_EQ );
105143 testcase( pFirstTerm->eOperator==WO_ISNULL );
105144 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
 
105145 }else if( bInEst==0 ){
105146 assert( pFirstTerm->eOperator==WO_IN );
105147 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
 
105148 }
105149 }
105150 #endif /* SQLITE_ENABLE_STAT3 */
105151
105152 /* Adjust the number of output rows and downward to reflect rows
105153 ** that are excluded by range constraints.
105154 */
105155 nRow = nRow/rangeDiv;
105156 if( nRow<1 ) nRow = 1;
105157
105158 /* Experiments run on real SQLite databases show that the time needed
105159 ** to do a binary search to locate a row in a table or index is roughly
105160 ** log10(N) times the time to move from one row to the next row within
105161 ** a table or index. The actual times can vary, with the size of
@@ -105166,57 +105272,58 @@
105166 ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
105167 ** not give us data on the relative sizes of table and index records.
105168 ** So this computation assumes table records are about twice as big
105169 ** as index records
105170 */
105171 if( (wsFlags&~WHERE_REVERSE)==WHERE_IDX_ONLY
105172 && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
105173 && sqlite3GlobalConfig.bUseCis
105174 && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
105175 ){
105176 /* This index is not useful for indexing, but it is a covering index.
105177 ** A full-scan of the index might be a little faster than a full-scan
105178 ** of the table, so give this case a cost slightly less than a table
105179 ** scan. */
105180 cost = aiRowEst[0]*3 + pProbe->nColumn;
105181 wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
105182 }else if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
105183 /* The cost of a full table scan is a number of move operations equal
105184 ** to the number of rows in the table.
105185 **
105186 ** We add an additional 4x penalty to full table scans. This causes
105187 ** the cost function to err on the side of choosing an index over
105188 ** choosing a full scan. This 4x full-scan penalty is an arguable
105189 ** decision and one which we expect to revisit in the future. But
105190 ** it seems to be working well enough at the moment.
105191 */
105192 cost = aiRowEst[0]*4;
105193 wsFlags &= ~WHERE_IDX_ONLY;
 
105194 }else{
105195 log10N = estLog(aiRowEst[0]);
105196 cost = nRow;
105197 if( pIdx ){
105198 if( bLookup ){
105199 /* For an index lookup followed by a table lookup:
105200 ** nInMul index searches to find the start of each index range
105201 ** + nRow steps through the index
105202 ** + nRow table searches to lookup the table entry using the rowid
105203 */
105204 cost += (nInMul + nRow)*log10N;
105205 }else{
105206 /* For a covering index:
105207 ** nInMul index searches to find the initial entry
105208 ** + nRow steps through the index
105209 */
105210 cost += nInMul*log10N;
105211 }
105212 }else{
105213 /* For a rowid primary key lookup:
105214 ** nInMult table searches to find the initial entry for each range
105215 ** + nRow steps through the table
105216 */
105217 cost += nInMul*log10N;
105218 }
105219 }
105220
105221 /* Add in the estimated cost of sorting the result. Actual experimental
105222 ** measurements of sorting performance in SQLite show that sorting time
@@ -105223,14 +105330,16 @@
105223 ** adds C*N*log10(N) to the cost, where N is the number of rows to be
105224 ** sorted and C is a factor between 1.95 and 4.3. We will split the
105225 ** difference and select C of 3.0.
105226 */
105227 if( bSort ){
105228 cost += nRow*estLog(nRow*(nOrderBy - nOBSat)/nOrderBy)*3;
 
 
105229 }
105230 if( bDist ){
105231 cost += nRow*estLog(nRow)*3;
105232 }
105233
105234 /**** Cost of using this index has now been computed ****/
105235
105236 /* If there are additional constraints on this table that cannot
@@ -105247,29 +105356,29 @@
105247 ** tables that are not in outer loops. If notReady is used here instead
105248 ** of notValid, then a optimal index that depends on inner joins loops
105249 ** might be selected even when there exists an optimal index that has
105250 ** no such dependency.
105251 */
105252 if( nRow>2 && cost<=p->cost.rCost ){
105253 int k; /* Loop counter */
105254 int nSkipEq = nEq; /* Number of == constraints to skip */
105255 int nSkipRange = nBound; /* Number of < constraints to skip */
105256 Bitmask thisTab; /* Bitmap for pSrc */
105257
105258 thisTab = getMask(pWC->pMaskSet, iCur);
105259 for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
105260 if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105261 if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
105262 if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105263 if( nSkipEq ){
105264 /* Ignore the first nEq equality matches since the index
105265 ** has already accounted for these */
105266 nSkipEq--;
105267 }else{
105268 /* Assume each additional equality match reduces the result
105269 ** set size by a factor of 10 */
105270 nRow /= 10;
105271 }
105272 }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
105273 if( nSkipRange ){
105274 /* Ignore the first nSkipRange range constraints since the index
105275 ** has already accounted for these */
@@ -105279,43 +105388,38 @@
105279 ** set size by a factor of 3. Indexed range constraints reduce
105280 ** the search space by a larger factor: 4. We make indexed range
105281 ** more selective intentionally because of the subjective
105282 ** observation that indexed range constraints really are more
105283 ** selective in practice, on average. */
105284 nRow /= 3;
105285 }
105286 }else if( pTerm->eOperator!=WO_NOOP ){
105287 /* Any other expression lowers the output row count by half */
105288 nRow /= 2;
105289 }
105290 }
105291 if( nRow<2 ) nRow = 2;
105292 }
105293
105294
105295 WHERETRACE((
105296 "%s(%s):\n"
105297 " nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
105298 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
105299 " used=0x%llx nOrdered=%d nOBSat=%d\n",
105300 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105301 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
105302 p->notReady, log10N, nRow, cost, used, nOrdered, nOBSat
 
105303 ));
105304
105305 /* If this index is the best we have seen so far, then record this
105306 ** index and its cost in the pCost structure.
105307 */
105308 if( (!pIdx || wsFlags)
105309 && (cost<p->cost.rCost || (cost<=p->cost.rCost && nRow<p->cost.plan.nRow))
105310 ){
105311 p->cost.rCost = cost;
105312 p->cost.used = used;
105313 p->cost.plan.nRow = nRow;
105314 p->cost.plan.wsFlags = (wsFlags&wsFlagMask);
105315 p->cost.plan.nEq = nEq;
105316 p->cost.plan.nOBSat = nOBSat;
105317 p->cost.plan.u.pIdx = pIdx;
105318 }
105319
105320 /* If there was an INDEXED BY clause, then only that one index is
105321 ** considered. */
@@ -105333,21 +105437,19 @@
105333 ** SQLite outputs rows in in the absence of an ORDER BY clause. */
105334 if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105335 p->cost.plan.wsFlags |= WHERE_REVERSE;
105336 }
105337
105338 assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERBY)==0 );
105339 assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105340 assert( pSrc->pIndex==0
105341 || p->cost.plan.u.pIdx==0
105342 || p->cost.plan.u.pIdx==pSrc->pIndex
105343 );
105344
105345 WHERETRACE(("best index is: %s\n",
105346 ((p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105347 p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk")
105348 ));
105349
105350 bestOrClauseIndex(p);
105351 bestAutomaticIndex(p);
105352 p->cost.plan.wsFlags |= eqTermMask;
105353 }
@@ -106071,11 +106173,11 @@
106071 ** should not have a NULL value stored in 'x'. If column 'x' is
106072 ** the first one after the nEq equality constraints in the index,
106073 ** this requires some special handling.
106074 */
106075 if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
106076 && (pLevel->plan.wsFlags&WHERE_ORDERBY)
106077 && (pIdx->nColumn>nEq)
106078 ){
106079 /* assert( pOrderBy->nExpr==1 ); */
106080 /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
106081 isMinQuery = 1;
@@ -106892,12 +106994,12 @@
106892 continue;
106893 }
106894 sWBI.notReady = (isOptimal ? m : sWBI.notValid);
106895 if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
106896
106897 WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
106898 j, isOptimal));
106899 assert( sWBI.pSrc->pTab );
106900 #ifndef SQLITE_OMIT_VIRTUALTABLE
106901 if( IsVirtual(sWBI.pSrc->pTab) ){
106902 sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
106903 bestVirtualIndex(&sWBI);
@@ -106934,48 +107036,46 @@
106934 ** combination of INDEXED BY clauses are given. The error
106935 ** will be detected and relayed back to the application later.
106936 ** The NEVER() comes about because rule (2) above prevents
106937 ** An indexable full-table-scan from reaching rule (3).
106938 **
106939 ** (4) The plan cost must be lower than prior plans or else the
106940 ** cost must be the same and the number of rows must be lower.
106941 */
106942 if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
106943 && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106944 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106945 || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106946 && (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
106947 || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106948 && (bestJ<0 || sWBI.cost.rCost<bestPlan.rCost /* (4) */
106949 || (sWBI.cost.rCost<=bestPlan.rCost
106950 && sWBI.cost.plan.nRow<bestPlan.plan.nRow))
106951 ){
106952 WHERETRACE(("=== table %d is best so far"
106953 " with cost=%.1f, nRow=%.1f, nOBSat=%d\n",
106954 j, sWBI.cost.rCost, sWBI.cost.plan.nRow,
106955 sWBI.cost.plan.nOBSat));
 
106956 bestPlan = sWBI.cost;
106957 bestJ = j;
106958 }
106959 if( doNotReorder ) break;
106960 }
106961 }
106962 assert( bestJ>=0 );
106963 assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106964 WHERETRACE(("*** Optimizer selects table %d for loop %d with:\n"
106965 " cost=%.1f, nRow=%.1f, nOBSat=%d wsFlags=0x%08x\n",
106966 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
 
106967 bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
106968 if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
106969 pWInfo->nOBSat = pOrderBy->nExpr;
106970 }
106971 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
106972 assert( pWInfo->eDistinct==0 );
106973 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
106974 }
106975 andFlags &= bestPlan.plan.wsFlags;
106976 pLevel->plan = bestPlan.plan;
 
106977 testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
106978 testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
106979 if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
106980 if( (wctrlFlags & WHERE_ONETABLE_ONLY)
106981 && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0
@@ -107013,15 +107113,22 @@
107013 }
107014 WHERETRACE(("*** Optimizer Finished ***\n"));
107015 if( pParse->nErr || db->mallocFailed ){
107016 goto whereBeginError;
107017 }
 
 
 
 
 
 
107018
107019 /* If the total query only selects a single row, then the ORDER BY
107020 ** clause is irrelevant.
107021 */
107022 if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
 
107023 pWInfo->nOBSat = pOrderBy->nExpr;
107024 }
107025
107026 /* If the caller is an UPDATE or DELETE statement that is requesting
107027 ** to use a one-pass algorithm, determine if this is appropriate.
@@ -107045,11 +107152,10 @@
107045 int iDb; /* Index of database containing table/index */
107046 struct SrcList_item *pTabItem;
107047
107048 pTabItem = &pTabList->a[pLevel->iFrom];
107049 pTab = pTabItem->pTab;
107050 pLevel->iTabCur = pTabItem->iCursor;
107051 pWInfo->nRowOut *= pLevel->plan.nRow;
107052 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107053 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
107054 /* Do nothing */
107055 }else
@@ -114978,11 +115084,11 @@
114978 ** with various optimizations disabled to verify that the same answer
114979 ** is obtained in every case.
114980 */
114981 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
114982 sqlite3 *db = va_arg(ap, sqlite3*);
114983 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
114984 break;
114985 }
114986
114987 #ifdef SQLITE_N_KEYWORD
114988 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
@@ -135232,11 +135338,11 @@
135232 /*
135233 ** Remove the entry with rowid=iDelete from the r-tree structure.
135234 */
135235 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
135236 int rc; /* Return code */
135237 RtreeNode *pLeaf; /* Leaf node containing record iDelete */
135238 int iCell; /* Index of iDelete cell in pLeaf */
135239 RtreeNode *pRoot; /* Root node of rtree structure */
135240
135241
135242 /* Obtain a reference to the root node to initialise Rtree.iDepth */
@@ -135435,11 +135541,11 @@
135435 ** (azData[2]..azData[argc-1]) contain a new record to insert into
135436 ** the r-tree structure.
135437 */
135438 if( rc==SQLITE_OK && nData>1 ){
135439 /* Insert the new record into the r-tree */
135440 RtreeNode *pLeaf;
135441
135442 /* Figure out the rowid of the new row. */
135443 if( bHaveRowid==0 ){
135444 rc = newRowid(pRtree, &cell.iRowid);
135445 }
135446
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674 ** [sqlite_version()] and [sqlite_source_id()].
675 */
676 #define SQLITE_VERSION "3.7.15"
677 #define SQLITE_VERSION_NUMBER 3007015
678 #define SQLITE_SOURCE_ID "2012-10-03 12:56:18 956e4d7f8958e7065ff2d61cd71519d6f4113d4a"
679
680 /*
681 ** CAPI3REF: Run-Time Library Version Numbers
682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
683 **
@@ -1421,10 +1421,21 @@
1421 ** that the VFS encountered an error while handling the [PRAGMA] and the
1422 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1423 ** file control occurs at the beginning of pragma statement analysis and so
1424 ** it is able to override built-in [PRAGMA] statements.
1425 ** </ul>
1426 **
1427 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1428 ** ^This file-control may be invoked by SQLite on the database file handle
1429 ** shortly after it is opened in order to provide a custom VFS with access
1430 ** to the connections busy-handler callback. The argument is of type (void **)
1431 ** - an array of two (void *) values. The first (void *) actually points
1432 ** to a function of type (int (*)(void *)). In order to invoke the connections
1433 ** busy-handler, this function should be invoked with the second (void *) in
1434 ** the array as the only argument. If it returns non-zero, then the operation
1435 ** should be retried. If it returns zero, the custom VFS should abandon the
1436 ** current operation.
1437 */
1438 #define SQLITE_FCNTL_LOCKSTATE 1
1439 #define SQLITE_GET_LOCKPROXYFILE 2
1440 #define SQLITE_SET_LOCKPROXYFILE 3
1441 #define SQLITE_LAST_ERRNO 4
@@ -1436,10 +1447,11 @@
1447 #define SQLITE_FCNTL_PERSIST_WAL 10
1448 #define SQLITE_FCNTL_OVERWRITE 11
1449 #define SQLITE_FCNTL_VFSNAME 12
1450 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1451 #define SQLITE_FCNTL_PRAGMA 14
1452 #define SQLITE_FCNTL_BUSYHANDLER 15
1453
1454 /*
1455 ** CAPI3REF: Mutex Handle
1456 **
1457 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -8381,10 +8393,13 @@
8393 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8394 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8395 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
8396 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
8397 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
8398 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
8399 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
8400 #endif
8401 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
8402 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
8403 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
8404 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
8405 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
@@ -9150,10 +9165,11 @@
9165 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
9166 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
9167 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
9168 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
9169 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
9170 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
9171
9172 /* Functions used to truncate the database file. */
9173 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9174
9175 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -25825,10 +25841,12 @@
25841 int prior = 0;
25842 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
25843 i64 newOffset;
25844 #endif
25845 TIMER_START;
25846 assert( cnt==(cnt&0x1ffff) );
25847 cnt &= 0x1ffff;
25848 do{
25849 #if defined(USE_PREAD)
25850 got = osPread(id->h, pBuf, cnt, offset);
25851 SimulateIOError( got = -1 );
25852 #elif defined(USE_PREAD64)
@@ -25914,10 +25932,12 @@
25932 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
25933 int got;
25934 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
25935 i64 newOffset;
25936 #endif
25937 assert( cnt==(cnt&0x1ffff) );
25938 cnt &= 0x1ffff;
25939 TIMER_START;
25940 #if defined(USE_PREAD)
25941 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
25942 #elif defined(USE_PREAD64)
25943 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
@@ -39558,10 +39578,25 @@
39578 }
39579 }
39580 }
39581 return rc;
39582 }
39583
39584 /*
39585 ** Return a sanitized version of the sector-size of OS file pFile. The
39586 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
39587 */
39588 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
39589 int iRet = sqlite3OsSectorSize(pFile);
39590 if( iRet<32 ){
39591 iRet = 512;
39592 }else if( iRet>MAX_SECTOR_SIZE ){
39593 assert( MAX_SECTOR_SIZE>=512 );
39594 iRet = MAX_SECTOR_SIZE;
39595 }
39596 return iRet;
39597 }
39598
39599 /*
39600 ** Set the value of the Pager.sectorSize variable for the given
39601 ** pager based on the value returned by the xSectorSize method
39602 ** of the open database file. The sector size will be used used
@@ -39594,18 +39629,11 @@
39629 /* Sector size doesn't matter for temporary files. Also, the file
39630 ** may not have been opened yet, in which case the OsSectorSize()
39631 ** call will segfault. */
39632 pPager->sectorSize = 512;
39633 }else{
39634 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
 
 
 
 
 
 
 
39635 }
39636 }
39637
39638 /*
39639 ** Playback the journal and thus restore the database file to
@@ -40518,13 +40546,20 @@
40546 */
40547 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
40548 Pager *pPager, /* Pager object */
40549 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
40550 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
40551 ){
40552 pPager->xBusyHandler = xBusyHandler;
40553 pPager->pBusyHandlerArg = pBusyHandlerArg;
40554
40555 if( isOpen(pPager->fd) ){
40556 void **ap = (void **)&pPager->xBusyHandler;
40557 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
40558 assert( ap[1]==pBusyHandlerArg );
40559 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
40560 }
40561 }
40562
40563 /*
40564 ** Change the page size used by the Pager object. The new page size
40565 ** is passed in *pPageSize.
@@ -46815,11 +46850,11 @@
46850 ** sector boundary is synced; the part of the last frame that extends
46851 ** past the sector boundary is written after the sync.
46852 */
46853 if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
46854 if( pWal->padToSectorBoundary ){
46855 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
46856 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
46857 while( iOffset<w.iSyncPoint ){
46858 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
46859 if( rc ) return rc;
46860 iOffset += szFrame;
@@ -50227,10 +50262,28 @@
50262 ** Return the currently defined page size
50263 */
50264 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
50265 return p->pBt->pageSize;
50266 }
50267
50268 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
50269 /*
50270 ** This function is similar to sqlite3BtreeGetReserve(), except that it
50271 ** may only be called if it is guaranteed that the b-tree mutex is already
50272 ** held.
50273 **
50274 ** This is useful in one special case in the backup API code where it is
50275 ** known that the shared b-tree mutex is held, but the mutex on the
50276 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
50277 ** were to be called, it might collide with some other operation on the
50278 ** database handle that owns *p, causing undefined behaviour.
50279 */
50280 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
50281 assert( sqlite3_mutex_held(p->pBt->mutex) );
50282 return p->pBt->pageSize - p->pBt->usableSize;
50283 }
50284 #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
50285
50286 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
50287 /*
50288 ** Return the number of bytes of space at the end of every page that
50289 ** are intentually left unused. This is the "reserved" space that is
@@ -53284,11 +53337,11 @@
53337 btreeParseCellPtr(pPage, pCell, &info);
53338 if( info.iOverflow==0 ){
53339 return SQLITE_OK; /* No overflow pages. Return without doing anything */
53340 }
53341 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53342 return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
53343 }
53344 ovflPgno = get4byte(&pCell[info.iOverflow]);
53345 assert( pBt->usableSize > 4 );
53346 ovflPageSize = pBt->usableSize - 4;
53347 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
@@ -53950,10 +54003,13 @@
54003 ** enough for all overflow cells.
54004 **
54005 ** If aOvflSpace is set to a null pointer, this function returns
54006 ** SQLITE_NOMEM.
54007 */
54008 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
54009 #pragma optimize("", off)
54010 #endif
54011 static int balance_nonroot(
54012 MemPage *pParent, /* Parent page of siblings being balanced */
54013 int iParentIdx, /* Index of "the page" in pParent */
54014 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
54015 int isRoot, /* True if pParent is a root-page */
@@ -54580,10 +54636,13 @@
54636 releasePage(apNew[i]);
54637 }
54638
54639 return rc;
54640 }
54641 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
54642 #pragma optimize("", on)
54643 #endif
54644
54645
54646 /*
54647 ** This function is called when the root page of a b-tree structure is
54648 ** overfull (has one or more overflow pages).
@@ -56558,17 +56617,20 @@
56617 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
56618 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
56619 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
56620 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
56621 #ifdef SQLITE_HAS_CODEC
56622 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
56623 ** guaranteed that the shared-mutex is held by this thread, handle
56624 ** p->pSrc may not actually be the owner. */
56625 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
56626 int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
56627 #endif
 
56628 int rc = SQLITE_OK;
56629 i64 iOff;
56630
56631 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
56632 assert( p->bDestLocked );
56633 assert( !isFatalError(p->rc) );
56634 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
56635 assert( zSrcData );
56636
@@ -91600,10 +91662,11 @@
91662 */
91663 aFcntl[0] = 0;
91664 aFcntl[1] = zLeft;
91665 aFcntl[2] = zRight;
91666 aFcntl[3] = 0;
91667 db->busyHandler.nBusy = 0;
91668 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
91669 if( rc==SQLITE_OK ){
91670 if( aFcntl[0] ){
91671 int mem = ++pParse->nMem;
91672 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
@@ -102123,14 +102186,15 @@
102186 #define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
102187 #define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
102188 #define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
102189 #define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
102190 #define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
102191 #define WHERE_IDX_ONLY 0x00400000 /* Use index only - omit table */
102192 #define WHERE_ORDERED 0x00800000 /* Output will appear in correct order */
102193 #define WHERE_REVERSE 0x01000000 /* Scan in reverse order */
102194 #define WHERE_UNIQUE 0x02000000 /* Selects no more than one row */
102195 #define WHERE_ALL_UNIQUE 0x04000000 /* This and all prior have one row */
102196 #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102197 #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102198 #define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102199 #define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102200 #define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
@@ -102154,10 +102218,21 @@
102218 sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
102219 int i, n; /* Which loop is being coded; # of loops */
102220 WhereLevel *aLevel; /* Info about outer loops */
102221 WhereCost cost; /* Lowest cost query plan */
102222 };
102223
102224 /*
102225 ** Return TRUE if the probe cost is less than the baseline cost
102226 */
102227 static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
102228 if( pProbe->rCost<pBaseline->rCost ) return 1;
102229 if( pProbe->rCost>pBaseline->rCost ) return 0;
102230 if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
102231 if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
102232 return 0;
102233 }
102234
102235 /*
102236 ** Initialize a preallocated WhereClause structure.
102237 */
102238 static void whereClauseInit(
@@ -103306,11 +103381,12 @@
103381 Table *pTab = pIdx->pTable;
103382 int i;
103383 if( pIdx->onError==OE_None ) return 0;
103384 for(i=nSkip; i<pIdx->nColumn; i++){
103385 int j = pIdx->aiColumn[i];
103386 assert( j>=0 && j<pTab->nCol );
103387 if( pTab->aCol[j].notNull==0 ) return 0;
103388 }
103389 return 1;
103390 }
103391
103392 /*
@@ -103370,11 +103446,12 @@
103446 int nEqCol /* Number of index columns with == */
103447 ){
103448 Bitmask mask = 0; /* Mask of unaccounted for pDistinct exprs */
103449 int i; /* Iterator variable */
103450
103451 assert( pDistinct!=0 );
103452 if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
103453 testcase( pDistinct->nExpr==BMS-1 );
103454
103455 /* Loop through all the expressions in the distinct list. If any of them
103456 ** are not simple column references, return early. Otherwise, test if the
103457 ** WHERE clause contains a "col=X" clause. If it does, the expression
@@ -103472,170 +103549,10 @@
103549 }
103550
103551 return 0;
103552 }
103553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103554 /*
103555 ** Prepare a crude estimate of the logarithm of the input value.
103556 ** The results need not be exact. This is only used for estimating
103557 ** the total cost of performing operations with O(logN) or O(NlogN)
103558 ** complexity. Because N is just a guess, it is no great tragedy if
@@ -103785,10 +103702,11 @@
103702 WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103703 if( rTotal<p->cost.rCost ){
103704 p->cost.rCost = rTotal;
103705 p->cost.used = used;
103706 p->cost.plan.nRow = nRow;
103707 p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
103708 p->cost.plan.wsFlags = flags;
103709 p->cost.plan.u.pTerm = pTerm;
103710 }
103711 }
103712 }
@@ -104327,11 +104245,14 @@
104245 }else{
104246 p->cost.rCost = rCost;
104247 }
104248 p->cost.plan.u.pVtabIdx = pIdxInfo;
104249 if( pIdxInfo->orderByConsumed ){
104250 p->cost.plan.wsFlags |= WHERE_ORDERED;
104251 p->cost.plan.nOBSat = nOrderBy;
104252 }else{
104253 p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
104254 }
104255 p->cost.plan.nEq = 0;
104256 pIdxInfo->nOrderBy = nOrderBy;
104257
104258 /* Try to find a more efficient access pattern by using multiple indexes
@@ -104750,20 +104671,26 @@
104671 WhereLevel *pLevel = &p->aLevel[p->i-1];
104672 Index *pIdx;
104673 u8 sortOrder;
104674 for(i=p->i-1; i>=0; i--, pLevel--){
104675 if( pLevel->iTabCur!=iTab ) continue;
104676 if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
104677 return 1;
104678 }
104679 if( (pLevel->plan.wsFlags & WHERE_ORDERED)==0 ){
104680 return 0;
104681 }
104682 if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
104683 if( iCol<0 ){
104684 sortOrder = 0;
104685 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104686 }else{
104687 int n = pIdx->nColumn;
104688 for(j=0; j<n; j++){
104689 if( iCol==pIdx->aiColumn[j] ) break;
104690 }
104691 if( j>=n ) return 0;
104692 sortOrder = pIdx->aSortOrder[j];
104693 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104694 }
104695 }else{
104696 if( iCol!=(-1) ) return 0;
@@ -104792,13 +104719,10 @@
104719 static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
104720 Expr *pExpr = pTerm->pExpr;
104721 assert( pExpr->op==TK_EQ );
104722 assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
104723 assert( pExpr->pRight!=0 );
 
 
 
104724 if( pTerm->prereqRight==0 ){
104725 return 1; /* RHS of the == is a constant */
104726 }
104727 if( pExpr->pRight->op==TK_COLUMN
104728 && isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
@@ -104808,10 +104732,177 @@
104732
104733 /* If we cannot prove that the constraint is ordered, assume it is not */
104734 return 0;
104735 }
104736
104737 /*
104738 ** This routine decides if pIdx can be used to satisfy the ORDER BY
104739 ** clause, either in whole or in part. The return value is the
104740 ** cumulative number of terms in the ORDER BY clause that are satisfied
104741 ** by the index pIdx and other indices in outer loops.
104742 **
104743 ** The table being queried has a cursor number of "base". pIdx is the
104744 ** index that is postulated for use to access the table.
104745 **
104746 ** nEqCol is the number of columns of pIdx that are used as equality
104747 ** constraints and where the other side of the == is an ordered column
104748 ** or constant. An "order column" in the previous sentence means a column
104749 ** in table from an outer loop whose values will always appear in the
104750 ** correct order due to othre index, or because the outer loop generates
104751 ** a unique result. Any of the first nEqCol columns of pIdx may be missing
104752 ** from the ORDER BY clause and the match can still be a success.
104753 **
104754 ** The *pbRev value is set to 0 order 1 depending on whether or not
104755 ** pIdx should be run in the forward order or in reverse order.
104756 */
104757 static int isSortingIndex(
104758 WhereBestIdx *p, /* Best index search context */
104759 Index *pIdx, /* The index we are testing */
104760 int base, /* Cursor number for the table to be sorted */
104761 int nEqCol, /* Number of index columns with ordered == constraints */
104762 int wsFlags, /* Index usages flags */
104763 int bOuterRev, /* True if outer loops scan in reverse order */
104764 int *pbRev /* Set to 1 for reverse-order scan of pIdx */
104765 ){
104766 int i; /* Number of pIdx terms used */
104767 int j; /* Number of ORDER BY terms satisfied */
104768 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
104769 int nTerm; /* Number of ORDER BY terms */
104770 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
104771 ExprList *pOrderBy; /* The ORDER BY clause */
104772 Parse *pParse = p->pParse; /* Parser context */
104773 sqlite3 *db = pParse->db; /* Database connection */
104774 int nPriorSat; /* ORDER BY terms satisfied by outer loops */
104775 int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
104776 int nEqOneRow; /* Idx columns that ref unique values */
104777
104778 if( p->i==0 ){
104779 nPriorSat = 0;
104780 }else{
104781 nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
104782 if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return nPriorSat;
104783 }
104784 if( nEqCol==0 ){
104785 if( p->i && (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
104786 return nPriorSat;
104787 }
104788 nEqOneRow = 0;
104789 }else if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
104790 nEqOneRow = nEqCol;
104791 }else{
104792 sortOrder = bOuterRev;
104793 nEqOneRow = -1;
104794 }
104795 pOrderBy = p->pOrderBy;
104796 assert( pOrderBy!=0 );
104797 if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
104798 if( pIdx->bUnordered ) return nPriorSat;
104799 nTerm = pOrderBy->nExpr;
104800 assert( nTerm>0 );
104801
104802 /* Argument pIdx must either point to a 'real' named index structure,
104803 ** or an index structure allocated on the stack by bestBtreeIndex() to
104804 ** represent the rowid index that is part of every table. */
104805 assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
104806
104807 /* Match terms of the ORDER BY clause against columns of
104808 ** the index.
104809 **
104810 ** Note that indices have pIdx->nColumn regular columns plus
104811 ** one additional column containing the rowid. The rowid column
104812 ** of the index is also allowed to match against the ORDER BY
104813 ** clause.
104814 */
104815 for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm; i++){
104816 Expr *pExpr; /* The expression of the ORDER BY pTerm */
104817 CollSeq *pColl; /* The collating sequence of pExpr */
104818 int termSortOrder; /* Sort order for this term */
104819 int iColumn; /* The i-th column of the index. -1 for rowid */
104820 int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
104821 const char *zColl; /* Name of the collating sequence for i-th index term */
104822
104823 assert( i<=pIdx->nColumn );
104824 pExpr = pTerm->pExpr;
104825 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
104826 /* Can not use an index sort on anything that is not a column in the
104827 ** left-most table of the FROM clause */
104828 break;
104829 }
104830 pColl = sqlite3ExprCollSeq(pParse, pExpr);
104831 if( !pColl ){
104832 pColl = db->pDfltColl;
104833 }
104834 if( pIdx->zName && i<pIdx->nColumn ){
104835 iColumn = pIdx->aiColumn[i];
104836 if( iColumn==pIdx->pTable->iPKey ){
104837 iColumn = -1;
104838 }
104839 iSortOrder = pIdx->aSortOrder[i];
104840 zColl = pIdx->azColl[i];
104841 }else{
104842 iColumn = -1;
104843 iSortOrder = 0;
104844 zColl = pColl->zName;
104845 }
104846 if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
104847 /* Term j of the ORDER BY clause does not match column i of the index */
104848 if( i<nEqCol ){
104849 /* If an index column that is constrained by == fails to match an
104850 ** ORDER BY term, that is OK. Just ignore that column of the index
104851 */
104852 continue;
104853 }else if( i==pIdx->nColumn ){
104854 /* Index column i is the rowid. All other terms match. */
104855 break;
104856 }else{
104857 /* If an index column fails to match and is not constrained by ==
104858 ** then the index cannot satisfy the ORDER BY constraint.
104859 */
104860 return nPriorSat;
104861 }
104862 }
104863 assert( pIdx->aSortOrder!=0 || iColumn==-1 );
104864 assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
104865 assert( iSortOrder==0 || iSortOrder==1 );
104866 termSortOrder = iSortOrder ^ pTerm->sortOrder;
104867 if( i>nEqOneRow ){
104868 if( termSortOrder!=sortOrder ){
104869 /* Indices can only be used if all ORDER BY terms past the
104870 ** equality constraints have the correct DESC or ASC. */
104871 break;
104872 }
104873 }else{
104874 sortOrder = termSortOrder;
104875 }
104876 j++;
104877 pTerm++;
104878 if( iColumn<0 ){
104879 seenRowid = 1;
104880 break;
104881 }
104882 }
104883 *pbRev = sortOrder;
104884
104885 /* If there was an "ORDER BY rowid" term that matched, or it is only
104886 ** possible for a single row from this table to match, then skip over
104887 ** any additional ORDER BY terms dealing with this table.
104888 */
104889 if( seenRowid ||
104890 ( (wsFlags & WHERE_COLUMN_NULL)==0
104891 && i>=pIdx->nColumn
104892 && indexIsUniqueNotNull(pIdx, nEqCol)
104893 )
104894 ){
104895 /* Advance j over additional ORDER BY terms associated with base */
104896 WhereMaskSet *pMS = p->pWC->pMaskSet;
104897 Bitmask m = ~getMask(pMS, base);
104898 while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
104899 j++;
104900 }
104901 }
104902 return j;
104903 }
104904
104905 /*
104906 ** Find the best query plan for accessing a particular table. Write the
104907 ** best query plan and its cost into the p->cost.
104908 **
@@ -104902,22 +104993,20 @@
104993
104994 /* Loop over all indices looking for the best one to use
104995 */
104996 for(; pProbe; pIdx=pProbe=pProbe->pNext){
104997 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104998 WhereCost pc; /* Cost of using pProbe */
 
104999 double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
105000 int bRev = 2; /* 0=forward scan. 1=reverse. 2=undecided */
105001 memset(&pc, 0, sizeof(pc));
 
105002
105003 /* The following variables are populated based on the properties of
105004 ** index being evaluated. They are then used to determine the expected
105005 ** cost and number of rows returned.
105006 **
105007 ** pc.plan.nEq:
105008 ** Number of equality terms that can be implemented using the index.
105009 ** In other words, the number of initial fields in the index that
105010 ** are used in == or IN or NOT NULL constraints of the WHERE clause.
105011 **
105012 ** nInMul:
@@ -104960,11 +105049,11 @@
105049 ** bSort:
105050 ** Boolean. True if there is an ORDER BY clause that will require an
105051 ** external sort (i.e. scanning the index being evaluated will not
105052 ** correctly order records).
105053 **
105054 ** bDist:
105055 ** Boolean. True if there is a DISTINCT clause that will require an
105056 ** external btree.
105057 **
105058 ** bLookup:
105059 ** Boolean. True if a table lookup is required for each index entry
@@ -104979,132 +105068,146 @@
105068 ** both available in the index.
105069 **
105070 ** SELECT a, b FROM tbl WHERE a = 1;
105071 ** SELECT a, b, c FROM tbl WHERE a = 1;
105072 */
 
105073 int nOrdered; /* Number of ordered terms matching index */
105074 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
105075 int nInMul = 1; /* Number of distinct equalities to lookup */
105076 double rangeDiv = (double)1; /* Estimated reduction in search space */
105077 int nBound = 0; /* Number of range constraints seen */
105078 int bSort; /* True if external sort required */
105079 int bDist; /* True if index cannot help with DISTINCT */
105080 int bLookup = 0; /* True if not a covering index */
105081 int nPriorSat; /* ORDER BY terms satisfied by outer loops */
105082 int nOrderBy; /* Number of ORDER BY terms */
105083 WhereTerm *pTerm; /* A single term of the WHERE clause */
105084 #ifdef SQLITE_ENABLE_STAT3
105085 WhereTerm *pFirstTerm = 0; /* First term matching the index */
105086 #endif
105087
105088 nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105089 if( p->i ){
105090 nPriorSat = pc.plan.nOBSat = p->aLevel[p->i-1].plan.nOBSat;
105091 bSort = nPriorSat<nOrderBy;
105092 bDist = 0;
105093 }else{
105094 nPriorSat = pc.plan.nOBSat = 0;
105095 bSort = nOrderBy>0;
105096 bDist = p->pDistinct!=0;
105097 }
105098
105099 /* Determine the values of pc.plan.nEq and nInMul */
105100 for(pc.plan.nEq=nOrdered=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
105101 int j = pProbe->aiColumn[pc.plan.nEq];
105102 pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
105103 if( pTerm==0 ) break;
105104 pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
105105 testcase( pTerm->pWC!=pWC );
105106 if( pTerm->eOperator & WO_IN ){
105107 Expr *pExpr = pTerm->pExpr;
105108 pc.plan.wsFlags |= WHERE_COLUMN_IN;
105109 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
105110 /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
105111 nInMul *= 25;
105112 bInEst = 1;
105113 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
105114 /* "x IN (value, value, ...)" */
105115 nInMul *= pExpr->x.pList->nExpr;
105116 }
105117 }else if( pTerm->eOperator & WO_ISNULL ){
105118 pc.plan.wsFlags |= WHERE_COLUMN_NULL;
105119 if( pc.plan.nEq==nOrdered ) nOrdered++;
105120 }else if( bSort && pc.plan.nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105121 nOrdered++;
105122 }
105123 #ifdef SQLITE_ENABLE_STAT3
105124 if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105125 #endif
105126 pc.used |= pTerm->prereqRight;
105127 }
105128
105129 /* If the index being considered is UNIQUE, and there is an equality
105130 ** constraint for all columns in the index, then this search will find
105131 ** at most a single row. In this case set the WHERE_UNIQUE flag to
105132 ** indicate this to the caller.
105133 **
105134 ** Otherwise, if the search may find more than one row, test to see if
105135 ** there is a range constraint on indexed column (pc.plan.nEq+1) that can be
105136 ** optimized using the index.
105137 */
105138 if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105139 testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
105140 testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
105141 if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105142 pc.plan.wsFlags |= WHERE_UNIQUE;
105143 if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
105144 pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
105145 }
105146 }
105147 }else if( pProbe->bUnordered==0 ){
105148 int j;
105149 j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
105150 if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105151 WhereTerm *pTop, *pBtm;
105152 pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
105153 pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
105154 whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
105155 if( pTop ){
105156 nBound = 1;
105157 pc.plan.wsFlags |= WHERE_TOP_LIMIT;
105158 pc.used |= pTop->prereqRight;
105159 testcase( pTop->pWC!=pWC );
105160 }
105161 if( pBtm ){
105162 nBound++;
105163 pc.plan.wsFlags |= WHERE_BTM_LIMIT;
105164 pc.used |= pBtm->prereqRight;
105165 testcase( pBtm->pWC!=pWC );
105166 }
105167 pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
105168 }
105169 }
105170
105171 /* If there is an ORDER BY clause and the index being considered will
105172 ** naturally scan rows in the required order, set the appropriate flags
105173 ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
105174 ** the index will scan rows in a different order, set the bSort
105175 ** variable. */
105176 assert( bRev>=0 && bRev<=2 );
105177 if( bSort ){
105178 testcase( bRev==0 );
105179 testcase( bRev==1 );
105180 testcase( bRev==2 );
105181 pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105182 pc.plan.wsFlags, bRev&1, &bRev);
105183 if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_UNIQUE)!=0 ){
105184 pc.plan.wsFlags |= WHERE_ORDERED;
105185 }
105186 if( nOrderBy==pc.plan.nOBSat ){
105187 bSort = 0;
105188 pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
105189 }
105190 if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
105191 }
105192
105193 /* If there is a DISTINCT qualifier and this index will scan rows in
105194 ** order of the DISTINCT expressions, clear bDist and set the appropriate
105195 ** flags in pc.plan.wsFlags. */
105196 if( bDist
105197 && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
105198 && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
105199 ){
105200 bDist = 0;
105201 pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
105202 }
105203
105204 /* If currently calculating the cost of using an index (not the IPK
105205 ** index), determine if all required column data may be obtained without
105206 ** using the main table (i.e. if the index is a covering
105207 ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
105208 ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true. */
105209 if( pIdx ){
105210 Bitmask m = pSrc->colUsed;
105211 int j;
105212 for(j=0; j<pIdx->nColumn; j++){
105213 int x = pIdx->aiColumn[j];
@@ -105111,51 +105214,54 @@
105214 if( x<BMS-1 ){
105215 m &= ~(((Bitmask)1)<<x);
105216 }
105217 }
105218 if( m==0 ){
105219 pc.plan.wsFlags |= WHERE_IDX_ONLY;
105220 }else{
105221 bLookup = 1;
105222 }
105223 }
105224
105225 /*
105226 ** Estimate the number of rows of output. For an "x IN (SELECT...)"
105227 ** constraint, do not let the estimate exceed half the rows in the table.
105228 */
105229 pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
105230 if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
105231 pc.plan.nRow = aiRowEst[0]/2;
105232 nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
105233 }
105234
105235 #ifdef SQLITE_ENABLE_STAT3
105236 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
105237 ** and we do not think that values of x are unique and if histogram
105238 ** data is available for column x, then it might be possible
105239 ** to get a better estimate on the number of rows based on
105240 ** VALUE and how common that value is according to the histogram.
105241 */
105242 if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
105243 && pFirstTerm!=0 && aiRowEst[1]>1 ){
105244 assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
105245 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
105246 testcase( pFirstTerm->eOperator==WO_EQ );
105247 testcase( pFirstTerm->eOperator==WO_ISNULL );
105248 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
105249 &pc.plan.nRow);
105250 }else if( bInEst==0 ){
105251 assert( pFirstTerm->eOperator==WO_IN );
105252 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
105253 &pc.plan.nRow);
105254 }
105255 }
105256 #endif /* SQLITE_ENABLE_STAT3 */
105257
105258 /* Adjust the number of output rows and downward to reflect rows
105259 ** that are excluded by range constraints.
105260 */
105261 pc.plan.nRow = pc.plan.nRow/rangeDiv;
105262 if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
105263
105264 /* Experiments run on real SQLite databases show that the time needed
105265 ** to do a binary search to locate a row in a table or index is roughly
105266 ** log10(N) times the time to move from one row to the next row within
105267 ** a table or index. The actual times can vary, with the size of
@@ -105166,57 +105272,58 @@
105272 ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
105273 ** not give us data on the relative sizes of table and index records.
105274 ** So this computation assumes table records are about twice as big
105275 ** as index records
105276 */
105277 if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED))==WHERE_IDX_ONLY
105278 && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
105279 && sqlite3GlobalConfig.bUseCis
105280 && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
105281 ){
105282 /* This index is not useful for indexing, but it is a covering index.
105283 ** A full-scan of the index might be a little faster than a full-scan
105284 ** of the table, so give this case a cost slightly less than a table
105285 ** scan. */
105286 pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
105287 pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
105288 }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
105289 /* The cost of a full table scan is a number of move operations equal
105290 ** to the number of rows in the table.
105291 **
105292 ** We add an additional 4x penalty to full table scans. This causes
105293 ** the cost function to err on the side of choosing an index over
105294 ** choosing a full scan. This 4x full-scan penalty is an arguable
105295 ** decision and one which we expect to revisit in the future. But
105296 ** it seems to be working well enough at the moment.
105297 */
105298 pc.rCost = aiRowEst[0]*4;
105299 pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
105300 if( pIdx ) pc.plan.wsFlags &= ~WHERE_ORDERED;
105301 }else{
105302 log10N = estLog(aiRowEst[0]);
105303 pc.rCost = pc.plan.nRow;
105304 if( pIdx ){
105305 if( bLookup ){
105306 /* For an index lookup followed by a table lookup:
105307 ** nInMul index searches to find the start of each index range
105308 ** + nRow steps through the index
105309 ** + nRow table searches to lookup the table entry using the rowid
105310 */
105311 pc.rCost += (nInMul + pc.plan.nRow)*log10N;
105312 }else{
105313 /* For a covering index:
105314 ** nInMul index searches to find the initial entry
105315 ** + nRow steps through the index
105316 */
105317 pc.rCost += nInMul*log10N;
105318 }
105319 }else{
105320 /* For a rowid primary key lookup:
105321 ** nInMult table searches to find the initial entry for each range
105322 ** + nRow steps through the table
105323 */
105324 pc.rCost += nInMul*log10N;
105325 }
105326 }
105327
105328 /* Add in the estimated cost of sorting the result. Actual experimental
105329 ** measurements of sorting performance in SQLite show that sorting time
@@ -105223,14 +105330,16 @@
105330 ** adds C*N*log10(N) to the cost, where N is the number of rows to be
105331 ** sorted and C is a factor between 1.95 and 4.3. We will split the
105332 ** difference and select C of 3.0.
105333 */
105334 if( bSort ){
105335 double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
105336 m *= (double)(pc.plan.nOBSat ? 2 : 3);
105337 pc.rCost += pc.plan.nRow*m;
105338 }
105339 if( bDist ){
105340 pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
105341 }
105342
105343 /**** Cost of using this index has now been computed ****/
105344
105345 /* If there are additional constraints on this table that cannot
@@ -105247,29 +105356,29 @@
105356 ** tables that are not in outer loops. If notReady is used here instead
105357 ** of notValid, then a optimal index that depends on inner joins loops
105358 ** might be selected even when there exists an optimal index that has
105359 ** no such dependency.
105360 */
105361 if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
105362 int k; /* Loop counter */
105363 int nSkipEq = pc.plan.nEq; /* Number of == constraints to skip */
105364 int nSkipRange = nBound; /* Number of < constraints to skip */
105365 Bitmask thisTab; /* Bitmap for pSrc */
105366
105367 thisTab = getMask(pWC->pMaskSet, iCur);
105368 for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
105369 if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105370 if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
105371 if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105372 if( nSkipEq ){
105373 /* Ignore the first pc.plan.nEq equality matches since the index
105374 ** has already accounted for these */
105375 nSkipEq--;
105376 }else{
105377 /* Assume each additional equality match reduces the result
105378 ** set size by a factor of 10 */
105379 pc.plan.nRow /= 10;
105380 }
105381 }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
105382 if( nSkipRange ){
105383 /* Ignore the first nSkipRange range constraints since the index
105384 ** has already accounted for these */
@@ -105279,43 +105388,38 @@
105388 ** set size by a factor of 3. Indexed range constraints reduce
105389 ** the search space by a larger factor: 4. We make indexed range
105390 ** more selective intentionally because of the subjective
105391 ** observation that indexed range constraints really are more
105392 ** selective in practice, on average. */
105393 pc.plan.nRow /= 3;
105394 }
105395 }else if( pTerm->eOperator!=WO_NOOP ){
105396 /* Any other expression lowers the output row count by half */
105397 pc.plan.nRow /= 2;
105398 }
105399 }
105400 if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
105401 }
105402
105403
105404 WHERETRACE((
105405 "%s(%s):\n"
105406 " nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
105407 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
105408 " used=0x%llx nOrdered=%d nOBSat=%d\n",
105409 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105410 pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
105411 p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used, nOrdered,
105412 pc.plan.nOBSat
105413 ));
105414
105415 /* If this index is the best we have seen so far, then record this
105416 ** index and its cost in the p->cost structure.
105417 */
105418 if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
105419 p->cost = pc;
105420 p->cost.plan.wsFlags &= wsFlagMask;
 
 
 
 
 
 
105421 p->cost.plan.u.pIdx = pIdx;
105422 }
105423
105424 /* If there was an INDEXED BY clause, then only that one index is
105425 ** considered. */
@@ -105333,21 +105437,19 @@
105437 ** SQLite outputs rows in in the absence of an ORDER BY clause. */
105438 if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105439 p->cost.plan.wsFlags |= WHERE_REVERSE;
105440 }
105441
105442 assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
105443 assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105444 assert( pSrc->pIndex==0
105445 || p->cost.plan.u.pIdx==0
105446 || p->cost.plan.u.pIdx==pSrc->pIndex
105447 );
105448
105449 WHERETRACE(("best index is: %s\n",
105450 p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
 
 
105451
105452 bestOrClauseIndex(p);
105453 bestAutomaticIndex(p);
105454 p->cost.plan.wsFlags |= eqTermMask;
105455 }
@@ -106071,11 +106173,11 @@
106173 ** should not have a NULL value stored in 'x'. If column 'x' is
106174 ** the first one after the nEq equality constraints in the index,
106175 ** this requires some special handling.
106176 */
106177 if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
106178 && (pLevel->plan.wsFlags&WHERE_ORDERED)
106179 && (pIdx->nColumn>nEq)
106180 ){
106181 /* assert( pOrderBy->nExpr==1 ); */
106182 /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
106183 isMinQuery = 1;
@@ -106892,12 +106994,12 @@
106994 continue;
106995 }
106996 sWBI.notReady = (isOptimal ? m : sWBI.notValid);
106997 if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
106998
106999 WHERETRACE(("=== trying table %d (%s) with isOptimal=%d ===\n",
107000 j, sWBI.pSrc->pTab->zName, isOptimal));
107001 assert( sWBI.pSrc->pTab );
107002 #ifndef SQLITE_OMIT_VIRTUALTABLE
107003 if( IsVirtual(sWBI.pSrc->pTab) ){
107004 sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
107005 bestVirtualIndex(&sWBI);
@@ -106934,48 +107036,46 @@
107036 ** combination of INDEXED BY clauses are given. The error
107037 ** will be detected and relayed back to the application later.
107038 ** The NEVER() comes about because rule (2) above prevents
107039 ** An indexable full-table-scan from reaching rule (3).
107040 **
107041 ** (4) The plan cost must be lower than prior plans, where "cost"
107042 ** is defined by the compareCost() function above.
107043 */
107044 if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
107045 && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
107046 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
107047 || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
107048 && (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
107049 || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
107050 && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan)) /* (4) */
 
 
107051 ){
107052 WHERETRACE(("=== table %d (%s) is best so far\n"
107053 " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
107054 j, sWBI.pSrc->pTab->zName,
107055 sWBI.cost.rCost, sWBI.cost.plan.nRow,
107056 sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
107057 bestPlan = sWBI.cost;
107058 bestJ = j;
107059 }
107060 if( doNotReorder ) break;
107061 }
107062 }
107063 assert( bestJ>=0 );
107064 assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
107065 WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
107066 " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
107067 bestJ, pTabList->a[bestJ].pTab->zName,
107068 pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
107069 bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
 
 
 
107070 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
107071 assert( pWInfo->eDistinct==0 );
107072 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
107073 }
107074 andFlags &= bestPlan.plan.wsFlags;
107075 pLevel->plan = bestPlan.plan;
107076 pLevel->iTabCur = pTabList->a[bestJ].iCursor;
107077 testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
107078 testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
107079 if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
107080 if( (wctrlFlags & WHERE_ONETABLE_ONLY)
107081 && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0
@@ -107013,15 +107113,22 @@
107113 }
107114 WHERETRACE(("*** Optimizer Finished ***\n"));
107115 if( pParse->nErr || db->mallocFailed ){
107116 goto whereBeginError;
107117 }
107118 if( nTabList ){
107119 pLevel--;
107120 pWInfo->nOBSat = pLevel->plan.nOBSat;
107121 }else{
107122 pWInfo->nOBSat = 0;
107123 }
107124
107125 /* If the total query only selects a single row, then the ORDER BY
107126 ** clause is irrelevant.
107127 */
107128 if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
107129 assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
107130 pWInfo->nOBSat = pOrderBy->nExpr;
107131 }
107132
107133 /* If the caller is an UPDATE or DELETE statement that is requesting
107134 ** to use a one-pass algorithm, determine if this is appropriate.
@@ -107045,11 +107152,10 @@
107152 int iDb; /* Index of database containing table/index */
107153 struct SrcList_item *pTabItem;
107154
107155 pTabItem = &pTabList->a[pLevel->iFrom];
107156 pTab = pTabItem->pTab;
 
107157 pWInfo->nRowOut *= pLevel->plan.nRow;
107158 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107159 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
107160 /* Do nothing */
107161 }else
@@ -114978,11 +115084,11 @@
115084 ** with various optimizations disabled to verify that the same answer
115085 ** is obtained in every case.
115086 */
115087 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
115088 sqlite3 *db = va_arg(ap, sqlite3*);
115089 db->dbOptFlags = (u8)(va_arg(ap, int) & 0xff);
115090 break;
115091 }
115092
115093 #ifdef SQLITE_N_KEYWORD
115094 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
@@ -135232,11 +135338,11 @@
135338 /*
135339 ** Remove the entry with rowid=iDelete from the r-tree structure.
135340 */
135341 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
135342 int rc; /* Return code */
135343 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
135344 int iCell; /* Index of iDelete cell in pLeaf */
135345 RtreeNode *pRoot; /* Root node of rtree structure */
135346
135347
135348 /* Obtain a reference to the root node to initialise Rtree.iDepth */
@@ -135435,11 +135541,11 @@
135541 ** (azData[2]..azData[argc-1]) contain a new record to insert into
135542 ** the r-tree structure.
135543 */
135544 if( rc==SQLITE_OK && nData>1 ){
135545 /* Insert the new record into the r-tree */
135546 RtreeNode *pLeaf = 0;
135547
135548 /* Figure out the rowid of the new row. */
135549 if( bHaveRowid==0 ){
135550 rc = newRowid(pRtree, &cell.iRowid);
135551 }
135552
+13 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.15"
111111
#define SQLITE_VERSION_NUMBER 3007015
112
-#define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
112
+#define SQLITE_SOURCE_ID "2012-10-03 12:56:18 956e4d7f8958e7065ff2d61cd71519d6f4113d4a"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -855,10 +855,21 @@
855855
** that the VFS encountered an error while handling the [PRAGMA] and the
856856
** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
857857
** file control occurs at the beginning of pragma statement analysis and so
858858
** it is able to override built-in [PRAGMA] statements.
859859
** </ul>
860
+**
861
+** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
862
+** ^This file-control may be invoked by SQLite on the database file handle
863
+** shortly after it is opened in order to provide a custom VFS with access
864
+** to the connections busy-handler callback. The argument is of type (void **)
865
+** - an array of two (void *) values. The first (void *) actually points
866
+** to a function of type (int (*)(void *)). In order to invoke the connections
867
+** busy-handler, this function should be invoked with the second (void *) in
868
+** the array as the only argument. If it returns non-zero, then the operation
869
+** should be retried. If it returns zero, the custom VFS should abandon the
870
+** current operation.
860871
*/
861872
#define SQLITE_FCNTL_LOCKSTATE 1
862873
#define SQLITE_GET_LOCKPROXYFILE 2
863874
#define SQLITE_SET_LOCKPROXYFILE 3
864875
#define SQLITE_LAST_ERRNO 4
@@ -870,10 +881,11 @@
870881
#define SQLITE_FCNTL_PERSIST_WAL 10
871882
#define SQLITE_FCNTL_OVERWRITE 11
872883
#define SQLITE_FCNTL_VFSNAME 12
873884
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
874885
#define SQLITE_FCNTL_PRAGMA 14
886
+#define SQLITE_FCNTL_BUSYHANDLER 15
875887
876888
/*
877889
** CAPI3REF: Mutex Handle
878890
**
879891
** The mutex module within SQLite defines [sqlite3_mutex] to be an
880892
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.15"
111 #define SQLITE_VERSION_NUMBER 3007015
112 #define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -855,10 +855,21 @@
855 ** that the VFS encountered an error while handling the [PRAGMA] and the
856 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
857 ** file control occurs at the beginning of pragma statement analysis and so
858 ** it is able to override built-in [PRAGMA] statements.
859 ** </ul>
 
 
 
 
 
 
 
 
 
 
 
860 */
861 #define SQLITE_FCNTL_LOCKSTATE 1
862 #define SQLITE_GET_LOCKPROXYFILE 2
863 #define SQLITE_SET_LOCKPROXYFILE 3
864 #define SQLITE_LAST_ERRNO 4
@@ -870,10 +881,11 @@
870 #define SQLITE_FCNTL_PERSIST_WAL 10
871 #define SQLITE_FCNTL_OVERWRITE 11
872 #define SQLITE_FCNTL_VFSNAME 12
873 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
874 #define SQLITE_FCNTL_PRAGMA 14
 
875
876 /*
877 ** CAPI3REF: Mutex Handle
878 **
879 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
880
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.15"
111 #define SQLITE_VERSION_NUMBER 3007015
112 #define SQLITE_SOURCE_ID "2012-10-03 12:56:18 956e4d7f8958e7065ff2d61cd71519d6f4113d4a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -855,10 +855,21 @@
855 ** that the VFS encountered an error while handling the [PRAGMA] and the
856 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
857 ** file control occurs at the beginning of pragma statement analysis and so
858 ** it is able to override built-in [PRAGMA] statements.
859 ** </ul>
860 **
861 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
862 ** ^This file-control may be invoked by SQLite on the database file handle
863 ** shortly after it is opened in order to provide a custom VFS with access
864 ** to the connections busy-handler callback. The argument is of type (void **)
865 ** - an array of two (void *) values. The first (void *) actually points
866 ** to a function of type (int (*)(void *)). In order to invoke the connections
867 ** busy-handler, this function should be invoked with the second (void *) in
868 ** the array as the only argument. If it returns non-zero, then the operation
869 ** should be retried. If it returns zero, the custom VFS should abandon the
870 ** current operation.
871 */
872 #define SQLITE_FCNTL_LOCKSTATE 1
873 #define SQLITE_GET_LOCKPROXYFILE 2
874 #define SQLITE_SET_LOCKPROXYFILE 3
875 #define SQLITE_LAST_ERRNO 4
@@ -870,10 +881,11 @@
881 #define SQLITE_FCNTL_PERSIST_WAL 10
882 #define SQLITE_FCNTL_OVERWRITE 11
883 #define SQLITE_FCNTL_VFSNAME 12
884 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
885 #define SQLITE_FCNTL_PRAGMA 14
886 #define SQLITE_FCNTL_BUSYHANDLER 15
887
888 /*
889 ** CAPI3REF: Mutex Handle
890 **
891 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
892

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button