blob: aeb9972745abfb39c3cfb1b21379dbd42a9d7d9c [file] [log] [blame]
drh9f18e8a2005-07-08 12:13:04 +00001/*
2** 2005 July 8
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12** This file contains code associated with the ANALYZE command.
13**
danielk1977cd3e8f72008-03-25 09:47:35 +000014** @(#) $Id: analyze.c,v 1.42 2008/03/25 09:47:35 danielk1977 Exp $
drh9f18e8a2005-07-08 12:13:04 +000015*/
16#ifndef SQLITE_OMIT_ANALYZE
17#include "sqliteInt.h"
18
19/*
drhff2d5ea2005-07-23 00:41:48 +000020** This routine generates code that opens the sqlite_stat1 table on cursor
21** iStatCur.
22**
23** If the sqlite_stat1 tables does not previously exist, it is created.
24** If it does previously exist, all entires associated with table zWhere
25** are removed. If zWhere==0 then all entries are removed.
26*/
27static void openStatTable(
28 Parse *pParse, /* Parsing context */
29 int iDb, /* The database we are looking in */
30 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
31 const char *zWhere /* Delete entries associated with this table */
32){
33 sqlite3 *db = pParse->db;
34 Db *pDb;
35 int iRootPage;
drhb7654112008-01-12 12:48:07 +000036 int createStat1 = 0;
drhff2d5ea2005-07-23 00:41:48 +000037 Table *pStat;
38 Vdbe *v = sqlite3GetVdbe(pParse);
39
drhcf1be452007-05-12 12:08:51 +000040 if( v==0 ) return;
drh1fee73e2007-08-29 04:00:57 +000041 assert( sqlite3BtreeHoldsAllMutexes(db) );
42 assert( sqlite3VdbeDb(v)==db );
drhff2d5ea2005-07-23 00:41:48 +000043 pDb = &db->aDb[iDb];
44 if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
45 /* The sqlite_stat1 tables does not exist. Create it.
46 ** Note that a side-effect of the CREATE TABLE statement is to leave
drhb7654112008-01-12 12:48:07 +000047 ** the rootpage of the new table in register pParse->regRoot. This is
drhff2d5ea2005-07-23 00:41:48 +000048 ** important because the OpenWrite opcode below will be needing it. */
49 sqlite3NestedParse(pParse,
50 "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)",
51 pDb->zName
52 );
drhb7654112008-01-12 12:48:07 +000053 iRootPage = pParse->regRoot;
54 createStat1 = 1; /* Cause rootpage to be taken from top of stack */
drhff2d5ea2005-07-23 00:41:48 +000055 }else if( zWhere ){
56 /* The sqlite_stat1 table exists. Delete all entries associated with
57 ** the table zWhere. */
58 sqlite3NestedParse(pParse,
59 "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q",
60 pDb->zName, zWhere
61 );
62 iRootPage = pStat->tnum;
63 }else{
64 /* The sqlite_stat1 table already exists. Delete all rows. */
65 iRootPage = pStat->tnum;
drh66a51672008-01-03 00:01:23 +000066 sqlite3VdbeAddOp2(v, OP_Clear, pStat->tnum, iDb);
drhff2d5ea2005-07-23 00:41:48 +000067 }
68
danielk1977c00da102006-01-07 13:21:04 +000069 /* Open the sqlite_stat1 table for writing. Unless it was created
70 ** by this vdbe program, lock it for writing at the shared-cache level.
71 ** If this vdbe did create the sqlite_stat1 table, then it must have
72 ** already obtained a schema-lock, making the write-lock redundant.
drhff2d5ea2005-07-23 00:41:48 +000073 */
drhb7654112008-01-12 12:48:07 +000074 if( !createStat1 ){
danielk1977c00da102006-01-07 13:21:04 +000075 sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
76 }
danielk1977cd3e8f72008-03-25 09:47:35 +000077 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, 3);
danielk1977207872a2008-01-03 07:54:23 +000078 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur, iRootPage, iDb);
drhb7654112008-01-12 12:48:07 +000079 sqlite3VdbeChangeP5(v, createStat1);
drhff2d5ea2005-07-23 00:41:48 +000080}
81
82/*
83** Generate code to do an analysis of all indices associated with
84** a single table.
85*/
86static void analyzeOneTable(
87 Parse *pParse, /* Parser context */
88 Table *pTab, /* Table whose indices are to be analyzed */
89 int iStatCur, /* Cursor that writes to the sqlite_stat1 table */
90 int iMem /* Available memory locations begin here */
91){
92 Index *pIdx; /* An index to being analyzed */
93 int iIdxCur; /* Cursor number for index being analyzed */
94 int nCol; /* Number of columns in the index */
95 Vdbe *v; /* The virtual machine being built up */
96 int i; /* Loop counter */
97 int topOfLoop; /* The top of the loop */
98 int endOfLoop; /* The end of the loop */
99 int addr; /* The address of an instruction */
danielk1977da184232006-01-05 11:34:32 +0000100 int iDb; /* Index of database containing pTab */
drhff2d5ea2005-07-23 00:41:48 +0000101
102 v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +0000103 if( v==0 || pTab==0 || pTab->pIndex==0 ){
drh0c356672005-09-10 22:40:53 +0000104 /* Do no analysis for tables that have no indices */
drhff2d5ea2005-07-23 00:41:48 +0000105 return;
106 }
drh1fee73e2007-08-29 04:00:57 +0000107 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000108 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
109 assert( iDb>=0 );
drhe6e04962005-07-23 02:17:03 +0000110#ifndef SQLITE_OMIT_AUTHORIZATION
111 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
danielk1977da184232006-01-05 11:34:32 +0000112 pParse->db->aDb[iDb].zName ) ){
drhe6e04962005-07-23 02:17:03 +0000113 return;
114 }
115#endif
116
danielk1977c00da102006-01-07 13:21:04 +0000117 /* Establish a read-lock on the table at the shared-cache level. */
118 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
119
drhff2d5ea2005-07-23 00:41:48 +0000120 iIdxCur = pParse->nTab;
121 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk1977b3bf5562006-01-10 17:58:23 +0000122 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
drh2d401ab2008-01-10 23:50:11 +0000123 int regFields; /* Register block for building records */
124 int regRec; /* Register holding completed record */
125 int regTemp; /* Temporary use register */
126 int regCol; /* Content of a column from the table being analyzed */
127 int regRowid; /* Rowid for the inserted record */
128 int regF2;
danielk1977b3bf5562006-01-10 17:58:23 +0000129
drhff2d5ea2005-07-23 00:41:48 +0000130 /* Open a cursor to the index to be analyzed
131 */
danielk1977da184232006-01-05 11:34:32 +0000132 assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
danielk1977cd3e8f72008-03-25 09:47:35 +0000133 nCol = pIdx->nColumn;
134 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, nCol+1);
danielk1977207872a2008-01-03 07:54:23 +0000135 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000136 (char *)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +0000137 VdbeComment((v, "%s", pIdx->zName));
drh2d401ab2008-01-10 23:50:11 +0000138 regFields = iMem+nCol*2;
139 regTemp = regRowid = regCol = regFields+3;
140 regRec = regCol+1;
141 if( regRec>pParse->nMem ){
142 pParse->nMem = regRec;
drhff2d5ea2005-07-23 00:41:48 +0000143 }
drhff2d5ea2005-07-23 00:41:48 +0000144
145 /* Memory cells are used as follows:
146 **
147 ** mem[iMem]: The total number of rows in the table.
148 ** mem[iMem+1]: Number of distinct values in column 1
149 ** ...
150 ** mem[iMem+nCol]: Number of distinct values in column N
151 ** mem[iMem+nCol+1] Last observed value of column 1
152 ** ...
153 ** mem[iMem+nCol+nCol]: Last observed value of column N
154 **
155 ** Cells iMem through iMem+nCol are initialized to 0. The others
156 ** are initialized to NULL.
157 */
drhff2d5ea2005-07-23 00:41:48 +0000158 for(i=0; i<=nCol; i++){
drh4c583122008-01-04 22:01:03 +0000159 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
drhff2d5ea2005-07-23 00:41:48 +0000160 }
drhff2d5ea2005-07-23 00:41:48 +0000161 for(i=0; i<nCol; i++){
drh4c583122008-01-04 22:01:03 +0000162 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000163 }
164
165 /* Do the analysis.
166 */
drhff2d5ea2005-07-23 00:41:48 +0000167 endOfLoop = sqlite3VdbeMakeLabel(v);
drh66a51672008-01-03 00:01:23 +0000168 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
drhe6e04962005-07-23 02:17:03 +0000169 topOfLoop = sqlite3VdbeCurrentAddr(v);
drh8558cde2008-01-05 05:20:10 +0000170 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
drhff2d5ea2005-07-23 00:41:48 +0000171 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000172 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
173 sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
174 /**** TODO: add collating sequence *****/
drh35573352008-01-08 23:54:25 +0000175 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
drhff2d5ea2005-07-23 00:41:48 +0000176 }
drh66a51672008-01-03 00:01:23 +0000177 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
drhff2d5ea2005-07-23 00:41:48 +0000178 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000179 sqlite3VdbeJumpHere(v, topOfLoop + 2*(i + 1));
180 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
drhb1fdb2a2008-01-05 04:06:03 +0000181 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000182 }
183 sqlite3VdbeResolveLabel(v, endOfLoop);
drh66a51672008-01-03 00:01:23 +0000184 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
185 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
drhff2d5ea2005-07-23 00:41:48 +0000186
187 /* Store the results.
188 **
drh4c103832007-06-20 13:37:31 +0000189 ** The result is a single row of the sqlite_stat1 table. The first
drhff2d5ea2005-07-23 00:41:48 +0000190 ** two columns are the names of the table and index. The third column
191 ** is a string composed of a list of integer statistics about the
drh17a18f22005-07-23 14:52:12 +0000192 ** index. The first integer in the list is the total number of entires
193 ** in the index. There is one additional integer in the list for each
194 ** column of the table. This additional integer is a guess of how many
195 ** rows of the table the index will select. If D is the count of distinct
196 ** values and K is the total number of rows, then the integer is computed
197 ** as:
drhff2d5ea2005-07-23 00:41:48 +0000198 **
199 ** I = (K+D-1)/D
200 **
201 ** If K==0 then no entry is made into the sqlite_stat1 table.
202 ** If K>0 then it is always the case the D>0 so division by zero
203 ** is never possible.
204 */
drh2d401ab2008-01-10 23:50:11 +0000205 addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
206 sqlite3VdbeAddOp4(v, OP_String8, 0, regFields, 0, pTab->zName, 0);
207 sqlite3VdbeAddOp4(v, OP_String8, 0, regFields+1, 0, pIdx->zName, 0);
208 regF2 = regFields+2;
209 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regF2);
drhff2d5ea2005-07-23 00:41:48 +0000210 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000211 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
212 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
213 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
214 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
215 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
216 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
217 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
drhff2d5ea2005-07-23 00:41:48 +0000218 }
drh1db639c2008-01-17 02:36:28 +0000219 sqlite3VdbeAddOp4(v, OP_MakeRecord, regFields, 3, regRec, "aaa", 0);
drh2d401ab2008-01-10 23:50:11 +0000220 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
221 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
222 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drhd654be82005-09-20 17:42:23 +0000223 sqlite3VdbeJumpHere(v, addr);
drhff2d5ea2005-07-23 00:41:48 +0000224 }
225}
226
227/*
drh497e4462005-07-23 03:18:40 +0000228** Generate code that will cause the most recent index analysis to
229** be laoded into internal hash tables where is can be used.
230*/
231static void loadAnalysis(Parse *pParse, int iDb){
232 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +0000233 if( v ){
drh66a51672008-01-03 00:01:23 +0000234 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
drhcf1be452007-05-12 12:08:51 +0000235 }
drh497e4462005-07-23 03:18:40 +0000236}
237
238/*
drhff2d5ea2005-07-23 00:41:48 +0000239** Generate code that will do an analysis of an entire database
240*/
241static void analyzeDatabase(Parse *pParse, int iDb){
242 sqlite3 *db = pParse->db;
danielk1977e501b892006-01-09 06:29:47 +0000243 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
drhff2d5ea2005-07-23 00:41:48 +0000244 HashElem *k;
245 int iStatCur;
246 int iMem;
247
248 sqlite3BeginWriteOperation(pParse, 0, iDb);
249 iStatCur = pParse->nTab++;
250 openStatTable(pParse, iDb, iStatCur, 0);
drh0a07c102008-01-03 18:03:08 +0000251 iMem = pParse->nMem+1;
danielk1977da184232006-01-05 11:34:32 +0000252 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
drhff2d5ea2005-07-23 00:41:48 +0000253 Table *pTab = (Table*)sqliteHashData(k);
254 analyzeOneTable(pParse, pTab, iStatCur, iMem);
255 }
drh497e4462005-07-23 03:18:40 +0000256 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000257}
258
259/*
260** Generate code that will do an analysis of a single table in
261** a database.
262*/
263static void analyzeTable(Parse *pParse, Table *pTab){
264 int iDb;
265 int iStatCur;
266
267 assert( pTab!=0 );
drh1fee73e2007-08-29 04:00:57 +0000268 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000269 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhff2d5ea2005-07-23 00:41:48 +0000270 sqlite3BeginWriteOperation(pParse, 0, iDb);
271 iStatCur = pParse->nTab++;
272 openStatTable(pParse, iDb, iStatCur, pTab->zName);
drh0a07c102008-01-03 18:03:08 +0000273 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
drh497e4462005-07-23 03:18:40 +0000274 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000275}
276
277/*
278** Generate code for the ANALYZE command. The parser calls this routine
279** when it recognizes an ANALYZE command.
drh9f18e8a2005-07-08 12:13:04 +0000280**
281** ANALYZE -- 1
drhff2d5ea2005-07-23 00:41:48 +0000282** ANALYZE <database> -- 2
drh9f18e8a2005-07-08 12:13:04 +0000283** ANALYZE ?<database>.?<tablename> -- 3
284**
285** Form 1 causes all indices in all attached databases to be analyzed.
286** Form 2 analyzes all indices the single database named.
287** Form 3 analyzes all indices associated with the named table.
288*/
289void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
drhff2d5ea2005-07-23 00:41:48 +0000290 sqlite3 *db = pParse->db;
291 int iDb;
292 int i;
293 char *z, *zDb;
294 Table *pTab;
295 Token *pTableName;
296
297 /* Read the database schema. If an error occurs, leave an error message
298 ** and code in pParse and return NULL. */
drh1fee73e2007-08-29 04:00:57 +0000299 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhff2d5ea2005-07-23 00:41:48 +0000300 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
301 return;
302 }
303
304 if( pName1==0 ){
305 /* Form 1: Analyze everything */
306 for(i=0; i<db->nDb; i++){
307 if( i==1 ) continue; /* Do not analyze the TEMP database */
308 analyzeDatabase(pParse, i);
309 }
drhe6e04962005-07-23 02:17:03 +0000310 }else if( pName2==0 || pName2->n==0 ){
drhff2d5ea2005-07-23 00:41:48 +0000311 /* Form 2: Analyze the database or table named */
312 iDb = sqlite3FindDb(db, pName1);
313 if( iDb>=0 ){
314 analyzeDatabase(pParse, iDb);
drhe6e04962005-07-23 02:17:03 +0000315 }else{
drh17435752007-08-16 04:30:38 +0000316 z = sqlite3NameFromToken(db, pName1);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000317 if( z ){
drhca424112008-01-25 15:04:48 +0000318 pTab = sqlite3LocateTable(pParse, 0, z, 0);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000319 sqlite3_free(z);
320 if( pTab ){
321 analyzeTable(pParse, pTab);
322 }
drhe6e04962005-07-23 02:17:03 +0000323 }
drhff2d5ea2005-07-23 00:41:48 +0000324 }
drhff2d5ea2005-07-23 00:41:48 +0000325 }else{
326 /* Form 3: Analyze the fully qualified table name */
327 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
328 if( iDb>=0 ){
329 zDb = db->aDb[iDb].zName;
drh17435752007-08-16 04:30:38 +0000330 z = sqlite3NameFromToken(db, pTableName);
drhcf1be452007-05-12 12:08:51 +0000331 if( z ){
drhca424112008-01-25 15:04:48 +0000332 pTab = sqlite3LocateTable(pParse, 0, z, zDb);
drh17435752007-08-16 04:30:38 +0000333 sqlite3_free(z);
drhcf1be452007-05-12 12:08:51 +0000334 if( pTab ){
335 analyzeTable(pParse, pTab);
336 }
drhff2d5ea2005-07-23 00:41:48 +0000337 }
338 }
339 }
drh9f18e8a2005-07-08 12:13:04 +0000340}
341
drh497e4462005-07-23 03:18:40 +0000342/*
343** Used to pass information from the analyzer reader through to the
344** callback routine.
345*/
346typedef struct analysisInfo analysisInfo;
347struct analysisInfo {
348 sqlite3 *db;
349 const char *zDatabase;
350};
351
352/*
353** This callback is invoked once for each index when reading the
354** sqlite_stat1 table.
355**
356** argv[0] = name of the index
357** argv[1] = results of analysis - on integer for each column
358*/
359static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
360 analysisInfo *pInfo = (analysisInfo*)pData;
361 Index *pIndex;
362 int i, c;
363 unsigned int v;
364 const char *z;
365
366 assert( argc==2 );
drh1ec43c92005-09-06 10:26:47 +0000367 if( argv==0 || argv[0]==0 || argv[1]==0 ){
drh497e4462005-07-23 03:18:40 +0000368 return 0;
369 }
370 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
371 if( pIndex==0 ){
372 return 0;
373 }
374 z = argv[1];
drh17a18f22005-07-23 14:52:12 +0000375 for(i=0; *z && i<=pIndex->nColumn; i++){
drh497e4462005-07-23 03:18:40 +0000376 v = 0;
377 while( (c=z[0])>='0' && c<='9' ){
378 v = v*10 + c - '0';
379 z++;
380 }
381 pIndex->aiRowEst[i] = v;
382 if( *z==' ' ) z++;
383 }
384 return 0;
385}
386
387/*
388** Load the content of the sqlite_stat1 table into the index hash tables.
389*/
drhcf1be452007-05-12 12:08:51 +0000390int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
drh497e4462005-07-23 03:18:40 +0000391 analysisInfo sInfo;
392 HashElem *i;
393 char *zSql;
drhcf1be452007-05-12 12:08:51 +0000394 int rc;
drh497e4462005-07-23 03:18:40 +0000395
drhff0587c2007-08-29 17:43:19 +0000396 assert( iDb>=0 && iDb<db->nDb );
397 assert( db->aDb[iDb].pBt!=0 );
398 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
drh1fee73e2007-08-29 04:00:57 +0000399
drh497e4462005-07-23 03:18:40 +0000400 /* Clear any prior statistics */
danielk1977da184232006-01-05 11:34:32 +0000401 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
drh497e4462005-07-23 03:18:40 +0000402 Index *pIdx = sqliteHashData(i);
drh51147ba2005-07-23 22:59:55 +0000403 sqlite3DefaultRowEst(pIdx);
drh497e4462005-07-23 03:18:40 +0000404 }
405
406 /* Check to make sure the sqlite_stat1 table existss */
407 sInfo.db = db;
408 sInfo.zDatabase = db->aDb[iDb].zName;
409 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
drhcf1be452007-05-12 12:08:51 +0000410 return SQLITE_ERROR;
drh497e4462005-07-23 03:18:40 +0000411 }
412
413
414 /* Load new statistics out of the sqlite_stat1 table */
danielk19771e536952007-08-16 10:09:01 +0000415 zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
drh497e4462005-07-23 03:18:40 +0000416 sInfo.zDatabase);
drh7e8b8482008-01-23 03:03:05 +0000417 (void)sqlite3SafetyOff(db);
drhcf1be452007-05-12 12:08:51 +0000418 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
drh7e8b8482008-01-23 03:03:05 +0000419 (void)sqlite3SafetyOn(db);
drh17435752007-08-16 04:30:38 +0000420 sqlite3_free(zSql);
drhcf1be452007-05-12 12:08:51 +0000421 return rc;
drh497e4462005-07-23 03:18:40 +0000422}
drh9f18e8a2005-07-08 12:13:04 +0000423
drhff2d5ea2005-07-23 00:41:48 +0000424
drh9f18e8a2005-07-08 12:13:04 +0000425#endif /* SQLITE_OMIT_ANALYZE */