1 /* 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 ** 14 ** The ANALYZE command gather statistics about the content of tables 15 ** and indices. These statistics are made available to the query planner 16 ** to help it make better decisions about how to perform queries. 17 ** 18 ** The following system tables are or have been supported: 19 ** 20 ** CREATE TABLE sqlite_stat1(tbl, idx, stat); 21 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); 22 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); 23 ** 24 ** Additional tables might be added in future releases of SQLite. 25 ** The sqlite_stat2 table is not created or used unless the SQLite version 26 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled 27 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. 28 ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only 29 ** created and used by SQLite versions 3.7.9 and later and with 30 ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 31 ** is a superset of sqlite_stat2. 32 ** 33 ** Format of sqlite_stat1: 34 ** 35 ** There is normally one row per index, with the index identified by the 36 ** name in the idx column. The tbl column is the name of the table to 37 ** which the index belongs. In each such row, the stat column will be 38 ** a string consisting of a list of integers. The first integer in this 39 ** list is the number of rows in the index and in the table. The second 40 ** integer is the average number of rows in the index that have the same 41 ** value in the first column of the index. The third integer is the average 42 ** number of rows in the index that have the same value for the first two 43 ** columns. The N-th integer (for N>1) is the average number of rows in 44 ** the index which have the same value for the first N-1 columns. For 45 ** a K-column index, there will be K+1 integers in the stat column. If 46 ** the index is unique, then the last integer will be 1. 47 ** 48 ** The list of integers in the stat column can optionally be followed 49 ** by the keyword "unordered". The "unordered" keyword, if it is present, 50 ** must be separated from the last integer by a single space. If the 51 ** "unordered" keyword is present, then the query planner assumes that 52 ** the index is unordered and will not use the index for a range query. 53 ** 54 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat 55 ** column contains a single integer which is the (estimated) number of 56 ** rows in the table identified by sqlite_stat1.tbl. 57 ** 58 ** Format of sqlite_stat2: 59 ** 60 ** The sqlite_stat2 is only created and is only used if SQLite is compiled 61 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between 62 ** 3.6.18 and 3.7.8. The "stat2" table contains additional information 63 ** about the distribution of keys within an index. The index is identified by 64 ** the "idx" column and the "tbl" column is the name of the table to which 65 ** the index belongs. There are usually 10 rows in the sqlite_stat2 66 ** table for each index. 67 ** 68 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 69 ** inclusive are samples of the left-most key value in the index taken at 70 ** evenly spaced points along the index. Let the number of samples be S 71 ** (10 in the standard build) and let C be the number of rows in the index. 72 ** Then the sampled rows are given by: 73 ** 74 ** rownumber = (i*C*2 + C)/(S*2) 75 ** 76 ** For i between 0 and S-1. Conceptually, the index space is divided into 77 ** S uniform buckets and the samples are the middle row from each bucket. 78 ** 79 ** The format for sqlite_stat2 is recorded here for legacy reference. This 80 ** version of SQLite does not support sqlite_stat2. It neither reads nor 81 ** writes the sqlite_stat2 table. This version of SQLite only supports 82 ** sqlite_stat3. 83 ** 84 ** Format for sqlite_stat3: 85 ** 86 ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is 87 ** used to avoid compatibility problems. 88 ** 89 ** The format of the sqlite_stat3 table is similar to the format of 90 ** the sqlite_stat2 table. There are multiple entries for each index. 91 ** The idx column names the index and the tbl column is the table of the 92 ** index. If the idx and tbl columns are the same, then the sample is 93 ** of the INTEGER PRIMARY KEY. The sample column is a value taken from 94 ** the left-most column of the index. The nEq column is the approximate 95 ** number of entires in the index whose left-most column exactly matches 96 ** the sample. nLt is the approximate number of entires whose left-most 97 ** column is less than the sample. The nDLt column is the approximate 98 ** number of distinct left-most entries in the index that are less than 99 ** the sample. 100 ** 101 ** Future versions of SQLite might change to store a string containing 102 ** multiple integers values in the nDLt column of sqlite_stat3. The first 103 ** integer will be the number of prior index entires that are distinct in 104 ** the left-most column. The second integer will be the number of prior index 105 ** entries that are distinct in the first two columns. The third integer 106 ** will be the number of prior index entries that are distinct in the first 107 ** three columns. And so forth. With that extension, the nDLt field is 108 ** similar in function to the sqlite_stat1.stat field. 109 ** 110 ** There can be an arbitrary number of sqlite_stat3 entries per index. 111 ** The ANALYZE command will typically generate sqlite_stat3 tables 112 ** that contain between 10 and 40 samples which are distributed across 113 ** the key space, though not uniformly, and which include samples with 114 ** largest possible nEq values. 115 */ 116 #ifndef SQLITE_OMIT_ANALYZE 117 #include "sqliteInt.h" 118 119 /* 120 ** This routine generates code that opens the sqlite_stat1 table for 121 ** writing with cursor iStatCur. If the library was built with the 122 ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is 123 ** opened for writing using cursor (iStatCur+1) 124 ** 125 ** If the sqlite_stat1 tables does not previously exist, it is created. 126 ** Similarly, if the sqlite_stat3 table does not exist and the library 127 ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created. 128 ** 129 ** Argument zWhere may be a pointer to a buffer containing a table name, 130 ** or it may be a NULL pointer. If it is not NULL, then all entries in 131 ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated 132 ** with the named table are deleted. If zWhere==0, then code is generated 133 ** to delete all stat table entries. 134 */ 135 static void openStatTable( 136 Parse *pParse, /* Parsing context */ 137 int iDb, /* The database we are looking in */ 138 int iStatCur, /* Open the sqlite_stat1 table on this cursor */ 139 const char *zWhere, /* Delete entries for this table or index */ 140 const char *zWhereType /* Either "tbl" or "idx" */ 141 ){ 142 static const struct { 143 const char *zName; 144 const char *zCols; 145 } aTable[] = { 146 { "sqlite_stat1", "tbl,idx,stat" }, 147 #ifdef SQLITE_ENABLE_STAT3 148 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, 149 #endif 150 }; 151 152 int aRoot[] = {0, 0}; 153 u8 aCreateTbl[] = {0, 0}; 154 155 int i; 156 sqlite3 *db = pParse->db; 157 Db *pDb; 158 Vdbe *v = sqlite3GetVdbe(pParse); 159 if( v==0 ) return; 160 assert( sqlite3BtreeHoldsAllMutexes(db) ); 161 assert( sqlite3VdbeDb(v)==db ); 162 pDb = &db->aDb[iDb]; 163 164 /* Create new statistic tables if they do not exist, or clear them 165 ** if they do already exist. 166 */ 167 for(i=0; i<ArraySize(aTable); i++){ 168 const char *zTab = aTable[i].zName; 169 Table *pStat; 170 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ 171 /* The sqlite_stat[12] table does not exist. Create it. Note that a 172 ** side-effect of the CREATE TABLE statement is to leave the rootpage 173 ** of the new table in register pParse->regRoot. This is important 174 ** because the OpenWrite opcode below will be needing it. */ 175 sqlite3NestedParse(pParse, 176 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols 177 ); 178 aRoot[i] = pParse->regRoot; 179 aCreateTbl[i] = OPFLAG_P2ISREG; 180 }else{ 181 /* The table already exists. If zWhere is not NULL, delete all entries 182 ** associated with the table zWhere. If zWhere is NULL, delete the 183 ** entire contents of the table. */ 184 aRoot[i] = pStat->tnum; 185 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); 186 if( zWhere ){ 187 sqlite3NestedParse(pParse, 188 "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere 189 ); 190 }else{ 191 /* The sqlite_stat[12] table already exists. Delete all rows. */ 192 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); 193 } 194 } 195 } 196 197 /* Open the sqlite_stat[13] tables for writing. */ 198 for(i=0; i<ArraySize(aTable); i++){ 199 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); 200 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); 201 sqlite3VdbeChangeP5(v, aCreateTbl[i]); 202 } 203 } 204 205 /* 206 ** Recommended number of samples for sqlite_stat3 207 */ 208 #ifndef SQLITE_STAT3_SAMPLES 209 # define SQLITE_STAT3_SAMPLES 24 210 #endif 211 212 /* 213 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() - 214 ** share an instance of the following structure to hold their state 215 ** information. 216 */ 217 typedef struct Stat3Accum Stat3Accum; 218 struct Stat3Accum { 219 tRowcnt nRow; /* Number of rows in the entire table */ 220 tRowcnt nPSample; /* How often to do a periodic sample */ 221 int iMin; /* Index of entry with minimum nEq and hash */ 222 int mxSample; /* Maximum number of samples to accumulate */ 223 int nSample; /* Current number of samples */ 224 u32 iPrn; /* Pseudo-random number used for sampling */ 225 struct Stat3Sample { 226 i64 iRowid; /* Rowid in main table of the key */ 227 tRowcnt nEq; /* sqlite_stat3.nEq */ 228 tRowcnt nLt; /* sqlite_stat3.nLt */ 229 tRowcnt nDLt; /* sqlite_stat3.nDLt */ 230 u8 isPSample; /* True if a periodic sample */ 231 u32 iHash; /* Tiebreaker hash */ 232 } *a; /* An array of samples */ 233 }; 234 235 #ifdef SQLITE_ENABLE_STAT3 236 /* 237 ** Implementation of the stat3_init(C,S) SQL function. The two parameters 238 ** are the number of rows in the table or index (C) and the number of samples 239 ** to accumulate (S). 240 ** 241 ** This routine allocates the Stat3Accum object. 242 ** 243 ** The return value is the Stat3Accum object (P). 244 */ 245 static void stat3Init( 246 sqlite3_context *context, 247 int argc, 248 sqlite3_value **argv 249 ){ 250 Stat3Accum *p; 251 tRowcnt nRow; 252 int mxSample; 253 int n; 254 255 UNUSED_PARAMETER(argc); 256 nRow = (tRowcnt)sqlite3_value_int64(argv[0]); 257 mxSample = sqlite3_value_int(argv[1]); 258 n = sizeof(*p) + sizeof(p->a[0])*mxSample; 259 p = sqlite3MallocZero( n ); 260 if( p==0 ){ 261 sqlite3_result_error_nomem(context); 262 return; 263 } 264 p->a = (struct Stat3Sample*)&p[1]; 265 p->nRow = nRow; 266 p->mxSample = mxSample; 267 p->nPSample = p->nRow/(mxSample/3+1) + 1; 268 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn); 269 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free); 270 } 271 static const FuncDef stat3InitFuncdef = { 272 2, /* nArg */ 273 SQLITE_UTF8, /* iPrefEnc */ 274 0, /* flags */ 275 0, /* pUserData */ 276 0, /* pNext */ 277 stat3Init, /* xFunc */ 278 0, /* xStep */ 279 0, /* xFinalize */ 280 "stat3_init", /* zName */ 281 0, /* pHash */ 282 0 /* pDestructor */ 283 }; 284 285 286 /* 287 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The 288 ** arguments describe a single key instance. This routine makes the 289 ** decision about whether or not to retain this key for the sqlite_stat3 290 ** table. 291 ** 292 ** The return value is NULL. 293 */ 294 static void stat3Push( 295 sqlite3_context *context, 296 int argc, 297 sqlite3_value **argv 298 ){ 299 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]); 300 tRowcnt nEq = sqlite3_value_int64(argv[0]); 301 tRowcnt nLt = sqlite3_value_int64(argv[1]); 302 tRowcnt nDLt = sqlite3_value_int64(argv[2]); 303 i64 rowid = sqlite3_value_int64(argv[3]); 304 u8 isPSample = 0; 305 u8 doInsert = 0; 306 int iMin = p->iMin; 307 struct Stat3Sample *pSample; 308 int i; 309 u32 h; 310 311 UNUSED_PARAMETER(context); 312 UNUSED_PARAMETER(argc); 313 if( nEq==0 ) return; 314 h = p->iPrn = p->iPrn*1103515245 + 12345; 315 if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){ 316 doInsert = isPSample = 1; 317 }else if( p->nSample<p->mxSample ){ 318 doInsert = 1; 319 }else{ 320 if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){ 321 doInsert = 1; 322 } 323 } 324 if( !doInsert ) return; 325 if( p->nSample==p->mxSample ){ 326 assert( p->nSample - iMin - 1 >= 0 ); 327 memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1)); 328 pSample = &p->a[p->nSample-1]; 329 }else{ 330 pSample = &p->a[p->nSample++]; 331 } 332 pSample->iRowid = rowid; 333 pSample->nEq = nEq; 334 pSample->nLt = nLt; 335 pSample->nDLt = nDLt; 336 pSample->iHash = h; 337 pSample->isPSample = isPSample; 338 339 /* Find the new minimum */ 340 if( p->nSample==p->mxSample ){ 341 pSample = p->a; 342 i = 0; 343 while( pSample->isPSample ){ 344 i++; 345 pSample++; 346 assert( i<p->nSample ); 347 } 348 nEq = pSample->nEq; 349 h = pSample->iHash; 350 iMin = i; 351 for(i++, pSample++; i<p->nSample; i++, pSample++){ 352 if( pSample->isPSample ) continue; 353 if( pSample->nEq<nEq 354 || (pSample->nEq==nEq && pSample->iHash<h) 355 ){ 356 iMin = i; 357 nEq = pSample->nEq; 358 h = pSample->iHash; 359 } 360 } 361 p->iMin = iMin; 362 } 363 } 364 static const FuncDef stat3PushFuncdef = { 365 5, /* nArg */ 366 SQLITE_UTF8, /* iPrefEnc */ 367 0, /* flags */ 368 0, /* pUserData */ 369 0, /* pNext */ 370 stat3Push, /* xFunc */ 371 0, /* xStep */ 372 0, /* xFinalize */ 373 "stat3_push", /* zName */ 374 0, /* pHash */ 375 0 /* pDestructor */ 376 }; 377 378 /* 379 ** Implementation of the stat3_get(P,N,...) SQL function. This routine is 380 ** used to query the results. Content is returned for the Nth sqlite_stat3 381 ** row where N is between 0 and S-1 and S is the number of samples. The 382 ** value returned depends on the number of arguments. 383 ** 384 ** argc==2 result: rowid 385 ** argc==3 result: nEq 386 ** argc==4 result: nLt 387 ** argc==5 result: nDLt 388 */ 389 static void stat3Get( 390 sqlite3_context *context, 391 int argc, 392 sqlite3_value **argv 393 ){ 394 int n = sqlite3_value_int(argv[1]); 395 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); 396 397 assert( p!=0 ); 398 if( p->nSample<=n ) return; 399 switch( argc ){ 400 case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; 401 case 3: sqlite3_result_int64(context, p->a[n].nEq); break; 402 case 4: sqlite3_result_int64(context, p->a[n].nLt); break; 403 default: sqlite3_result_int64(context, p->a[n].nDLt); break; 404 } 405 } 406 static const FuncDef stat3GetFuncdef = { 407 -1, /* nArg */ 408 SQLITE_UTF8, /* iPrefEnc */ 409 0, /* flags */ 410 0, /* pUserData */ 411 0, /* pNext */ 412 stat3Get, /* xFunc */ 413 0, /* xStep */ 414 0, /* xFinalize */ 415 "stat3_get", /* zName */ 416 0, /* pHash */ 417 0 /* pDestructor */ 418 }; 419 #endif /* SQLITE_ENABLE_STAT3 */ 420 421 422 423 424 /* 425 ** Generate code to do an analysis of all indices associated with 426 ** a single table. 427 */ 428 static void analyzeOneTable( 429 Parse *pParse, /* Parser context */ 430 Table *pTab, /* Table whose indices are to be analyzed */ 431 Index *pOnlyIdx, /* If not NULL, only analyze this one index */ 432 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ 433 int iMem /* Available memory locations begin here */ 434 ){ 435 sqlite3 *db = pParse->db; /* Database handle */ 436 Index *pIdx; /* An index to being analyzed */ 437 int iIdxCur; /* Cursor open on index being analyzed */ 438 Vdbe *v; /* The virtual machine being built up */ 439 int i; /* Loop counter */ 440 int topOfLoop; /* The top of the loop */ 441 int endOfLoop; /* The end of the loop */ 442 int jZeroRows = -1; /* Jump from here if number of rows is zero */ 443 int iDb; /* Index of database containing pTab */ 444 int regTabname = iMem++; /* Register containing table name */ 445 int regIdxname = iMem++; /* Register containing index name */ 446 int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ 447 #ifdef SQLITE_ENABLE_STAT3 448 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ 449 int regNumLt = iMem++; /* Number of keys less than regSample */ 450 int regNumDLt = iMem++; /* Number of distinct keys less than regSample */ 451 int regSample = iMem++; /* The next sample value */ 452 int regRowid = regSample; /* Rowid of a sample */ 453 int regAccum = iMem++; /* Register to hold Stat3Accum object */ 454 int regLoop = iMem++; /* Loop counter */ 455 int regCount = iMem++; /* Number of rows in the table or index */ 456 int regTemp1 = iMem++; /* Intermediate register */ 457 int regTemp2 = iMem++; /* Intermediate register */ 458 int once = 1; /* One-time initialization */ 459 int shortJump = 0; /* Instruction address */ 460 int iTabCur = pParse->nTab++; /* Table cursor */ 461 #endif 462 int regCol = iMem++; /* Content of a column in analyzed table */ 463 int regRec = iMem++; /* Register holding completed record */ 464 int regTemp = iMem++; /* Temporary use register */ 465 int regNewRowid = iMem++; /* Rowid for the inserted record */ 466 467 468 v = sqlite3GetVdbe(pParse); 469 if( v==0 || NEVER(pTab==0) ){ 470 return; 471 } 472 if( pTab->tnum==0 ){ 473 /* Do not gather statistics on views or virtual tables */ 474 return; 475 } 476 if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ 477 /* Do not gather statistics on system tables */ 478 return; 479 } 480 assert( sqlite3BtreeHoldsAllMutexes(db) ); 481 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 482 assert( iDb>=0 ); 483 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 484 #ifndef SQLITE_OMIT_AUTHORIZATION 485 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, 486 db->aDb[iDb].zName ) ){ 487 return; 488 } 489 #endif 490 491 /* Establish a read-lock on the table at the shared-cache level. */ 492 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 493 494 iIdxCur = pParse->nTab++; 495 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); 496 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 497 int nCol; 498 KeyInfo *pKey; 499 int addrIfNot = 0; /* address of OP_IfNot */ 500 int *aChngAddr; /* Array of jump instruction addresses */ 501 502 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; 503 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName)); 504 nCol = pIdx->nColumn; 505 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol); 506 if( aChngAddr==0 ) continue; 507 pKey = sqlite3IndexKeyinfo(pParse, pIdx); 508 if( iMem+1+(nCol*2)>pParse->nMem ){ 509 pParse->nMem = iMem+1+(nCol*2); 510 } 511 512 /* Open a cursor to the index to be analyzed. */ 513 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); 514 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, 515 (char *)pKey, P4_KEYINFO_HANDOFF); 516 VdbeComment((v, "%s", pIdx->zName)); 517 518 /* Populate the register containing the index name. */ 519 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); 520 521 #ifdef SQLITE_ENABLE_STAT3 522 if( once ){ 523 once = 0; 524 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); 525 } 526 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); 527 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); 528 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); 529 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); 530 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt); 531 sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum); 532 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, 533 (char*)&stat3InitFuncdef, P4_FUNCDEF); 534 sqlite3VdbeChangeP5(v, 2); 535 #endif /* SQLITE_ENABLE_STAT3 */ 536 537 /* The block of memory cells initialized here is used as follows. 538 ** 539 ** iMem: 540 ** The total number of rows in the table. 541 ** 542 ** iMem+1 .. iMem+nCol: 543 ** Number of distinct entries in index considering the 544 ** left-most N columns only, where N is between 1 and nCol, 545 ** inclusive. 546 ** 547 ** iMem+nCol+1 .. Mem+2*nCol: 548 ** Previous value of indexed columns, from left to right. 549 ** 550 ** Cells iMem through iMem+nCol are initialized to 0. The others are 551 ** initialized to contain an SQL NULL. 552 */ 553 for(i=0; i<=nCol; i++){ 554 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i); 555 } 556 for(i=0; i<nCol; i++){ 557 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1); 558 } 559 560 /* Start the analysis loop. This loop runs through all the entries in 561 ** the index b-tree. */ 562 endOfLoop = sqlite3VdbeMakeLabel(v); 563 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); 564 topOfLoop = sqlite3VdbeCurrentAddr(v); 565 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */ 566 567 for(i=0; i<nCol; i++){ 568 CollSeq *pColl; 569 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); 570 if( i==0 ){ 571 /* Always record the very first row */ 572 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); 573 } 574 assert( pIdx->azColl!=0 ); 575 assert( pIdx->azColl[i]!=0 ); 576 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); 577 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, 578 (char*)pColl, P4_COLLSEQ); 579 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); 580 VdbeComment((v, "jump if column %d changed", i)); 581 #ifdef SQLITE_ENABLE_STAT3 582 if( i==0 ){ 583 sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1); 584 VdbeComment((v, "incr repeat count")); 585 } 586 #endif 587 } 588 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); 589 for(i=0; i<nCol; i++){ 590 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ 591 if( i==0 ){ 592 sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ 593 #ifdef SQLITE_ENABLE_STAT3 594 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, 595 (char*)&stat3PushFuncdef, P4_FUNCDEF); 596 sqlite3VdbeChangeP5(v, 5); 597 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid); 598 sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt); 599 sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1); 600 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq); 601 #endif 602 } 603 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); 604 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); 605 } 606 sqlite3DbFree(db, aChngAddr); 607 608 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ 609 sqlite3VdbeResolveLabel(v, endOfLoop); 610 611 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); 612 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); 613 #ifdef SQLITE_ENABLE_STAT3 614 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, 615 (char*)&stat3PushFuncdef, P4_FUNCDEF); 616 sqlite3VdbeChangeP5(v, 5); 617 sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop); 618 shortJump = 619 sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1); 620 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1, 621 (char*)&stat3GetFuncdef, P4_FUNCDEF); 622 sqlite3VdbeChangeP5(v, 2); 623 sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1); 624 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1); 625 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample); 626 sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample); 627 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq, 628 (char*)&stat3GetFuncdef, P4_FUNCDEF); 629 sqlite3VdbeChangeP5(v, 3); 630 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt, 631 (char*)&stat3GetFuncdef, P4_FUNCDEF); 632 sqlite3VdbeChangeP5(v, 4); 633 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt, 634 (char*)&stat3GetFuncdef, P4_FUNCDEF); 635 sqlite3VdbeChangeP5(v, 5); 636 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0); 637 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); 638 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid); 639 sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump); 640 sqlite3VdbeJumpHere(v, shortJump+2); 641 #endif 642 643 /* Store the results in sqlite_stat1. 644 ** 645 ** The result is a single row of the sqlite_stat1 table. The first 646 ** two columns are the names of the table and index. The third column 647 ** is a string composed of a list of integer statistics about the 648 ** index. The first integer in the list is the total number of entries 649 ** in the index. There is one additional integer in the list for each 650 ** column of the table. This additional integer is a guess of how many 651 ** rows of the table the index will select. If D is the count of distinct 652 ** values and K is the total number of rows, then the integer is computed 653 ** as: 654 ** 655 ** I = (K+D-1)/D 656 ** 657 ** If K==0 then no entry is made into the sqlite_stat1 table. 658 ** If K>0 then it is always the case the D>0 so division by zero 659 ** is never possible. 660 */ 661 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1); 662 if( jZeroRows<0 ){ 663 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); 664 } 665 for(i=0; i<nCol; i++){ 666 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); 667 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); 668 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); 669 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); 670 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); 671 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); 672 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); 673 } 674 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); 675 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); 676 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); 677 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 678 } 679 680 /* If the table has no indices, create a single sqlite_stat1 entry 681 ** containing NULL as the index name and the row count as the content. 682 */ 683 if( pTab->pIndex==0 ){ 684 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); 685 VdbeComment((v, "%s", pTab->zName)); 686 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1); 687 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); 688 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); 689 }else{ 690 sqlite3VdbeJumpHere(v, jZeroRows); 691 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto); 692 } 693 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); 694 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); 695 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); 696 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); 697 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 698 if( pParse->nMem<regRec ) pParse->nMem = regRec; 699 sqlite3VdbeJumpHere(v, jZeroRows); 700 } 701 702 703 /* 704 ** Generate code that will cause the most recent index analysis to 705 ** be loaded into internal hash tables where is can be used. 706 */ 707 static void loadAnalysis(Parse *pParse, int iDb){ 708 Vdbe *v = sqlite3GetVdbe(pParse); 709 if( v ){ 710 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); 711 } 712 } 713 714 /* 715 ** Generate code that will do an analysis of an entire database 716 */ 717 static void analyzeDatabase(Parse *pParse, int iDb){ 718 sqlite3 *db = pParse->db; 719 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ 720 HashElem *k; 721 int iStatCur; 722 int iMem; 723 724 sqlite3BeginWriteOperation(pParse, 0, iDb); 725 iStatCur = pParse->nTab; 726 pParse->nTab += 3; 727 openStatTable(pParse, iDb, iStatCur, 0, 0); 728 iMem = pParse->nMem+1; 729 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 730 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ 731 Table *pTab = (Table*)sqliteHashData(k); 732 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem); 733 } 734 loadAnalysis(pParse, iDb); 735 } 736 737 /* 738 ** Generate code that will do an analysis of a single table in 739 ** a database. If pOnlyIdx is not NULL then it is a single index 740 ** in pTab that should be analyzed. 741 */ 742 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){ 743 int iDb; 744 int iStatCur; 745 746 assert( pTab!=0 ); 747 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); 748 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 749 sqlite3BeginWriteOperation(pParse, 0, iDb); 750 iStatCur = pParse->nTab; 751 pParse->nTab += 3; 752 if( pOnlyIdx ){ 753 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); 754 }else{ 755 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); 756 } 757 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1); 758 loadAnalysis(pParse, iDb); 759 } 760 761 /* 762 ** Generate code for the ANALYZE command. The parser calls this routine 763 ** when it recognizes an ANALYZE command. 764 ** 765 ** ANALYZE -- 1 766 ** ANALYZE <database> -- 2 767 ** ANALYZE ?<database>.?<tablename> -- 3 768 ** 769 ** Form 1 causes all indices in all attached databases to be analyzed. 770 ** Form 2 analyzes all indices the single database named. 771 ** Form 3 analyzes all indices associated with the named table. 772 */ 773 void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ 774 sqlite3 *db = pParse->db; 775 int iDb; 776 int i; 777 char *z, *zDb; 778 Table *pTab; 779 Index *pIdx; 780 Token *pTableName; 781 782 /* Read the database schema. If an error occurs, leave an error message 783 ** and code in pParse and return NULL. */ 784 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); 785 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ 786 return; 787 } 788 789 assert( pName2!=0 || pName1==0 ); 790 if( pName1==0 ){ 791 /* Form 1: Analyze everything */ 792 for(i=0; i<db->nDb; i++){ 793 if( i==1 ) continue; /* Do not analyze the TEMP database */ 794 analyzeDatabase(pParse, i); 795 } 796 }else if( pName2->n==0 ){ 797 /* Form 2: Analyze the database or table named */ 798 iDb = sqlite3FindDb(db, pName1); 799 if( iDb>=0 ){ 800 analyzeDatabase(pParse, iDb); 801 }else{ 802 z = sqlite3NameFromToken(db, pName1); 803 if( z ){ 804 if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ 805 analyzeTable(pParse, pIdx->pTable, pIdx); 806 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ 807 analyzeTable(pParse, pTab, 0); 808 } 809 sqlite3DbFree(db, z); 810 } 811 } 812 }else{ 813 /* Form 3: Analyze the fully qualified table name */ 814 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); 815 if( iDb>=0 ){ 816 zDb = db->aDb[iDb].zName; 817 z = sqlite3NameFromToken(db, pTableName); 818 if( z ){ 819 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ 820 analyzeTable(pParse, pIdx->pTable, pIdx); 821 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){ 822 analyzeTable(pParse, pTab, 0); 823 } 824 sqlite3DbFree(db, z); 825 } 826 } 827 } 828 } 829 830 /* 831 ** Used to pass information from the analyzer reader through to the 832 ** callback routine. 833 */ 834 typedef struct analysisInfo analysisInfo; 835 struct analysisInfo { 836 sqlite3 *db; 837 const char *zDatabase; 838 }; 839 840 /* 841 ** This callback is invoked once for each index when reading the 842 ** sqlite_stat1 table. 843 ** 844 ** argv[0] = name of the table 845 ** argv[1] = name of the index (might be NULL) 846 ** argv[2] = results of analysis - on integer for each column 847 ** 848 ** Entries for which argv[1]==NULL simply record the number of rows in 849 ** the table. 850 */ 851 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ 852 analysisInfo *pInfo = (analysisInfo*)pData; 853 Index *pIndex; 854 Table *pTable; 855 int i, c, n; 856 tRowcnt v; 857 const char *z; 858 859 assert( argc==3 ); 860 UNUSED_PARAMETER2(NotUsed, argc); 861 862 if( argv==0 || argv[0]==0 || argv[2]==0 ){ 863 return 0; 864 } 865 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); 866 if( pTable==0 ){ 867 return 0; 868 } 869 if( argv[1] ){ 870 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); 871 }else{ 872 pIndex = 0; 873 } 874 n = pIndex ? pIndex->nColumn : 0; 875 z = argv[2]; 876 for(i=0; *z && i<=n; i++){ 877 v = 0; 878 while( (c=z[0])>='0' && c<='9' ){ 879 v = v*10 + c - '0'; 880 z++; 881 } 882 if( i==0 ) pTable->nRowEst = v; 883 if( pIndex==0 ) break; 884 pIndex->aiRowEst[i] = v; 885 if( *z==' ' ) z++; 886 if( memcmp(z, "unordered", 10)==0 ){ 887 pIndex->bUnordered = 1; 888 break; 889 } 890 } 891 return 0; 892 } 893 894 /* 895 ** If the Index.aSample variable is not NULL, delete the aSample[] array 896 ** and its contents. 897 */ 898 void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ 899 #ifdef SQLITE_ENABLE_STAT3 900 if( pIdx->aSample ){ 901 int j; 902 for(j=0; j<pIdx->nSample; j++){ 903 IndexSample *p = &pIdx->aSample[j]; 904 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ 905 sqlite3DbFree(db, p->u.z); 906 } 907 } 908 sqlite3DbFree(db, pIdx->aSample); 909 } 910 if( db && db->pnBytesFreed==0 ){ 911 pIdx->nSample = 0; 912 pIdx->aSample = 0; 913 } 914 #else 915 UNUSED_PARAMETER(db); 916 UNUSED_PARAMETER(pIdx); 917 #endif 918 } 919 920 #ifdef SQLITE_ENABLE_STAT3 921 /* 922 ** Load content from the sqlite_stat3 table into the Index.aSample[] 923 ** arrays of all indices. 924 */ 925 static int loadStat3(sqlite3 *db, const char *zDb){ 926 int rc; /* Result codes from subroutines */ 927 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ 928 char *zSql; /* Text of the SQL statement */ 929 Index *pPrevIdx = 0; /* Previous index in the loop */ 930 int idx = 0; /* slot in pIdx->aSample[] for next sample */ 931 int eType; /* Datatype of a sample */ 932 IndexSample *pSample; /* A slot in pIdx->aSample[] */ 933 934 assert( db->lookaside.bEnabled==0 ); 935 if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){ 936 return SQLITE_OK; 937 } 938 939 zSql = sqlite3MPrintf(db, 940 "SELECT idx,count(*) FROM %Q.sqlite_stat3" 941 " GROUP BY idx", zDb); 942 if( !zSql ){ 943 return SQLITE_NOMEM; 944 } 945 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); 946 sqlite3DbFree(db, zSql); 947 if( rc ) return rc; 948 949 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 950 char *zIndex; /* Index name */ 951 Index *pIdx; /* Pointer to the index object */ 952 int nSample; /* Number of samples */ 953 954 zIndex = (char *)sqlite3_column_text(pStmt, 0); 955 if( zIndex==0 ) continue; 956 nSample = sqlite3_column_int(pStmt, 1); 957 pIdx = sqlite3FindIndex(db, zIndex, zDb); 958 if( pIdx==0 ) continue; 959 assert( pIdx->nSample==0 ); 960 pIdx->nSample = nSample; 961 pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample)); 962 pIdx->avgEq = pIdx->aiRowEst[1]; 963 if( pIdx->aSample==0 ){ 964 db->mallocFailed = 1; 965 sqlite3_finalize(pStmt); 966 return SQLITE_NOMEM; 967 } 968 } 969 rc = sqlite3_finalize(pStmt); 970 if( rc ) return rc; 971 972 zSql = sqlite3MPrintf(db, 973 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb); 974 if( !zSql ){ 975 return SQLITE_NOMEM; 976 } 977 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); 978 sqlite3DbFree(db, zSql); 979 if( rc ) return rc; 980 981 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 982 char *zIndex; /* Index name */ 983 Index *pIdx; /* Pointer to the index object */ 984 int i; /* Loop counter */ 985 tRowcnt sumEq; /* Sum of the nEq values */ 986 987 zIndex = (char *)sqlite3_column_text(pStmt, 0); 988 if( zIndex==0 ) continue; 989 pIdx = sqlite3FindIndex(db, zIndex, zDb); 990 if( pIdx==0 ) continue; 991 if( pIdx==pPrevIdx ){ 992 idx++; 993 }else{ 994 pPrevIdx = pIdx; 995 idx = 0; 996 } 997 assert( idx<pIdx->nSample ); 998 pSample = &pIdx->aSample[idx]; 999 pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1); 1000 pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2); 1001 pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3); 1002 if( idx==pIdx->nSample-1 ){ 1003 if( pSample->nDLt>0 ){ 1004 for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq; 1005 pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt; 1006 } 1007 if( pIdx->avgEq<=0 ) pIdx->avgEq = 1; 1008 } 1009 eType = sqlite3_column_type(pStmt, 4); 1010 pSample->eType = (u8)eType; 1011 switch( eType ){ 1012 case SQLITE_INTEGER: { 1013 pSample->u.i = sqlite3_column_int64(pStmt, 4); 1014 break; 1015 } 1016 case SQLITE_FLOAT: { 1017 pSample->u.r = sqlite3_column_double(pStmt, 4); 1018 break; 1019 } 1020 case SQLITE_NULL: { 1021 break; 1022 } 1023 default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); { 1024 const char *z = (const char *)( 1025 (eType==SQLITE_BLOB) ? 1026 sqlite3_column_blob(pStmt, 4): 1027 sqlite3_column_text(pStmt, 4) 1028 ); 1029 int n = z ? sqlite3_column_bytes(pStmt, 4) : 0; 1030 pSample->nByte = n; 1031 if( n < 1){ 1032 pSample->u.z = 0; 1033 }else{ 1034 pSample->u.z = sqlite3DbMallocRaw(db, n); 1035 if( pSample->u.z==0 ){ 1036 db->mallocFailed = 1; 1037 sqlite3_finalize(pStmt); 1038 return SQLITE_NOMEM; 1039 } 1040 memcpy(pSample->u.z, z, n); 1041 } 1042 } 1043 } 1044 } 1045 return sqlite3_finalize(pStmt); 1046 } 1047 #endif /* SQLITE_ENABLE_STAT3 */ 1048 1049 /* 1050 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The 1051 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] 1052 ** arrays. The contents of sqlite_stat3 are used to populate the 1053 ** Index.aSample[] arrays. 1054 ** 1055 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR 1056 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 1057 ** during compilation and the sqlite_stat3 table is present, no data is 1058 ** read from it. 1059 ** 1060 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the 1061 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is 1062 ** returned. However, in this case, data is read from the sqlite_stat1 1063 ** table (if it is present) before returning. 1064 ** 1065 ** If an OOM error occurs, this function always sets db->mallocFailed. 1066 ** This means if the caller does not care about other errors, the return 1067 ** code may be ignored. 1068 */ 1069 int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ 1070 analysisInfo sInfo; 1071 HashElem *i; 1072 char *zSql; 1073 int rc; 1074 1075 assert( iDb>=0 && iDb<db->nDb ); 1076 assert( db->aDb[iDb].pBt!=0 ); 1077 1078 /* Clear any prior statistics */ 1079 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 1080 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ 1081 Index *pIdx = sqliteHashData(i); 1082 sqlite3DefaultRowEst(pIdx); 1083 #ifdef SQLITE_ENABLE_STAT3 1084 sqlite3DeleteIndexSamples(db, pIdx); 1085 pIdx->aSample = 0; 1086 #endif 1087 } 1088 1089 /* Check to make sure the sqlite_stat1 table exists */ 1090 sInfo.db = db; 1091 sInfo.zDatabase = db->aDb[iDb].zName; 1092 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ 1093 return SQLITE_ERROR; 1094 } 1095 1096 /* Load new statistics out of the sqlite_stat1 table */ 1097 zSql = sqlite3MPrintf(db, 1098 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); 1099 if( zSql==0 ){ 1100 rc = SQLITE_NOMEM; 1101 }else{ 1102 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); 1103 sqlite3DbFree(db, zSql); 1104 } 1105 1106 1107 /* Load the statistics from the sqlite_stat3 table. */ 1108 #ifdef SQLITE_ENABLE_STAT3 1109 if( rc==SQLITE_OK ){ 1110 int lookasideEnabled = db->lookaside.bEnabled; 1111 db->lookaside.bEnabled = 0; 1112 rc = loadStat3(db, sInfo.zDatabase); 1113 db->lookaside.bEnabled = lookasideEnabled; 1114 } 1115 #endif 1116 1117 if( rc==SQLITE_NOMEM ){ 1118 db->mallocFailed = 1; 1119 } 1120 return rc; 1121 } 1122 1123 1124 #endif /* SQLITE_OMIT_ANALYZE */ 1125