Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a53499

Browse files
author
drh
committed
Enhance the DELETE logic so that it can make use of WHERE_ONEPASS_DESIRED
for rowid tables. FossilOrigin-Name: 8f479a72758ab6fedb171ada612b1963143c32fa
1 parent aedfc50 commit 6a53499

File tree

7 files changed

+101
-72
lines changed

7 files changed

+101
-72
lines changed

manifest

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Fully\sconstraint\sthe\sORDER\sBY\son\sthe\stop-10\sline\sof\sthe\s--summary\soutput\nfrom\sthe\swordcount\stest\sprogram.\s\sAdd\sthe\srun-wordcount.bash\sscript\sfor\nrunning\swordcount\sin\svarious\sconfigurations.
2-
D 2013-11-16T15:35:18.956
1+
C Enhance\sthe\sDELETE\slogic\sso\sthat\sit\scan\smake\suse\sof\sWHERE_ONEPASS_DESIRED\nfor\srowid\stables.
2+
D 2013-11-16T20:13:39.579
33
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
44
F Makefile.in 8a07bebafbfda0eb67728f4bd15a36201662d1a1
55
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -174,7 +174,7 @@ F src/callback.c f99a8957ba2adf369645fac0db09ad8adcf1caa2
174174
F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
175175
F src/ctime.c ea4b7f3623a0fcb1146e7f245d7410033e86859c
176176
F src/date.c 593c744b2623971e45affd0bde347631bdfa4625
177-
F src/delete.c ddb92f44595366c4817e576b5f11cad5a915c3ef
177+
F src/delete.c 765215277172cd66123516b92c99804bfab490f1
178178
F src/expr.c 1a295d8b0a2ba08919ad9300ebf7b67988ff4030
179179
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
180180
F src/fkey.c 78364daed38e26269c53ddb94c515bceac1063c6
@@ -183,7 +183,7 @@ F src/global.c 5caf4deab621abb45b4c607aad1bd21c20aac759
183183
F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
184184
F src/hash.h 8890a25af81fb85a9ad7790d32eedab4b994da22
185185
F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08
186-
F src/insert.c 63003c569c55793c3278ad7634bdd0c49d755a47
186+
F src/insert.c 3cf8012325857d162f74389420b14be7976a538d
187187
F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
188188
F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12
189189
F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b
@@ -213,7 +213,7 @@ F src/parse.y 073a8294e1826f1b1656e84806b77e4199f4bb57
213213
F src/pcache.c f8043b433a57aba85384a531e3937a804432a346
214214
F src/pcache.h a5e4f5d9f5d592051d91212c5949517971ae6222
215215
F src/pcache1.c a467393909a4ed7ca9de066d85ba5c5b04a5be63
216-
F src/pragma.c c8d70c47ec8d8ba93575d92e34d30ddff8e9b517
216+
F src/pragma.c 5ab7279d132143feb77f773688a24ab05da75fd7
217217
F src/prepare.c 359d1a1e9c9bd4488e4dd3a1aaaf2d2ebb9bb768
218218
F src/printf.c da9119eb31a187a4b99f60aa4a225141c0ebb74b
219219
F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68
@@ -224,7 +224,7 @@ F src/shell.c b98e74123d6c2e20369607c1da2d23c71db633d9
224224
F src/sqlite.h.in 4dedcab5b32358bf7a596badffe7363be1f1a82d
225225
F src/sqlite3.rc 11094cc6a157a028b301a9f06b3d03089ea37c3e
226226
F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc
227-
F src/sqliteInt.h e16eb6e62146234c05f34a4403a75cf242777ad7
227+
F src/sqliteInt.h 03b91c6bceccd7718473f3d790abcb8d5c2b1bf1
228228
F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
229229
F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158
230230
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -276,7 +276,7 @@ F src/test_vfstrace.c 34b544e80ba7fb77be15395a609c669df2e660a2
276276
F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
277277
F src/tokenize.c ec4c1a62b890bf1dbcdb966399e140b904c700a4
278278
F src/trigger.c d84e1f3669e9a217731a14a9d472b1c7b87c87ba
279-
F src/update.c 3de7e657b98ac67338d775c114a4068faf732402
279+
F src/update.c 6a45368f8c7b400ffdac9a4c6cd19a5bbf3385e2
280280
F src/utf.c 6fc6c88d50448c469c5c196acf21617a24f90269
281281
F src/util.c 2fa6c821d28bbdbeec1b2a7b091a281c9ef8f918
282282
F src/vacuum.c 3728d74919d4fb1356f9e9a13e27773db60b7179
@@ -1140,7 +1140,10 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
11401140
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
11411141
F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
11421142
F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
1143-
P de08a7e7abbad9b94d0268d096ef4555d31c8b0c
1144-
R 172f7957a55e2959255a9b6c3406c731
1143+
P 7edf39eb93a8f9059a788f5fccf41c2be40afd4d
1144+
R a6d07ee396f35147cc60e236a37bb900
1145+
T *branch * optimize-delete
1146+
T *sym-optimize-delete *
1147+
T -sym-trunk *
11451148
U drh
1146-
Z e8555787a9b5df5e1de6c7d36654ffb3
1149+
Z fab6ca68390544d72160beaeac98129b

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7edf39eb93a8f9059a788f5fccf41c2be40afd4d
1+
8f479a72758ab6fedb171ada612b1963143c32fa

src/delete.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,16 @@ void sqlite3DeleteFrom(
234234
int iTabCur; /* Cursor number for the table */
235235
int iDataCur; /* VDBE cursor for the canonical data source */
236236
int iIdxCur; /* Cursor number of the first index */
237+
int nIdx; /* Number of indices */
237238
sqlite3 *db; /* Main database structure */
238239
AuthContext sContext; /* Authorization context */
239240
NameContext sNC; /* Name context to resolve expressions in */
240241
int iDb; /* Database number */
241242
int memCnt = -1; /* Memory cell used for change counting */
242243
int rcauth; /* Value returned by authorization callback */
244+
int okOnePass; /* True for one-pass algorithm without the FIFO */
245+
int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
246+
u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
243247

244248
#ifndef SQLITE_OMIT_TRIGGER
245249
int isView; /* True if attempting to delete from a view */
@@ -295,11 +299,11 @@ void sqlite3DeleteFrom(
295299
}
296300
assert(!isView || pTrigger);
297301

298-
/* Assign cursor number to the table and all its indices.
302+
/* Assign cursor numbers to the table and all its indices.
299303
*/
300304
assert( pTabList->nSrc==1 );
301305
iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
302-
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
306+
for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
303307
pParse->nTab++;
304308
}
305309

@@ -399,8 +403,8 @@ void sqlite3DeleteFrom(
399403

400404
/* Open cursors for all indices of the table.
401405
*/
402-
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite,
403-
iTabCur, &iDataCur, &iIdxCur);
406+
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
407+
&iDataCur, &iIdxCur);
404408

405409
/* Loop over the primary keys to be deleted. */
406410
addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph);
@@ -424,22 +428,39 @@ void sqlite3DeleteFrom(
424428
** all rowids to be deleted into a RowSet.
425429
*/
426430
int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */
427-
int iRowid = ++pParse->nMem; /* Used for storing rowid values. */
428431
int regRowid; /* Actual register containing rowids */
429432

430433
/* Collect rowids of every row to be deleted.
431434
*/
432435
sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
433436
pWInfo = sqlite3WhereBegin(
434-
pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
437+
pParse, pTabList, pWhere, 0, 0,
438+
WHERE_DUPLICATES_OK|WHERE_ONEPASS_DESIRED, iTabCur+1
435439
);
436440
if( pWInfo==0 ) goto delete_from_cleanup;
437-
regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0);
438-
sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
441+
okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
439442
if( db->flags & SQLITE_CountRows ){
440443
sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
441444
}
445+
regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur,
446+
pParse->nMem+1, 0);
447+
if( regRowid>pParse->nMem ) pParse->nMem = regRowid;
448+
if( okOnePass ){
449+
aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
450+
if( aToOpen==0 ) goto delete_from_cleanup;
451+
memset(aToOpen, 1, nIdx+1);
452+
aToOpen[nIdx+1] = 0;
453+
if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
454+
if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
455+
addr = sqlite3VdbeAddOp0(v, OP_Goto);
456+
}else{
457+
sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
458+
}
442459
sqlite3WhereEnd(pWInfo);
460+
if( okOnePass ){
461+
sqlite3VdbeAddOp0(v, OP_Halt);
462+
sqlite3VdbeJumpHere(v, addr);
463+
}
443464

444465
/* Delete every item whose key was written to the list during the
445466
** database scan. We have to delete items after the scan is complete
@@ -451,32 +472,36 @@ void sqlite3DeleteFrom(
451472
** only effect this statement has is to fire the INSTEAD OF
452473
** triggers. */
453474
if( !isView ){
454-
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur,
475+
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
455476
&iDataCur, &iIdxCur);
456477
assert( iDataCur==iTabCur );
457478
assert( iIdxCur==iDataCur+1 );
458479
}
459480

460-
addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
481+
if( !okOnePass ){
482+
addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, regRowid);
483+
}
461484

462485
/* Delete the row */
463486
#ifndef SQLITE_OMIT_VIRTUALTABLE
464487
if( IsVirtual(pTab) ){
465488
const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
466489
sqlite3VtabMakeWritable(pParse, pTab);
467-
sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
490+
sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, regRowid, pVTab, P4_VTAB);
468491
sqlite3VdbeChangeP5(v, OE_Abort);
469492
sqlite3MayAbort(pParse);
470493
}else
471494
#endif
472495
{
473496
int count = (pParse->nested==0); /* True to count changes */
474497
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
475-
iRowid, 1, count, OE_Default, 0);
498+
regRowid, 1, count, OE_Default, okOnePass);
476499
}
477500

478501
/* End of the delete loop */
479-
sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
502+
if( !okOnePass ){
503+
sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
504+
}
480505
sqlite3VdbeResolveLabel(v, end);
481506

482507
/* Close the cursors open on the table and its indexes. */
@@ -510,6 +535,7 @@ void sqlite3DeleteFrom(
510535
sqlite3AuthContextPop(&sContext);
511536
sqlite3SrcListDelete(db, pTabList);
512537
sqlite3ExprDelete(db, pWhere);
538+
sqlite3DbFree(db, aToOpen);
513539
return;
514540
}
515541
/* Make sure "isView" and other macros defined above are undefined. Otherwise

src/insert.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ void sqlite3Insert(
820820
/* If this is not a view, open the table and and all indices */
821821
if( !isView ){
822822
int nIdx;
823-
nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1,
823+
nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
824824
&iDataCur, &iIdxCur);
825825
aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
826826
if( aRegIdx==0 ){
@@ -1680,16 +1680,19 @@ int sqlite3OpenTableAndIndices(
16801680
Table *pTab, /* Table to be opened */
16811681
int op, /* OP_OpenRead or OP_OpenWrite */
16821682
int iBase, /* Use this for the table cursor, if there is one */
1683+
u8 *aToOpen, /* If not NULL: boolean for each table and index */
16831684
int *piDataCur, /* Write the database source cursor number here */
16841685
int *piIdxCur /* Write the first index cursor number here */
16851686
){
16861687
int i;
16871688
int iDb;
1689+
int iDataCur;
16881690
Index *pIdx;
16891691
Vdbe *v;
16901692

16911693
assert( op==OP_OpenRead || op==OP_OpenWrite );
16921694
if( IsVirtual(pTab) ){
1695+
assert( aToOpen==0 );
16931696
*piDataCur = 0;
16941697
*piIdxCur = 1;
16951698
return 0;
@@ -1698,20 +1701,25 @@ int sqlite3OpenTableAndIndices(
16981701
v = sqlite3GetVdbe(pParse);
16991702
assert( v!=0 );
17001703
if( iBase<0 ) iBase = pParse->nTab;
1701-
if( HasRowid(pTab) ){
1702-
*piDataCur = iBase++;
1703-
sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op);
1704+
iDataCur = iBase++;
1705+
if( piDataCur ) *piDataCur = iDataCur;
1706+
if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
1707+
sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
17041708
}else{
17051709
sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
17061710
}
1707-
*piIdxCur = iBase;
1711+
if( piIdxCur ) *piIdxCur = iBase;
17081712
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
17091713
int iIdxCur = iBase++;
17101714
assert( pIdx->pSchema==pTab->pSchema );
1711-
if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur;
1712-
sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
1713-
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1714-
VdbeComment((v, "%s", pIdx->zName));
1715+
if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
1716+
*piDataCur = iIdxCur;
1717+
}
1718+
if( aToOpen==0 || aToOpen[i+1] ){
1719+
sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
1720+
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1721+
VdbeComment((v, "%s", pIdx->zName));
1722+
}
17151723
}
17161724
if( iBase>pParse->nTab ) pParse->nTab = iBase;
17171725
return i;

src/pragma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ void sqlite3Pragma(
18911891
sqlite3VdbeJumpHere(v, addr);
18921892
sqlite3ExprCacheClear(pParse);
18931893
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
1894-
1, &iDataCur, &iIdxCur);
1894+
1, 0, &iDataCur, &iIdxCur);
18951895
sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
18961896
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
18971897
sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */

src/sqliteInt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2942,7 +2942,7 @@ int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
29422942
void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
29432943
u8,u8,int,int*);
29442944
void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
2945-
int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*);
2945+
int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
29462946
void sqlite3BeginWriteOperation(Parse*, int, int);
29472947
void sqlite3MultiWrite(Parse*);
29482948
void sqlite3MayAbort(Parse*);

0 commit comments

Comments
 (0)