4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 #include "sqlite3expert.h"
18 #if !defined(SQLITE_AMALGAMATION)
19 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
20 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
22 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
23 # define ALWAYS(X) (1)
25 #elif !defined(NDEBUG)
26 # define ALWAYS(X) ((X)?1:(assert(0),0))
27 # define NEVER(X) ((X)?(assert(0),1):0)
29 # define ALWAYS(X) (X)
32 #endif /* !defined(SQLITE_AMALGAMATION) */
35 #ifndef SQLITE_OMIT_VIRTUALTABLE
37 typedef sqlite3_int64 i64
;
38 typedef sqlite3_uint64 u64
;
40 typedef struct IdxColumn IdxColumn
;
41 typedef struct IdxConstraint IdxConstraint
;
42 typedef struct IdxScan IdxScan
;
43 typedef struct IdxStatement IdxStatement
;
44 typedef struct IdxTable IdxTable
;
45 typedef struct IdxWrite IdxWrite
;
47 #define STRLEN (int)strlen
50 ** A temp table name that we assume no user database will actually use.
51 ** If this assumption proves incorrect triggers on the table with the
52 ** conflicting name will be ignored.
54 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
57 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
58 ** any other type of single-ended range constraint on a column).
61 ** Used to temporarily link IdxConstraint objects into lists while
62 ** creating candidate indexes.
64 struct IdxConstraint
{
65 char *zColl
; /* Collation sequence */
66 int bRange
; /* True for range, false for eq */
67 int iCol
; /* Constrained table column */
68 int bFlag
; /* Used by idxFindCompatible() */
69 int bDesc
; /* True if ORDER BY <expr> DESC */
70 IdxConstraint
*pNext
; /* Next constraint in pEq or pRange list */
71 IdxConstraint
*pLink
; /* See above */
75 ** A single scan of a single table.
78 IdxTable
*pTab
; /* Associated table object */
79 int iDb
; /* Database containing table zTable */
80 i64 covering
; /* Mask of columns required for cov. index */
81 IdxConstraint
*pOrder
; /* ORDER BY columns */
82 IdxConstraint
*pEq
; /* List of == constraints */
83 IdxConstraint
*pRange
; /* List of < constraints */
84 IdxScan
*pNextScan
; /* Next IdxScan object for same analysis */
88 ** Information regarding a single database table. Extracted from
89 ** "PRAGMA table_info" by function idxGetTableInfo().
98 char *zName
; /* Table name */
100 IdxTable
*pNext
; /* Next table in linked list of all tables */
104 ** An object of the following type is created for each unique table/write-op
105 ** seen. The objects are stored in a singly-linked list beginning at
106 ** sqlite3expert.pWrite.
110 int eOp
; /* SQLITE_UPDATE, DELETE or INSERT */
115 ** Each statement being analyzed is represented by an instance of this
118 struct IdxStatement
{
119 int iId
; /* Statement number */
120 char *zSql
; /* SQL statement */
121 char *zIdx
; /* Indexes */
122 char *zEQP
; /* Plan */
128 ** A hash table for storing strings. With space for a payload string
129 ** with each entry. Methods are:
136 #define IDX_HASH_SIZE 1023
137 typedef struct IdxHashEntry IdxHashEntry
;
138 typedef struct IdxHash IdxHash
;
139 struct IdxHashEntry
{
140 char *zKey
; /* nul-terminated key */
141 char *zVal
; /* nul-terminated value string */
142 char *zVal2
; /* nul-terminated value string 2 */
143 IdxHashEntry
*pHashNext
; /* Next entry in same hash bucket */
144 IdxHashEntry
*pNext
; /* Next entry in hash */
147 IdxHashEntry
*pFirst
;
148 IdxHashEntry
*aHash
[IDX_HASH_SIZE
];
152 ** sqlite3expert object.
154 struct sqlite3expert
{
155 int iSample
; /* Percentage of tables to sample for stat1 */
156 sqlite3
*db
; /* User database */
157 sqlite3
*dbm
; /* In-memory db for this analysis */
158 sqlite3
*dbv
; /* Vtab schema for this analysis */
159 IdxTable
*pTable
; /* List of all IdxTable objects */
160 IdxScan
*pScan
; /* List of scan objects */
161 IdxWrite
*pWrite
; /* List of write objects */
162 IdxStatement
*pStatement
; /* List of IdxStatement objects */
163 int bRun
; /* True once analysis has run */
165 int rc
; /* Error code from whereinfo hook */
166 IdxHash hIdx
; /* Hash containing all candidate indexes */
167 char *zCandidates
; /* For EXPERT_REPORT_CANDIDATES */
172 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
173 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
175 static void *idxMalloc(int *pRc
, int nByte
){
177 assert( *pRc
==SQLITE_OK
);
179 pRet
= sqlite3_malloc(nByte
);
181 memset(pRet
, 0, nByte
);
189 ** Initialize an IdxHash hash table.
191 static void idxHashInit(IdxHash
*pHash
){
192 memset(pHash
, 0, sizeof(IdxHash
));
196 ** Reset an IdxHash hash table.
198 static void idxHashClear(IdxHash
*pHash
){
200 for(i
=0; i
<IDX_HASH_SIZE
; i
++){
201 IdxHashEntry
*pEntry
;
203 for(pEntry
=pHash
->aHash
[i
]; pEntry
; pEntry
=pNext
){
204 pNext
= pEntry
->pHashNext
;
205 sqlite3_free(pEntry
->zVal2
);
206 sqlite3_free(pEntry
);
209 memset(pHash
, 0, sizeof(IdxHash
));
213 ** Return the index of the hash bucket that the string specified by the
214 ** arguments to this function belongs.
216 static int idxHashString(const char *z
, int n
){
217 unsigned int ret
= 0;
220 ret
+= (ret
<<3) + (unsigned char)(z
[i
]);
222 return (int)(ret
% IDX_HASH_SIZE
);
226 ** If zKey is already present in the hash table, return non-zero and do
227 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
228 ** the hash table passed as the second argument.
230 static int idxHashAdd(
236 int nKey
= STRLEN(zKey
);
237 int iHash
= idxHashString(zKey
, nKey
);
238 int nVal
= (zVal
? STRLEN(zVal
) : 0);
239 IdxHashEntry
*pEntry
;
241 for(pEntry
=pHash
->aHash
[iHash
]; pEntry
; pEntry
=pEntry
->pHashNext
){
242 if( STRLEN(pEntry
->zKey
)==nKey
&& 0==memcmp(pEntry
->zKey
, zKey
, nKey
) ){
246 pEntry
= idxMalloc(pRc
, sizeof(IdxHashEntry
) + nKey
+1 + nVal
+1);
248 pEntry
->zKey
= (char*)&pEntry
[1];
249 memcpy(pEntry
->zKey
, zKey
, nKey
);
251 pEntry
->zVal
= &pEntry
->zKey
[nKey
+1];
252 memcpy(pEntry
->zVal
, zVal
, nVal
);
254 pEntry
->pHashNext
= pHash
->aHash
[iHash
];
255 pHash
->aHash
[iHash
] = pEntry
;
257 pEntry
->pNext
= pHash
->pFirst
;
258 pHash
->pFirst
= pEntry
;
264 ** If zKey/nKey is present in the hash table, return a pointer to the
265 ** hash-entry object.
267 static IdxHashEntry
*idxHashFind(IdxHash
*pHash
, const char *zKey
, int nKey
){
269 IdxHashEntry
*pEntry
;
270 if( nKey
<0 ) nKey
= STRLEN(zKey
);
271 iHash
= idxHashString(zKey
, nKey
);
273 for(pEntry
=pHash
->aHash
[iHash
]; pEntry
; pEntry
=pEntry
->pHashNext
){
274 if( STRLEN(pEntry
->zKey
)==nKey
&& 0==memcmp(pEntry
->zKey
, zKey
, nKey
) ){
282 ** If the hash table contains an entry with a key equal to the string
283 ** passed as the final two arguments to this function, return a pointer
284 ** to the payload string. Otherwise, if zKey/nKey is not present in the
285 ** hash table, return NULL.
287 static const char *idxHashSearch(IdxHash
*pHash
, const char *zKey
, int nKey
){
288 IdxHashEntry
*pEntry
= idxHashFind(pHash
, zKey
, nKey
);
289 if( pEntry
) return pEntry
->zVal
;
294 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
295 ** variable to point to a copy of nul-terminated string zColl.
297 static IdxConstraint
*idxNewConstraint(int *pRc
, const char *zColl
){
299 int nColl
= STRLEN(zColl
);
301 assert( *pRc
==SQLITE_OK
);
302 pNew
= (IdxConstraint
*)idxMalloc(pRc
, sizeof(IdxConstraint
) * nColl
+ 1);
304 pNew
->zColl
= (char*)&pNew
[1];
305 memcpy(pNew
->zColl
, zColl
, nColl
+1);
311 ** An error associated with database handle db has just occurred. Pass
312 ** the error message to callback function xOut.
314 static void idxDatabaseError(
315 sqlite3
*db
, /* Database handle */
316 char **pzErrmsg
/* Write error here */
318 *pzErrmsg
= sqlite3_mprintf("%s", sqlite3_errmsg(db
));
322 ** Prepare an SQL statement.
324 static int idxPrepareStmt(
325 sqlite3
*db
, /* Database handle to compile against */
326 sqlite3_stmt
**ppStmt
, /* OUT: Compiled SQL statement */
327 char **pzErrmsg
, /* OUT: sqlite3_malloc()ed error message */
328 const char *zSql
/* SQL statement to compile */
330 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
333 idxDatabaseError(db
, pzErrmsg
);
339 ** Prepare an SQL statement using the results of a printf() formatting.
341 static int idxPrintfPrepareStmt(
342 sqlite3
*db
, /* Database handle to compile against */
343 sqlite3_stmt
**ppStmt
, /* OUT: Compiled SQL statement */
344 char **pzErrmsg
, /* OUT: sqlite3_malloc()ed error message */
345 const char *zFmt
, /* printf() format of SQL statement */
346 ... /* Trailing printf() arguments */
352 zSql
= sqlite3_vmprintf(zFmt
, ap
);
356 rc
= idxPrepareStmt(db
, ppStmt
, pzErrmsg
, zSql
);
364 /*************************************************************************
365 ** Beginning of virtual table implementation.
367 typedef struct ExpertVtab ExpertVtab
;
371 sqlite3expert
*pExpert
;
374 typedef struct ExpertCsr ExpertCsr
;
376 sqlite3_vtab_cursor base
;
380 static char *expertDequote(const char *zIn
){
382 char *zRet
= sqlite3_malloc(n
);
384 assert( zIn
[0]=='\'' );
385 assert( zIn
[n
-1]=='\'' );
390 for(iIn
=1; iIn
<(n
-1); iIn
++){
391 if( zIn
[iIn
]=='\'' ){
392 assert( zIn
[iIn
+1]=='\'' );
395 zRet
[iOut
++] = zIn
[iIn
];
404 ** This function is the implementation of both the xConnect and xCreate
405 ** methods of the r-tree virtual table.
407 ** argv[0] -> module name
408 ** argv[1] -> database name
409 ** argv[2] -> table name
410 ** argv[...] -> column names...
412 static int expertConnect(
415 int argc
, const char *const*argv
,
416 sqlite3_vtab
**ppVtab
,
419 sqlite3expert
*pExpert
= (sqlite3expert
*)pAux
;
424 *pzErr
= sqlite3_mprintf("internal error!");
427 char *zCreateTable
= expertDequote(argv
[3]);
429 rc
= sqlite3_declare_vtab(db
, zCreateTable
);
431 p
= idxMalloc(&rc
, sizeof(ExpertVtab
));
434 p
->pExpert
= pExpert
;
435 p
->pTab
= pExpert
->pTable
;
436 assert( sqlite3_stricmp(p
->pTab
->zName
, argv
[2])==0 );
438 sqlite3_free(zCreateTable
);
444 *ppVtab
= (sqlite3_vtab
*)p
;
448 static int expertDisconnect(sqlite3_vtab
*pVtab
){
449 ExpertVtab
*p
= (ExpertVtab
*)pVtab
;
454 static int expertBestIndex(sqlite3_vtab
*pVtab
, sqlite3_index_info
*pIdxInfo
){
455 ExpertVtab
*p
= (ExpertVtab
*)pVtab
;
460 SQLITE_INDEX_CONSTRAINT_EQ
| SQLITE_INDEX_CONSTRAINT_GT
|
461 SQLITE_INDEX_CONSTRAINT_LT
| SQLITE_INDEX_CONSTRAINT_GE
|
462 SQLITE_INDEX_CONSTRAINT_LE
;
464 pScan
= idxMalloc(&rc
, sizeof(IdxScan
));
468 /* Link the new scan object into the list */
469 pScan
->pTab
= p
->pTab
;
470 pScan
->pNextScan
= p
->pExpert
->pScan
;
471 p
->pExpert
->pScan
= pScan
;
473 /* Add the constraints to the IdxScan object */
474 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++){
475 struct sqlite3_index_constraint
*pCons
= &pIdxInfo
->aConstraint
[i
];
478 && p
->pTab
->aCol
[pCons
->iColumn
].iPk
==0
479 && (pCons
->op
& opmask
)
482 const char *zColl
= sqlite3_vtab_collation(pIdxInfo
, i
);
483 pNew
= idxNewConstraint(&rc
, zColl
);
485 pNew
->iCol
= pCons
->iColumn
;
486 if( pCons
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
487 pNew
->pNext
= pScan
->pEq
;
491 pNew
->pNext
= pScan
->pRange
;
492 pScan
->pRange
= pNew
;
496 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= n
;
500 /* Add the ORDER BY to the IdxScan object */
501 for(i
=pIdxInfo
->nOrderBy
-1; i
>=0; i
--){
502 int iCol
= pIdxInfo
->aOrderBy
[i
].iColumn
;
504 IdxConstraint
*pNew
= idxNewConstraint(&rc
, p
->pTab
->aCol
[iCol
].zColl
);
507 pNew
->bDesc
= pIdxInfo
->aOrderBy
[i
].desc
;
508 pNew
->pNext
= pScan
->pOrder
;
509 pNew
->pLink
= pScan
->pOrder
;
510 pScan
->pOrder
= pNew
;
517 pIdxInfo
->estimatedCost
= 1000000.0 / (n
+1);
521 static int expertUpdate(
524 sqlite3_value
**azData
,
535 ** Virtual table module xOpen method.
537 static int expertOpen(sqlite3_vtab
*pVTab
, sqlite3_vtab_cursor
**ppCursor
){
541 pCsr
= idxMalloc(&rc
, sizeof(ExpertCsr
));
542 *ppCursor
= (sqlite3_vtab_cursor
*)pCsr
;
547 ** Virtual table module xClose method.
549 static int expertClose(sqlite3_vtab_cursor
*cur
){
550 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
551 sqlite3_finalize(pCsr
->pData
);
557 ** Virtual table module xEof method.
559 ** Return non-zero if the cursor does not currently point to a valid
560 ** record (i.e if the scan has finished), or zero otherwise.
562 static int expertEof(sqlite3_vtab_cursor
*cur
){
563 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
564 return pCsr
->pData
==0;
568 ** Virtual table module xNext method.
570 static int expertNext(sqlite3_vtab_cursor
*cur
){
571 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
574 assert( pCsr
->pData
);
575 rc
= sqlite3_step(pCsr
->pData
);
576 if( rc
!=SQLITE_ROW
){
577 rc
= sqlite3_finalize(pCsr
->pData
);
587 ** Virtual table module xRowid method.
589 static int expertRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
596 ** Virtual table module xColumn method.
598 static int expertColumn(sqlite3_vtab_cursor
*cur
, sqlite3_context
*ctx
, int i
){
599 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
601 pVal
= sqlite3_column_value(pCsr
->pData
, i
);
603 sqlite3_result_value(ctx
, pVal
);
609 ** Virtual table module xFilter method.
611 static int expertFilter(
612 sqlite3_vtab_cursor
*cur
,
613 int idxNum
, const char *idxStr
,
614 int argc
, sqlite3_value
**argv
616 ExpertCsr
*pCsr
= (ExpertCsr
*)cur
;
617 ExpertVtab
*pVtab
= (ExpertVtab
*)(cur
->pVtab
);
618 sqlite3expert
*pExpert
= pVtab
->pExpert
;
625 rc
= sqlite3_finalize(pCsr
->pData
);
628 rc
= idxPrintfPrepareStmt(pExpert
->db
, &pCsr
->pData
, &pVtab
->base
.zErrMsg
,
629 "SELECT * FROM main.%Q WHERE sample()", pVtab
->pTab
->zName
634 rc
= expertNext(cur
);
639 static int idxRegisterVtab(sqlite3expert
*p
){
640 static sqlite3_module expertModule
= {
642 expertConnect
, /* xCreate - create a table */
643 expertConnect
, /* xConnect - connect to an existing table */
644 expertBestIndex
, /* xBestIndex - Determine search strategy */
645 expertDisconnect
, /* xDisconnect - Disconnect from a table */
646 expertDisconnect
, /* xDestroy - Drop a table */
647 expertOpen
, /* xOpen - open a cursor */
648 expertClose
, /* xClose - close a cursor */
649 expertFilter
, /* xFilter - configure scan constraints */
650 expertNext
, /* xNext - advance a cursor */
651 expertEof
, /* xEof */
652 expertColumn
, /* xColumn - read data */
653 expertRowid
, /* xRowid - read data */
654 expertUpdate
, /* xUpdate - write data */
655 0, /* xBegin - begin transaction */
656 0, /* xSync - sync transaction */
657 0, /* xCommit - commit transaction */
658 0, /* xRollback - rollback transaction */
659 0, /* xFindFunction - function overloading */
660 0, /* xRename - rename the table */
667 return sqlite3_create_module(p
->dbv
, "expert", &expertModule
, (void*)p
);
670 ** End of virtual table implementation.
671 *************************************************************************/
673 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
674 ** is called, set it to the return value of sqlite3_finalize() before
675 ** returning. Otherwise, discard the sqlite3_finalize() return value.
677 static void idxFinalize(int *pRc
, sqlite3_stmt
*pStmt
){
678 int rc
= sqlite3_finalize(pStmt
);
679 if( *pRc
==SQLITE_OK
) *pRc
= rc
;
683 ** Attempt to allocate an IdxTable structure corresponding to table zTab
684 ** in the main database of connection db. If successful, set (*ppOut) to
685 ** point to the new object and return SQLITE_OK. Otherwise, return an
686 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
687 ** set to point to an error string.
689 ** It is the responsibility of the caller to eventually free either the
690 ** IdxTable object or error message using sqlite3_free().
692 static int idxGetTableInfo(
693 sqlite3
*db
, /* Database connection to read details from */
694 const char *zTab
, /* Table name */
695 IdxTable
**ppOut
, /* OUT: New object (if successful) */
696 char **pzErrmsg
/* OUT: Error message (if not) */
698 sqlite3_stmt
*p1
= 0;
708 if( zTab
==0 ) return SQLITE_ERROR
;
710 nByte
= sizeof(IdxTable
) + nTab
+ 1;
711 rc
= idxPrintfPrepareStmt(db
, &p1
, pzErrmsg
, "PRAGMA table_xinfo=%Q", zTab
);
712 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(p1
) ){
713 const char *zCol
= (const char*)sqlite3_column_text(p1
, 1);
714 const char *zColSeq
= 0;
719 nByte
+= 1 + STRLEN(zCol
);
720 rc
= sqlite3_table_column_metadata(
721 db
, "main", zTab
, zCol
, 0, &zColSeq
, 0, 0, 0
723 if( zColSeq
==0 ) zColSeq
= "binary";
724 nByte
+= 1 + STRLEN(zColSeq
);
726 nPk
+= (sqlite3_column_int(p1
, 5)>0);
728 rc2
= sqlite3_reset(p1
);
729 if( rc
==SQLITE_OK
) rc
= rc2
;
731 nByte
+= sizeof(IdxColumn
) * nCol
;
733 pNew
= idxMalloc(&rc
, nByte
);
736 pNew
->aCol
= (IdxColumn
*)&pNew
[1];
738 pCsr
= (char*)&pNew
->aCol
[nCol
];
742 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(p1
) ){
743 const char *zCol
= (const char*)sqlite3_column_text(p1
, 1);
744 const char *zColSeq
= 0;
746 if( zCol
==0 ) continue;
747 nCopy
= STRLEN(zCol
) + 1;
748 pNew
->aCol
[nCol
].zName
= pCsr
;
749 pNew
->aCol
[nCol
].iPk
= (sqlite3_column_int(p1
, 5)==1 && nPk
==1);
750 memcpy(pCsr
, zCol
, nCopy
);
753 rc
= sqlite3_table_column_metadata(
754 db
, "main", zTab
, zCol
, 0, &zColSeq
, 0, 0, 0
757 if( zColSeq
==0 ) zColSeq
= "binary";
758 nCopy
= STRLEN(zColSeq
) + 1;
759 pNew
->aCol
[nCol
].zColl
= pCsr
;
760 memcpy(pCsr
, zColSeq
, nCopy
);
766 idxFinalize(&rc
, p1
);
771 }else if( ALWAYS(pNew
!=0) ){
773 if( ALWAYS(pNew
->zName
!=0) ) memcpy(pNew
->zName
, zTab
, nTab
+1);
781 ** This function is a no-op if *pRc is set to anything other than
782 ** SQLITE_OK when it is called.
784 ** If *pRc is initially set to SQLITE_OK, then the text specified by
785 ** the printf() style arguments is appended to zIn and the result returned
786 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
787 ** zIn before returning.
789 static char *idxAppendText(int *pRc
, char *zIn
, const char *zFmt
, ...){
793 int nIn
= zIn
? STRLEN(zIn
) : 0;
796 if( *pRc
==SQLITE_OK
){
797 zAppend
= sqlite3_vmprintf(zFmt
, ap
);
799 nAppend
= STRLEN(zAppend
);
800 zRet
= (char*)sqlite3_malloc(nIn
+ nAppend
+ 1);
802 if( zAppend
&& zRet
){
803 if( nIn
) memcpy(zRet
, zIn
, nIn
);
804 memcpy(&zRet
[nIn
], zAppend
, nAppend
+1);
810 sqlite3_free(zAppend
);
818 ** Return true if zId must be quoted in order to use it as an SQL
819 ** identifier, or false otherwise.
821 static int idxIdentifierRequiresQuotes(const char *zId
){
823 int nId
= STRLEN(zId
);
825 if( sqlite3_keyword_check(zId
, nId
) ) return 1;
827 for(i
=0; zId
[i
]; i
++){
829 && !(zId
[i
]>='0' && zId
[i
]<='9')
830 && !(zId
[i
]>='a' && zId
[i
]<='z')
831 && !(zId
[i
]>='A' && zId
[i
]<='Z')
840 ** This function appends an index column definition suitable for constraint
841 ** pCons to the string passed as zIn and returns the result.
843 static char *idxAppendColDefn(
844 int *pRc
, /* IN/OUT: Error code */
845 char *zIn
, /* Column defn accumulated so far */
846 IdxTable
*pTab
, /* Table index will be created on */
850 IdxColumn
*p
= &pTab
->aCol
[pCons
->iCol
];
851 if( zRet
) zRet
= idxAppendText(pRc
, zRet
, ", ");
853 if( idxIdentifierRequiresQuotes(p
->zName
) ){
854 zRet
= idxAppendText(pRc
, zRet
, "%Q", p
->zName
);
856 zRet
= idxAppendText(pRc
, zRet
, "%s", p
->zName
);
859 if( sqlite3_stricmp(p
->zColl
, pCons
->zColl
) ){
860 if( idxIdentifierRequiresQuotes(pCons
->zColl
) ){
861 zRet
= idxAppendText(pRc
, zRet
, " COLLATE %Q", pCons
->zColl
);
863 zRet
= idxAppendText(pRc
, zRet
, " COLLATE %s", pCons
->zColl
);
868 zRet
= idxAppendText(pRc
, zRet
, " DESC");
874 ** Search database dbm for an index compatible with the one idxCreateFromCons()
875 ** would create from arguments pScan, pEq and pTail. If no error occurs and
876 ** such an index is found, return non-zero. Or, if no such index is found,
879 ** If an error occurs, set *pRc to an SQLite error code and return zero.
881 static int idxFindCompatible(
882 int *pRc
, /* OUT: Error code */
883 sqlite3
* dbm
, /* Database to search */
884 IdxScan
*pScan
, /* Scan for table to search for index on */
885 IdxConstraint
*pEq
, /* List of == constraints */
886 IdxConstraint
*pTail
/* List of range constraints */
888 const char *zTbl
= pScan
->pTab
->zName
;
889 sqlite3_stmt
*pIdxList
= 0;
890 IdxConstraint
*pIter
;
891 int nEq
= 0; /* Number of elements in pEq */
894 /* Count the elements in list pEq */
895 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
) nEq
++;
897 rc
= idxPrintfPrepareStmt(dbm
, &pIdxList
, 0, "PRAGMA index_list=%Q", zTbl
);
898 while( rc
==SQLITE_OK
&& sqlite3_step(pIdxList
)==SQLITE_ROW
){
900 IdxConstraint
*pT
= pTail
;
901 sqlite3_stmt
*pInfo
= 0;
902 const char *zIdx
= (const char*)sqlite3_column_text(pIdxList
, 1);
903 if( zIdx
==0 ) continue;
905 /* Zero the IdxConstraint.bFlag values in the pEq list */
906 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
) pIter
->bFlag
= 0;
908 rc
= idxPrintfPrepareStmt(dbm
, &pInfo
, 0, "PRAGMA index_xInfo=%Q", zIdx
);
909 while( rc
==SQLITE_OK
&& sqlite3_step(pInfo
)==SQLITE_ROW
){
910 int iIdx
= sqlite3_column_int(pInfo
, 0);
911 int iCol
= sqlite3_column_int(pInfo
, 1);
912 const char *zColl
= (const char*)sqlite3_column_text(pInfo
, 4);
915 for(pIter
=pEq
; pIter
; pIter
=pIter
->pLink
){
916 if( pIter
->bFlag
) continue;
917 if( pIter
->iCol
!=iCol
) continue;
918 if( sqlite3_stricmp(pIter
->zColl
, zColl
) ) continue;
928 if( pT
->iCol
!=iCol
|| sqlite3_stricmp(pT
->zColl
, zColl
) ){
936 idxFinalize(&rc
, pInfo
);
938 if( rc
==SQLITE_OK
&& bMatch
){
939 sqlite3_finalize(pIdxList
);
943 idxFinalize(&rc
, pIdxList
);
949 /* Callback for sqlite3_exec() with query with leading count(*) column.
950 * The first argument is expected to be an int*, referent to be incremented
951 * if that leading column is not exactly '0'.
953 static int countNonzeros(void* pCount
, int nc
,
954 char* azResults
[], char* azColumns
[]){
955 (void)azColumns
; /* Suppress unused parameter warning */
956 if( nc
>0 && (azResults
[0][0]!='0' || azResults
[0][1]!=0) ){
957 *((int *)pCount
) += 1;
962 static int idxCreateFromCons(
968 sqlite3
*dbm
= p
->dbm
;
970 if( (pEq
|| pTail
) && 0==idxFindCompatible(&rc
, dbm
, pScan
, pEq
, pTail
) ){
971 IdxTable
*pTab
= pScan
->pTab
;
974 IdxConstraint
*pCons
;
978 for(pCons
=pEq
; pCons
; pCons
=pCons
->pLink
){
979 zCols
= idxAppendColDefn(&rc
, zCols
, pTab
, pCons
);
981 for(pCons
=pTail
; pCons
; pCons
=pCons
->pLink
){
982 zCols
= idxAppendColDefn(&rc
, zCols
, pTab
, pCons
);
986 /* Hash the list of columns to come up with a name for the index */
987 const char *zTable
= pScan
->pTab
->zName
;
988 int quoteTable
= idxIdentifierRequiresQuotes(zTable
);
989 char *zName
= 0; /* Index name */
994 for(i
=0; zCols
[i
]; i
++){
995 h
+= ((h
<<3) + zCols
[i
]);
998 zName
= sqlite3_mprintf("%s_idx_%08x", zTable
, h
);
999 if( zName
==0 ) break;
1000 /* Is is unique among table, view and index names? */
1001 zFmt
= "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
1002 " AND type in ('index','table','view')";
1003 zFind
= sqlite3_mprintf(zFmt
, zName
);
1005 rc
= sqlite3_exec(dbm
, zFind
, countNonzeros
, &i
, 0);
1006 assert(rc
==SQLITE_OK
);
1007 sqlite3_free(zFind
);
1013 }while( collisions
<50 && zName
!=0 );
1015 /* This return means "Gave up trying to find a unique index name." */
1016 rc
= SQLITE_BUSY_TIMEOUT
;
1017 }else if( zName
==0 ){
1021 zFmt
= "CREATE INDEX \"%w\" ON \"%w\"(%s)";
1023 zFmt
= "CREATE INDEX %s ON %s(%s)";
1025 zIdx
= sqlite3_mprintf(zFmt
, zName
, zTable
, zCols
);
1029 rc
= sqlite3_exec(dbm
, zIdx
, 0, 0, p
->pzErrmsg
);
1030 if( rc
!=SQLITE_OK
){
1031 rc
= SQLITE_BUSY_TIMEOUT
;
1033 idxHashAdd(&rc
, &p
->hIdx
, zName
, zIdx
);
1036 sqlite3_free(zName
);
1041 sqlite3_free(zCols
);
1047 ** Return true if list pList (linked by IdxConstraint.pLink) contains
1048 ** a constraint compatible with *p. Otherwise return false.
1050 static int idxFindConstraint(IdxConstraint
*pList
, IdxConstraint
*p
){
1051 IdxConstraint
*pCmp
;
1052 for(pCmp
=pList
; pCmp
; pCmp
=pCmp
->pLink
){
1053 if( p
->iCol
==pCmp
->iCol
) return 1;
1058 static int idxCreateFromWhere(
1060 IdxScan
*pScan
, /* Create indexes for this scan */
1061 IdxConstraint
*pTail
/* range/ORDER BY constraints for inclusion */
1063 IdxConstraint
*p1
= 0;
1064 IdxConstraint
*pCon
;
1067 /* Gather up all the == constraints. */
1068 for(pCon
=pScan
->pEq
; pCon
; pCon
=pCon
->pNext
){
1069 if( !idxFindConstraint(p1
, pCon
) && !idxFindConstraint(pTail
, pCon
) ){
1075 /* Create an index using the == constraints collected above. And the
1076 ** range constraint/ORDER BY terms passed in by the caller, if any. */
1077 rc
= idxCreateFromCons(p
, pScan
, p1
, pTail
);
1079 /* If no range/ORDER BY passed by the caller, create a version of the
1080 ** index for each range constraint. */
1082 for(pCon
=pScan
->pRange
; rc
==SQLITE_OK
&& pCon
; pCon
=pCon
->pNext
){
1083 assert( pCon
->pLink
==0 );
1084 if( !idxFindConstraint(p1
, pCon
) && !idxFindConstraint(pTail
, pCon
) ){
1085 rc
= idxCreateFromCons(p
, pScan
, p1
, pCon
);
1094 ** Create candidate indexes in database [dbm] based on the data in
1095 ** linked-list pScan.
1097 static int idxCreateCandidates(sqlite3expert
*p
){
1101 for(pIter
=p
->pScan
; pIter
&& rc
==SQLITE_OK
; pIter
=pIter
->pNextScan
){
1102 rc
= idxCreateFromWhere(p
, pIter
, 0);
1103 if( rc
==SQLITE_OK
&& pIter
->pOrder
){
1104 rc
= idxCreateFromWhere(p
, pIter
, pIter
->pOrder
);
1112 ** Free all elements of the linked list starting at pConstraint.
1114 static void idxConstraintFree(IdxConstraint
*pConstraint
){
1115 IdxConstraint
*pNext
;
1118 for(p
=pConstraint
; p
; p
=pNext
){
1125 ** Free all elements of the linked list starting from pScan up until pLast
1126 ** (pLast is not freed).
1128 static void idxScanFree(IdxScan
*pScan
, IdxScan
*pLast
){
1131 for(p
=pScan
; p
!=pLast
; p
=pNext
){
1132 pNext
= p
->pNextScan
;
1133 idxConstraintFree(p
->pOrder
);
1134 idxConstraintFree(p
->pEq
);
1135 idxConstraintFree(p
->pRange
);
1141 ** Free all elements of the linked list starting from pStatement up
1142 ** until pLast (pLast is not freed).
1144 static void idxStatementFree(IdxStatement
*pStatement
, IdxStatement
*pLast
){
1146 IdxStatement
*pNext
;
1147 for(p
=pStatement
; p
!=pLast
; p
=pNext
){
1149 sqlite3_free(p
->zEQP
);
1150 sqlite3_free(p
->zIdx
);
1156 ** Free the linked list of IdxTable objects starting at pTab.
1158 static void idxTableFree(IdxTable
*pTab
){
1161 for(pIter
=pTab
; pIter
; pIter
=pNext
){
1162 pNext
= pIter
->pNext
;
1163 sqlite3_free(pIter
);
1168 ** Free the linked list of IdxWrite objects starting at pTab.
1170 static void idxWriteFree(IdxWrite
*pTab
){
1173 for(pIter
=pTab
; pIter
; pIter
=pNext
){
1174 pNext
= pIter
->pNext
;
1175 sqlite3_free(pIter
);
1182 ** This function is called after candidate indexes have been created. It
1183 ** runs all the queries to see which indexes they prefer, and populates
1184 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
1186 static int idxFindIndexes(
1188 char **pzErr
/* OUT: Error message (sqlite3_malloc) */
1190 IdxStatement
*pStmt
;
1191 sqlite3
*dbm
= p
->dbm
;
1197 for(pStmt
=p
->pStatement
; rc
==SQLITE_OK
&& pStmt
; pStmt
=pStmt
->pNext
){
1198 IdxHashEntry
*pEntry
;
1199 sqlite3_stmt
*pExplain
= 0;
1200 idxHashClear(&hIdx
);
1201 rc
= idxPrintfPrepareStmt(dbm
, &pExplain
, pzErr
,
1202 "EXPLAIN QUERY PLAN %s", pStmt
->zSql
1204 while( rc
==SQLITE_OK
&& sqlite3_step(pExplain
)==SQLITE_ROW
){
1205 /* int iId = sqlite3_column_int(pExplain, 0); */
1206 /* int iParent = sqlite3_column_int(pExplain, 1); */
1207 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
1208 const char *zDetail
= (const char*)sqlite3_column_text(pExplain
, 3);
1212 if( !zDetail
) continue;
1213 nDetail
= STRLEN(zDetail
);
1215 for(i
=0; i
<nDetail
; i
++){
1216 const char *zIdx
= 0;
1217 if( i
+13<nDetail
&& memcmp(&zDetail
[i
], " USING INDEX ", 13)==0 ){
1218 zIdx
= &zDetail
[i
+13];
1219 }else if( i
+22<nDetail
1220 && memcmp(&zDetail
[i
], " USING COVERING INDEX ", 22)==0
1222 zIdx
= &zDetail
[i
+22];
1227 while( zIdx
[nIdx
]!='\0' && (zIdx
[nIdx
]!=' ' || zIdx
[nIdx
+1]!='(') ){
1230 zSql
= idxHashSearch(&p
->hIdx
, zIdx
, nIdx
);
1232 idxHashAdd(&rc
, &hIdx
, zSql
, 0);
1233 if( rc
) goto find_indexes_out
;
1239 if( zDetail
[0]!='-' ){
1240 pStmt
->zEQP
= idxAppendText(&rc
, pStmt
->zEQP
, "%s\n", zDetail
);
1244 for(pEntry
=hIdx
.pFirst
; pEntry
; pEntry
=pEntry
->pNext
){
1245 pStmt
->zIdx
= idxAppendText(&rc
, pStmt
->zIdx
, "%s;\n", pEntry
->zKey
);
1248 idxFinalize(&rc
, pExplain
);
1252 idxHashClear(&hIdx
);
1256 static int idxAuthCallback(
1262 const char *zTrigger
1267 if( eOp
==SQLITE_INSERT
|| eOp
==SQLITE_UPDATE
|| eOp
==SQLITE_DELETE
){
1268 if( sqlite3_stricmp(zDb
, "main")==0 ){
1269 sqlite3expert
*p
= (sqlite3expert
*)pCtx
;
1271 for(pTab
=p
->pTable
; pTab
; pTab
=pTab
->pNext
){
1272 if( 0==sqlite3_stricmp(z3
, pTab
->zName
) ) break;
1276 for(pWrite
=p
->pWrite
; pWrite
; pWrite
=pWrite
->pNext
){
1277 if( pWrite
->pTab
==pTab
&& pWrite
->eOp
==eOp
) break;
1280 pWrite
= idxMalloc(&rc
, sizeof(IdxWrite
));
1281 if( rc
==SQLITE_OK
){
1282 pWrite
->pTab
= pTab
;
1284 pWrite
->pNext
= p
->pWrite
;
1294 static int idxProcessOneTrigger(
1299 static const char *zInt
= UNIQUE_TABLE_NAME
;
1300 static const char *zDrop
= "DROP TABLE " UNIQUE_TABLE_NAME
;
1301 IdxTable
*pTab
= pWrite
->pTab
;
1302 const char *zTab
= pTab
->zName
;
1304 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
1305 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
1307 sqlite3_stmt
*pSelect
= 0;
1311 /* Create the table and its triggers in the temp schema */
1312 rc
= idxPrintfPrepareStmt(p
->db
, &pSelect
, pzErr
, zSql
, zTab
, zTab
);
1313 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSelect
) ){
1314 const char *zCreate
= (const char*)sqlite3_column_text(pSelect
, 0);
1315 if( zCreate
==0 ) continue;
1316 rc
= sqlite3_exec(p
->dbv
, zCreate
, 0, 0, pzErr
);
1318 idxFinalize(&rc
, pSelect
);
1320 /* Rename the table in the temp schema to zInt */
1321 if( rc
==SQLITE_OK
){
1322 char *z
= sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab
, zInt
);
1326 rc
= sqlite3_exec(p
->dbv
, z
, 0, 0, pzErr
);
1331 switch( pWrite
->eOp
){
1332 case SQLITE_INSERT
: {
1334 zWrite
= idxAppendText(&rc
, zWrite
, "INSERT INTO %Q VALUES(", zInt
);
1335 for(i
=0; i
<pTab
->nCol
; i
++){
1336 zWrite
= idxAppendText(&rc
, zWrite
, "%s?", i
==0 ? "" : ", ");
1338 zWrite
= idxAppendText(&rc
, zWrite
, ")");
1341 case SQLITE_UPDATE
: {
1343 zWrite
= idxAppendText(&rc
, zWrite
, "UPDATE %Q SET ", zInt
);
1344 for(i
=0; i
<pTab
->nCol
; i
++){
1345 zWrite
= idxAppendText(&rc
, zWrite
, "%s%Q=?", i
==0 ? "" : ", ",
1352 assert( pWrite
->eOp
==SQLITE_DELETE
);
1353 if( rc
==SQLITE_OK
){
1354 zWrite
= sqlite3_mprintf("DELETE FROM %Q", zInt
);
1355 if( zWrite
==0 ) rc
= SQLITE_NOMEM
;
1360 if( rc
==SQLITE_OK
){
1361 sqlite3_stmt
*pX
= 0;
1362 rc
= sqlite3_prepare_v2(p
->dbv
, zWrite
, -1, &pX
, 0);
1363 idxFinalize(&rc
, pX
);
1364 if( rc
!=SQLITE_OK
){
1365 idxDatabaseError(p
->dbv
, pzErr
);
1368 sqlite3_free(zWrite
);
1370 if( rc
==SQLITE_OK
){
1371 rc
= sqlite3_exec(p
->dbv
, zDrop
, 0, 0, pzErr
);
1377 static int idxProcessTriggers(sqlite3expert
*p
, char **pzErr
){
1380 IdxWrite
*pFirst
= p
->pWrite
;
1382 while( rc
==SQLITE_OK
&& pFirst
!=pEnd
){
1384 for(pIter
=pFirst
; rc
==SQLITE_OK
&& pIter
!=pEnd
; pIter
=pIter
->pNext
){
1385 rc
= idxProcessOneTrigger(p
, pIter
, pzErr
);
1395 static int idxCreateVtabSchema(sqlite3expert
*p
, char **pzErrmsg
){
1396 int rc
= idxRegisterVtab(p
);
1397 sqlite3_stmt
*pSchema
= 0;
1399 /* For each table in the main db schema:
1401 ** 1) Add an entry to the p->pTable list, and
1402 ** 2) Create the equivalent virtual table in dbv.
1404 rc
= idxPrepareStmt(p
->db
, &pSchema
, pzErrmsg
,
1405 "SELECT type, name, sql, 1 FROM sqlite_schema "
1406 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
1408 "SELECT type, name, sql, 2 FROM sqlite_schema "
1409 "WHERE type = 'trigger'"
1410 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
1413 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSchema
) ){
1414 const char *zType
= (const char*)sqlite3_column_text(pSchema
, 0);
1415 const char *zName
= (const char*)sqlite3_column_text(pSchema
, 1);
1416 const char *zSql
= (const char*)sqlite3_column_text(pSchema
, 2);
1418 if( zType
==0 || zName
==0 ) continue;
1419 if( zType
[0]=='v' || zType
[1]=='r' ){
1420 if( zSql
) rc
= sqlite3_exec(p
->dbv
, zSql
, 0, 0, pzErrmsg
);
1423 rc
= idxGetTableInfo(p
->db
, zName
, &pTab
, pzErrmsg
);
1424 if( rc
==SQLITE_OK
){
1428 pTab
->pNext
= p
->pTable
;
1431 /* The statement the vtab will pass to sqlite3_declare_vtab() */
1432 zInner
= idxAppendText(&rc
, 0, "CREATE TABLE x(");
1433 for(i
=0; i
<pTab
->nCol
; i
++){
1434 zInner
= idxAppendText(&rc
, zInner
, "%s%Q COLLATE %s",
1435 (i
==0 ? "" : ", "), pTab
->aCol
[i
].zName
, pTab
->aCol
[i
].zColl
1438 zInner
= idxAppendText(&rc
, zInner
, ")");
1440 /* The CVT statement to create the vtab */
1441 zOuter
= idxAppendText(&rc
, 0,
1442 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName
, zInner
1444 if( rc
==SQLITE_OK
){
1445 rc
= sqlite3_exec(p
->dbv
, zOuter
, 0, 0, pzErrmsg
);
1447 sqlite3_free(zInner
);
1448 sqlite3_free(zOuter
);
1452 idxFinalize(&rc
, pSchema
);
1456 struct IdxSampleCtx
{
1458 double target
; /* Target nRet/nRow value */
1459 double nRow
; /* Number of rows seen */
1460 double nRet
; /* Number of rows returned */
1463 static void idxSampleFunc(
1464 sqlite3_context
*pCtx
,
1466 sqlite3_value
**argv
1468 struct IdxSampleCtx
*p
= (struct IdxSampleCtx
*)sqlite3_user_data(pCtx
);
1476 bRet
= (p
->nRet
/ p
->nRow
) <= p
->target
;
1479 sqlite3_randomness(2, (void*)&rnd
);
1480 bRet
= ((int)rnd
% 100) <= p
->iTarget
;
1484 sqlite3_result_int(pCtx
, bRet
);
1486 p
->nRet
+= (double)bRet
;
1492 int eType
; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
1493 i64 iVal
; /* SQLITE_INTEGER value */
1494 double rVal
; /* SQLITE_FLOAT value */
1495 int nByte
; /* Bytes of space allocated at z */
1496 int n
; /* Size of buffer z */
1497 char *z
; /* SQLITE_TEXT/BLOB value */
1502 ** Implementation of scalar function rem().
1504 static void idxRemFunc(
1505 sqlite3_context
*pCtx
,
1507 sqlite3_value
**argv
1509 struct IdxRemCtx
*p
= (struct IdxRemCtx
*)sqlite3_user_data(pCtx
);
1510 struct IdxRemSlot
*pSlot
;
1514 iSlot
= sqlite3_value_int(argv
[0]);
1515 assert( iSlot
<=p
->nSlot
);
1516 pSlot
= &p
->aSlot
[iSlot
];
1518 switch( pSlot
->eType
){
1523 case SQLITE_INTEGER
:
1524 sqlite3_result_int64(pCtx
, pSlot
->iVal
);
1528 sqlite3_result_double(pCtx
, pSlot
->rVal
);
1532 sqlite3_result_blob(pCtx
, pSlot
->z
, pSlot
->n
, SQLITE_TRANSIENT
);
1536 sqlite3_result_text(pCtx
, pSlot
->z
, pSlot
->n
, SQLITE_TRANSIENT
);
1540 pSlot
->eType
= sqlite3_value_type(argv
[1]);
1541 switch( pSlot
->eType
){
1546 case SQLITE_INTEGER
:
1547 pSlot
->iVal
= sqlite3_value_int64(argv
[1]);
1551 pSlot
->rVal
= sqlite3_value_double(argv
[1]);
1556 int nByte
= sqlite3_value_bytes(argv
[1]);
1557 const void *pData
= 0;
1558 if( nByte
>pSlot
->nByte
){
1559 char *zNew
= (char*)sqlite3_realloc(pSlot
->z
, nByte
*2);
1561 sqlite3_result_error_nomem(pCtx
);
1564 pSlot
->nByte
= nByte
*2;
1568 if( pSlot
->eType
==SQLITE_BLOB
){
1569 pData
= sqlite3_value_blob(argv
[1]);
1570 if( pData
) memcpy(pSlot
->z
, pData
, nByte
);
1572 pData
= sqlite3_value_text(argv
[1]);
1573 memcpy(pSlot
->z
, pData
, nByte
);
1580 static int idxLargestIndex(sqlite3
*db
, int *pnMax
, char **pzErr
){
1583 "SELECT max(i.seqno) FROM "
1584 " sqlite_schema AS s, "
1585 " pragma_index_list(s.name) AS l, "
1586 " pragma_index_info(l.name) AS i "
1587 "WHERE s.type = 'table'";
1588 sqlite3_stmt
*pMax
= 0;
1591 rc
= idxPrepareStmt(db
, &pMax
, pzErr
, zMax
);
1592 if( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pMax
) ){
1593 *pnMax
= sqlite3_column_int(pMax
, 0) + 1;
1595 idxFinalize(&rc
, pMax
);
1600 static int idxPopulateOneStat1(
1602 sqlite3_stmt
*pIndexXInfo
,
1603 sqlite3_stmt
*pWriteStat
,
1613 sqlite3_stmt
*pQuery
= 0;
1617 assert( p
->iSample
>0 );
1619 /* Formulate the query text */
1620 sqlite3_bind_text(pIndexXInfo
, 1, zIdx
, -1, SQLITE_STATIC
);
1621 while( SQLITE_OK
==rc
&& SQLITE_ROW
==sqlite3_step(pIndexXInfo
) ){
1622 const char *zComma
= zCols
==0 ? "" : ", ";
1623 const char *zName
= (const char*)sqlite3_column_text(pIndexXInfo
, 0);
1624 const char *zColl
= (const char*)sqlite3_column_text(pIndexXInfo
, 1);
1625 zCols
= idxAppendText(&rc
, zCols
,
1626 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma
, zName
, nCol
, zName
, zColl
1628 zOrder
= idxAppendText(&rc
, zOrder
, "%s%d", zComma
, ++nCol
);
1630 sqlite3_reset(pIndexXInfo
);
1631 if( rc
==SQLITE_OK
){
1632 if( p
->iSample
==100 ){
1633 zQuery
= sqlite3_mprintf(
1634 "SELECT %s FROM %Q x ORDER BY %s", zCols
, zTab
, zOrder
1637 zQuery
= sqlite3_mprintf(
1638 "SELECT %s FROM temp."UNIQUE_TABLE_NAME
" x ORDER BY %s", zCols
, zOrder
1642 sqlite3_free(zCols
);
1643 sqlite3_free(zOrder
);
1645 /* Formulate the query text */
1646 if( rc
==SQLITE_OK
){
1647 sqlite3
*dbrem
= (p
->iSample
==100 ? p
->db
: p
->dbv
);
1648 rc
= idxPrepareStmt(dbrem
, &pQuery
, pzErr
, zQuery
);
1650 sqlite3_free(zQuery
);
1652 if( rc
==SQLITE_OK
){
1653 aStat
= (int*)idxMalloc(&rc
, sizeof(int)*(nCol
+1));
1655 if( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pQuery
) ){
1656 IdxHashEntry
*pEntry
;
1658 for(i
=0; i
<=nCol
; i
++) aStat
[i
] = 1;
1659 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pQuery
) ){
1661 for(i
=0; i
<nCol
; i
++){
1662 if( sqlite3_column_int(pQuery
, i
)==0 ) break;
1664 for(/*no-op*/; i
<nCol
; i
++){
1669 if( rc
==SQLITE_OK
){
1671 zStat
= sqlite3_mprintf("%d", s0
);
1672 if( zStat
==0 ) rc
= SQLITE_NOMEM
;
1673 for(i
=1; rc
==SQLITE_OK
&& i
<=nCol
; i
++){
1674 zStat
= idxAppendText(&rc
, zStat
, " %d", (s0
+aStat
[i
]/2) / aStat
[i
]);
1678 if( rc
==SQLITE_OK
){
1679 sqlite3_bind_text(pWriteStat
, 1, zTab
, -1, SQLITE_STATIC
);
1680 sqlite3_bind_text(pWriteStat
, 2, zIdx
, -1, SQLITE_STATIC
);
1681 sqlite3_bind_text(pWriteStat
, 3, zStat
, -1, SQLITE_STATIC
);
1682 sqlite3_step(pWriteStat
);
1683 rc
= sqlite3_reset(pWriteStat
);
1686 pEntry
= idxHashFind(&p
->hIdx
, zIdx
, STRLEN(zIdx
));
1688 assert( pEntry
->zVal2
==0 );
1689 pEntry
->zVal2
= zStat
;
1691 sqlite3_free(zStat
);
1694 sqlite3_free(aStat
);
1695 idxFinalize(&rc
, pQuery
);
1700 static int idxBuildSampleTable(sqlite3expert
*p
, const char *zTab
){
1704 rc
= sqlite3_exec(p
->dbv
,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME
,0,0,0);
1705 if( rc
!=SQLITE_OK
) return rc
;
1707 zSql
= sqlite3_mprintf(
1708 "CREATE TABLE temp." UNIQUE_TABLE_NAME
" AS SELECT * FROM %Q", zTab
1710 if( zSql
==0 ) return SQLITE_NOMEM
;
1711 rc
= sqlite3_exec(p
->dbv
, zSql
, 0, 0, 0);
1718 ** This function is called as part of sqlite3_expert_analyze(). Candidate
1719 ** indexes have already been created in database sqlite3expert.dbm, this
1720 ** function populates sqlite_stat1 table in the same database.
1722 ** The stat1 data is generated by querying the
1724 static int idxPopulateStat1(sqlite3expert
*p
, char **pzErr
){
1727 struct IdxRemCtx
*pCtx
= 0;
1728 struct IdxSampleCtx samplectx
;
1730 i64 iPrev
= -100000;
1731 sqlite3_stmt
*pAllIndex
= 0;
1732 sqlite3_stmt
*pIndexXInfo
= 0;
1733 sqlite3_stmt
*pWrite
= 0;
1735 const char *zAllIndex
=
1736 "SELECT s.rowid, s.name, l.name FROM "
1737 " sqlite_schema AS s, "
1738 " pragma_index_list(s.name) AS l "
1739 "WHERE s.type = 'table'";
1740 const char *zIndexXInfo
=
1741 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
1742 const char *zWrite
= "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
1744 /* If iSample==0, no sqlite_stat1 data is required. */
1745 if( p
->iSample
==0 ) return SQLITE_OK
;
1747 rc
= idxLargestIndex(p
->dbm
, &nMax
, pzErr
);
1748 if( nMax
<=0 || rc
!=SQLITE_OK
) return rc
;
1750 rc
= sqlite3_exec(p
->dbm
, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
1752 if( rc
==SQLITE_OK
){
1753 int nByte
= sizeof(struct IdxRemCtx
) + (sizeof(struct IdxRemSlot
) * nMax
);
1754 pCtx
= (struct IdxRemCtx
*)idxMalloc(&rc
, nByte
);
1757 if( rc
==SQLITE_OK
){
1758 sqlite3
*dbrem
= (p
->iSample
==100 ? p
->db
: p
->dbv
);
1759 rc
= sqlite3_create_function(
1760 dbrem
, "rem", 2, SQLITE_UTF8
, (void*)pCtx
, idxRemFunc
, 0, 0
1763 if( rc
==SQLITE_OK
){
1764 rc
= sqlite3_create_function(
1765 p
->db
, "sample", 0, SQLITE_UTF8
, (void*)&samplectx
, idxSampleFunc
, 0, 0
1769 if( rc
==SQLITE_OK
){
1770 pCtx
->nSlot
= nMax
+1;
1771 rc
= idxPrepareStmt(p
->dbm
, &pAllIndex
, pzErr
, zAllIndex
);
1773 if( rc
==SQLITE_OK
){
1774 rc
= idxPrepareStmt(p
->dbm
, &pIndexXInfo
, pzErr
, zIndexXInfo
);
1776 if( rc
==SQLITE_OK
){
1777 rc
= idxPrepareStmt(p
->dbm
, &pWrite
, pzErr
, zWrite
);
1780 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pAllIndex
) ){
1781 i64 iRowid
= sqlite3_column_int64(pAllIndex
, 0);
1782 const char *zTab
= (const char*)sqlite3_column_text(pAllIndex
, 1);
1783 const char *zIdx
= (const char*)sqlite3_column_text(pAllIndex
, 2);
1784 if( zTab
==0 || zIdx
==0 ) continue;
1785 if( p
->iSample
<100 && iPrev
!=iRowid
){
1786 samplectx
.target
= (double)p
->iSample
/ 100.0;
1787 samplectx
.iTarget
= p
->iSample
;
1788 samplectx
.nRow
= 0.0;
1789 samplectx
.nRet
= 0.0;
1790 rc
= idxBuildSampleTable(p
, zTab
);
1791 if( rc
!=SQLITE_OK
) break;
1793 rc
= idxPopulateOneStat1(p
, pIndexXInfo
, pWrite
, zTab
, zIdx
, pzErr
);
1796 if( rc
==SQLITE_OK
&& p
->iSample
<100 ){
1797 rc
= sqlite3_exec(p
->dbv
,
1798 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME
, 0,0,0
1802 idxFinalize(&rc
, pAllIndex
);
1803 idxFinalize(&rc
, pIndexXInfo
);
1804 idxFinalize(&rc
, pWrite
);
1807 for(i
=0; i
<pCtx
->nSlot
; i
++){
1808 sqlite3_free(pCtx
->aSlot
[i
].z
);
1813 if( rc
==SQLITE_OK
){
1814 rc
= sqlite3_exec(p
->dbm
, "ANALYZE sqlite_schema", 0, 0, 0);
1817 sqlite3_exec(p
->db
, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME
,0,0,0);
1822 ** Allocate a new sqlite3expert object.
1824 sqlite3expert
*sqlite3_expert_new(sqlite3
*db
, char **pzErrmsg
){
1826 sqlite3expert
*pNew
;
1828 pNew
= (sqlite3expert
*)idxMalloc(&rc
, sizeof(sqlite3expert
));
1830 /* Open two in-memory databases to work with. The "vtab database" (dbv)
1831 ** will contain a virtual table corresponding to each real table in
1832 ** the user database schema, and a copy of each view. It is used to
1833 ** collect information regarding the WHERE, ORDER BY and other clauses
1834 ** of the user's query.
1836 if( rc
==SQLITE_OK
){
1838 pNew
->iSample
= 100;
1839 rc
= sqlite3_open(":memory:", &pNew
->dbv
);
1841 if( rc
==SQLITE_OK
){
1842 rc
= sqlite3_open(":memory:", &pNew
->dbm
);
1843 if( rc
==SQLITE_OK
){
1844 sqlite3_db_config(pNew
->dbm
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, (int*)0);
1849 /* Copy the entire schema of database [db] into [dbm]. */
1850 if( rc
==SQLITE_OK
){
1851 sqlite3_stmt
*pSql
= 0;
1852 rc
= idxPrintfPrepareStmt(pNew
->db
, &pSql
, pzErrmsg
,
1853 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
1854 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
1856 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
1857 const char *zSql
= (const char*)sqlite3_column_text(pSql
, 0);
1858 if( zSql
) rc
= sqlite3_exec(pNew
->dbm
, zSql
, 0, 0, pzErrmsg
);
1860 idxFinalize(&rc
, pSql
);
1863 /* Create the vtab schema */
1864 if( rc
==SQLITE_OK
){
1865 rc
= idxCreateVtabSchema(pNew
, pzErrmsg
);
1868 /* Register the auth callback with dbv */
1869 if( rc
==SQLITE_OK
){
1870 sqlite3_set_authorizer(pNew
->dbv
, idxAuthCallback
, (void*)pNew
);
1873 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
1874 ** return the new sqlite3expert handle. */
1875 if( rc
!=SQLITE_OK
){
1876 sqlite3_expert_destroy(pNew
);
1883 ** Configure an sqlite3expert object.
1885 int sqlite3_expert_config(sqlite3expert
*p
, int op
, ...){
1890 case EXPERT_CONFIG_SAMPLE
: {
1891 int iVal
= va_arg(ap
, int);
1892 if( iVal
<0 ) iVal
= 0;
1893 if( iVal
>100 ) iVal
= 100;
1898 rc
= SQLITE_NOTFOUND
;
1907 ** Add an SQL statement to the analysis.
1909 int sqlite3_expert_sql(
1910 sqlite3expert
*p
, /* From sqlite3_expert_new() */
1911 const char *zSql
, /* SQL statement to add */
1912 char **pzErr
/* OUT: Error message (if any) */
1914 IdxScan
*pScanOrig
= p
->pScan
;
1915 IdxStatement
*pStmtOrig
= p
->pStatement
;
1917 const char *zStmt
= zSql
;
1919 if( p
->bRun
) return SQLITE_MISUSE
;
1921 while( rc
==SQLITE_OK
&& zStmt
&& zStmt
[0] ){
1922 sqlite3_stmt
*pStmt
= 0;
1923 rc
= sqlite3_prepare_v2(p
->dbv
, zStmt
, -1, &pStmt
, &zStmt
);
1924 if( rc
==SQLITE_OK
){
1927 const char *z
= sqlite3_sql(pStmt
);
1929 pNew
= (IdxStatement
*)idxMalloc(&rc
, sizeof(IdxStatement
) + n
+1);
1930 if( rc
==SQLITE_OK
){
1931 pNew
->zSql
= (char*)&pNew
[1];
1932 memcpy(pNew
->zSql
, z
, n
+1);
1933 pNew
->pNext
= p
->pStatement
;
1934 if( p
->pStatement
) pNew
->iId
= p
->pStatement
->iId
+1;
1935 p
->pStatement
= pNew
;
1937 sqlite3_finalize(pStmt
);
1940 idxDatabaseError(p
->dbv
, pzErr
);
1944 if( rc
!=SQLITE_OK
){
1945 idxScanFree(p
->pScan
, pScanOrig
);
1946 idxStatementFree(p
->pStatement
, pStmtOrig
);
1947 p
->pScan
= pScanOrig
;
1948 p
->pStatement
= pStmtOrig
;
1954 int sqlite3_expert_analyze(sqlite3expert
*p
, char **pzErr
){
1956 IdxHashEntry
*pEntry
;
1958 /* Do trigger processing to collect any extra IdxScan structures */
1959 rc
= idxProcessTriggers(p
, pzErr
);
1961 /* Create candidate indexes within the in-memory database file */
1962 if( rc
==SQLITE_OK
){
1963 rc
= idxCreateCandidates(p
);
1964 }else if ( rc
==SQLITE_BUSY_TIMEOUT
){
1966 *pzErr
= sqlite3_mprintf("Cannot find a unique index name to propose.");
1970 /* Generate the stat1 data */
1971 if( rc
==SQLITE_OK
){
1972 rc
= idxPopulateStat1(p
, pzErr
);
1975 /* Formulate the EXPERT_REPORT_CANDIDATES text */
1976 for(pEntry
=p
->hIdx
.pFirst
; pEntry
; pEntry
=pEntry
->pNext
){
1977 p
->zCandidates
= idxAppendText(&rc
, p
->zCandidates
,
1978 "%s;%s%s\n", pEntry
->zVal
,
1979 pEntry
->zVal2
? " -- stat1: " : "", pEntry
->zVal2
1983 /* Figure out which of the candidate indexes are preferred by the query
1984 ** planner and report the results to the user. */
1985 if( rc
==SQLITE_OK
){
1986 rc
= idxFindIndexes(p
, pzErr
);
1989 if( rc
==SQLITE_OK
){
1996 ** Return the total number of statements that have been added to this
1997 ** sqlite3expert using sqlite3_expert_sql().
1999 int sqlite3_expert_count(sqlite3expert
*p
){
2001 if( p
->pStatement
) nRet
= p
->pStatement
->iId
+1;
2006 ** Return a component of the report.
2008 const char *sqlite3_expert_report(sqlite3expert
*p
, int iStmt
, int eReport
){
2009 const char *zRet
= 0;
2010 IdxStatement
*pStmt
;
2012 if( p
->bRun
==0 ) return 0;
2013 for(pStmt
=p
->pStatement
; pStmt
&& pStmt
->iId
!=iStmt
; pStmt
=pStmt
->pNext
);
2015 case EXPERT_REPORT_SQL
:
2016 if( pStmt
) zRet
= pStmt
->zSql
;
2018 case EXPERT_REPORT_INDEXES
:
2019 if( pStmt
) zRet
= pStmt
->zIdx
;
2021 case EXPERT_REPORT_PLAN
:
2022 if( pStmt
) zRet
= pStmt
->zEQP
;
2024 case EXPERT_REPORT_CANDIDATES
:
2025 zRet
= p
->zCandidates
;
2032 ** Free an sqlite3expert object.
2034 void sqlite3_expert_destroy(sqlite3expert
*p
){
2036 sqlite3_close(p
->dbm
);
2037 sqlite3_close(p
->dbv
);
2038 idxScanFree(p
->pScan
, 0);
2039 idxStatementFree(p
->pStatement
, 0);
2040 idxTableFree(p
->pTable
);
2041 idxWriteFree(p
->pWrite
);
2042 idxHashClear(&p
->hIdx
);
2043 sqlite3_free(p
->zCandidates
);
2048 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */