analyze.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /*
  2. ** 2005-07-08
  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. ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
  24. **
  25. ** Additional tables might be added in future releases of SQLite.
  26. ** The sqlite_stat2 table is not created or used unless the SQLite version
  27. ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
  28. ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
  29. ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
  30. ** created and used by SQLite versions 3.7.9 and later and with
  31. ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
  32. ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
  33. ** version of sqlite_stat3 and is only available when compiled with
  34. ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
  35. ** not possible to enable both STAT3 and STAT4 at the same time. If they
  36. ** are both enabled, then STAT4 takes precedence.
  37. **
  38. ** For most applications, sqlite_stat1 provides all the statisics required
  39. ** for the query planner to make good choices.
  40. **
  41. ** Format of sqlite_stat1:
  42. **
  43. ** There is normally one row per index, with the index identified by the
  44. ** name in the idx column. The tbl column is the name of the table to
  45. ** which the index belongs. In each such row, the stat column will be
  46. ** a string consisting of a list of integers. The first integer in this
  47. ** list is the number of rows in the index. (This is the same as the
  48. ** number of rows in the table, except for partial indices.) The second
  49. ** integer is the average number of rows in the index that have the same
  50. ** value in the first column of the index. The third integer is the average
  51. ** number of rows in the index that have the same value for the first two
  52. ** columns. The N-th integer (for N>1) is the average number of rows in
  53. ** the index which have the same value for the first N-1 columns. For
  54. ** a K-column index, there will be K+1 integers in the stat column. If
  55. ** the index is unique, then the last integer will be 1.
  56. **
  57. ** The list of integers in the stat column can optionally be followed
  58. ** by the keyword "unordered". The "unordered" keyword, if it is present,
  59. ** must be separated from the last integer by a single space. If the
  60. ** "unordered" keyword is present, then the query planner assumes that
  61. ** the index is unordered and will not use the index for a range query.
  62. **
  63. ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
  64. ** column contains a single integer which is the (estimated) number of
  65. ** rows in the table identified by sqlite_stat1.tbl.
  66. **
  67. ** Format of sqlite_stat2:
  68. **
  69. ** The sqlite_stat2 is only created and is only used if SQLite is compiled
  70. ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
  71. ** 3.6.18 and 3.7.8. The "stat2" table contains additional information
  72. ** about the distribution of keys within an index. The index is identified by
  73. ** the "idx" column and the "tbl" column is the name of the table to which
  74. ** the index belongs. There are usually 10 rows in the sqlite_stat2
  75. ** table for each index.
  76. **
  77. ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
  78. ** inclusive are samples of the left-most key value in the index taken at
  79. ** evenly spaced points along the index. Let the number of samples be S
  80. ** (10 in the standard build) and let C be the number of rows in the index.
  81. ** Then the sampled rows are given by:
  82. **
  83. ** rownumber = (i*C*2 + C)/(S*2)
  84. **
  85. ** For i between 0 and S-1. Conceptually, the index space is divided into
  86. ** S uniform buckets and the samples are the middle row from each bucket.
  87. **
  88. ** The format for sqlite_stat2 is recorded here for legacy reference. This
  89. ** version of SQLite does not support sqlite_stat2. It neither reads nor
  90. ** writes the sqlite_stat2 table. This version of SQLite only supports
  91. ** sqlite_stat3.
  92. **
  93. ** Format for sqlite_stat3:
  94. **
  95. ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
  96. ** sqlite_stat4 format will be described first. Further information
  97. ** about sqlite_stat3 follows the sqlite_stat4 description.
  98. **
  99. ** Format for sqlite_stat4:
  100. **
  101. ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
  102. ** to aid the query planner in choosing good indices based on the values
  103. ** that indexed columns are compared against in the WHERE clauses of
  104. ** queries.
  105. **
  106. ** The sqlite_stat4 table contains multiple entries for each index.
  107. ** The idx column names the index and the tbl column is the table of the
  108. ** index. If the idx and tbl columns are the same, then the sample is
  109. ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
  110. ** binary encoding of a key from the index. The nEq column is a
  111. ** list of integers. The first integer is the approximate number
  112. ** of entries in the index whose left-most column exactly matches
  113. ** the left-most column of the sample. The second integer in nEq
  114. ** is the approximate number of entries in the index where the
  115. ** first two columns match the first two columns of the sample.
  116. ** And so forth. nLt is another list of integers that show the approximate
  117. ** number of entries that are strictly less than the sample. The first
  118. ** integer in nLt contains the number of entries in the index where the
  119. ** left-most column is less than the left-most column of the sample.
  120. ** The K-th integer in the nLt entry is the number of index entries
  121. ** where the first K columns are less than the first K columns of the
  122. ** sample. The nDLt column is like nLt except that it contains the
  123. ** number of distinct entries in the index that are less than the
  124. ** sample.
  125. **
  126. ** There can be an arbitrary number of sqlite_stat4 entries per index.
  127. ** The ANALYZE command will typically generate sqlite_stat4 tables
  128. ** that contain between 10 and 40 samples which are distributed across
  129. ** the key space, though not uniformly, and which include samples with
  130. ** large nEq values.
  131. **
  132. ** Format for sqlite_stat3 redux:
  133. **
  134. ** The sqlite_stat3 table is like sqlite_stat4 except that it only
  135. ** looks at the left-most column of the index. The sqlite_stat3.sample
  136. ** column contains the actual value of the left-most column instead
  137. ** of a blob encoding of the complete index key as is found in
  138. ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
  139. ** all contain just a single integer which is the same as the first
  140. ** integer in the equivalent columns in sqlite_stat4.
  141. */
  142. #ifndef SQLITE_OMIT_ANALYZE
  143. #include "sqliteInt.h"
  144. #if defined(SQLITE_ENABLE_STAT4)
  145. # define IsStat4 1
  146. # define IsStat3 0
  147. #elif defined(SQLITE_ENABLE_STAT3)
  148. # define IsStat4 0
  149. # define IsStat3 1
  150. #else
  151. # define IsStat4 0
  152. # define IsStat3 0
  153. # undef SQLITE_STAT4_SAMPLES
  154. # define SQLITE_STAT4_SAMPLES 1
  155. #endif
  156. #define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
  157. /*
  158. ** This routine generates code that opens the sqlite_statN tables.
  159. ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
  160. ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
  161. ** appropriate compile-time options are provided.
  162. **
  163. ** If the sqlite_statN tables do not previously exist, it is created.
  164. **
  165. ** Argument zWhere may be a pointer to a buffer containing a table name,
  166. ** or it may be a NULL pointer. If it is not NULL, then all entries in
  167. ** the sqlite_statN tables associated with the named table are deleted.
  168. ** If zWhere==0, then code is generated to delete all stat table entries.
  169. */
  170. static void openStatTable(
  171. Parse *pParse, /* Parsing context */
  172. int iDb, /* The database we are looking in */
  173. int iStatCur, /* Open the sqlite_stat1 table on this cursor */
  174. const char *zWhere, /* Delete entries for this table or index */
  175. const char *zWhereType /* Either "tbl" or "idx" */
  176. ){
  177. static const struct {
  178. const char *zName;
  179. const char *zCols;
  180. } aTable[] = {
  181. { "sqlite_stat1", "tbl,idx,stat" },
  182. #if defined(SQLITE_ENABLE_STAT4)
  183. { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
  184. { "sqlite_stat3", 0 },
  185. #elif defined(SQLITE_ENABLE_STAT3)
  186. { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
  187. { "sqlite_stat4", 0 },
  188. #else
  189. { "sqlite_stat3", 0 },
  190. { "sqlite_stat4", 0 },
  191. #endif
  192. };
  193. int i;
  194. sqlite3 *db = pParse->db;
  195. Db *pDb;
  196. Vdbe *v = sqlite3GetVdbe(pParse);
  197. int aRoot[ArraySize(aTable)];
  198. u8 aCreateTbl[ArraySize(aTable)];
  199. if( v==0 ) return;
  200. assert( sqlite3BtreeHoldsAllMutexes(db) );
  201. assert( sqlite3VdbeDb(v)==db );
  202. pDb = &db->aDb[iDb];
  203. /* Create new statistic tables if they do not exist, or clear them
  204. ** if they do already exist.
  205. */
  206. for(i=0; i<ArraySize(aTable); i++){
  207. const char *zTab = aTable[i].zName;
  208. Table *pStat;
  209. if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
  210. if( aTable[i].zCols ){
  211. /* The sqlite_statN table does not exist. Create it. Note that a
  212. ** side-effect of the CREATE TABLE statement is to leave the rootpage
  213. ** of the new table in register pParse->regRoot. This is important
  214. ** because the OpenWrite opcode below will be needing it. */
  215. sqlite3NestedParse(pParse,
  216. "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
  217. );
  218. aRoot[i] = pParse->regRoot;
  219. aCreateTbl[i] = OPFLAG_P2ISREG;
  220. }
  221. }else{
  222. /* The table already exists. If zWhere is not NULL, delete all entries
  223. ** associated with the table zWhere. If zWhere is NULL, delete the
  224. ** entire contents of the table. */
  225. aRoot[i] = pStat->tnum;
  226. aCreateTbl[i] = 0;
  227. sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
  228. if( zWhere ){
  229. sqlite3NestedParse(pParse,
  230. "DELETE FROM %Q.%s WHERE %s=%Q",
  231. pDb->zName, zTab, zWhereType, zWhere
  232. );
  233. }else{
  234. /* The sqlite_stat[134] table already exists. Delete all rows. */
  235. sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
  236. }
  237. }
  238. }
  239. /* Open the sqlite_stat[134] tables for writing. */
  240. for(i=0; aTable[i].zCols; i++){
  241. assert( i<ArraySize(aTable) );
  242. sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
  243. sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
  244. sqlite3VdbeChangeP5(v, aCreateTbl[i]);
  245. }
  246. }
  247. /*
  248. ** Recommended number of samples for sqlite_stat4
  249. */
  250. #ifndef SQLITE_STAT4_SAMPLES
  251. # define SQLITE_STAT4_SAMPLES 24
  252. #endif
  253. /*
  254. ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
  255. ** share an instance of the following structure to hold their state
  256. ** information.
  257. */
  258. typedef struct Stat4Accum Stat4Accum;
  259. typedef struct Stat4Sample Stat4Sample;
  260. struct Stat4Sample {
  261. tRowcnt *anEq; /* sqlite_stat4.nEq */
  262. tRowcnt *anDLt; /* sqlite_stat4.nDLt */
  263. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  264. tRowcnt *anLt; /* sqlite_stat4.nLt */
  265. i64 iRowid; /* Rowid in main table of the key */
  266. u8 isPSample; /* True if a periodic sample */
  267. int iCol; /* If !isPSample, the reason for inclusion */
  268. u32 iHash; /* Tiebreaker hash */
  269. #endif
  270. };
  271. struct Stat4Accum {
  272. tRowcnt nRow; /* Number of rows in the entire table */
  273. tRowcnt nPSample; /* How often to do a periodic sample */
  274. int nCol; /* Number of columns in index + rowid */
  275. int mxSample; /* Maximum number of samples to accumulate */
  276. Stat4Sample current; /* Current row as a Stat4Sample */
  277. u32 iPrn; /* Pseudo-random number used for sampling */
  278. Stat4Sample *aBest; /* Array of (nCol-1) best samples */
  279. int iMin; /* Index in a[] of entry with minimum score */
  280. int nSample; /* Current number of samples */
  281. int iGet; /* Index of current sample accessed by stat_get() */
  282. Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
  283. };
  284. /*
  285. ** Implementation of the stat_init(N,C) SQL function. The two parameters
  286. ** are the number of rows in the table or index (C) and the number of columns
  287. ** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
  288. **
  289. ** This routine allocates the Stat4Accum object in heap memory. The return
  290. ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
  291. ** the size of the blob is sizeof(void*) bytes).
  292. */
  293. static void statInit(
  294. sqlite3_context *context,
  295. int argc,
  296. sqlite3_value **argv
  297. ){
  298. Stat4Accum *p;
  299. int nCol; /* Number of columns in index being sampled */
  300. int nColUp; /* nCol rounded up for alignment */
  301. int n; /* Bytes of space to allocate */
  302. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  303. int mxSample = SQLITE_STAT4_SAMPLES;
  304. #endif
  305. /* Decode the three function arguments */
  306. UNUSED_PARAMETER(argc);
  307. nCol = sqlite3_value_int(argv[0]);
  308. assert( nCol>1 ); /* >1 because it includes the rowid column */
  309. nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
  310. /* Allocate the space required for the Stat4Accum object */
  311. n = sizeof(*p)
  312. + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
  313. + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
  314. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  315. + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
  316. + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
  317. + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
  318. #endif
  319. ;
  320. p = sqlite3MallocZero(n);
  321. if( p==0 ){
  322. sqlite3_result_error_nomem(context);
  323. return;
  324. }
  325. p->nRow = 0;
  326. p->nCol = nCol;
  327. p->current.anDLt = (tRowcnt*)&p[1];
  328. p->current.anEq = &p->current.anDLt[nColUp];
  329. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  330. {
  331. u8 *pSpace; /* Allocated space not yet assigned */
  332. int i; /* Used to iterate through p->aSample[] */
  333. p->iGet = -1;
  334. p->mxSample = mxSample;
  335. p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
  336. p->current.anLt = &p->current.anEq[nColUp];
  337. p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
  338. /* Set up the Stat4Accum.a[] and aBest[] arrays */
  339. p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
  340. p->aBest = &p->a[mxSample];
  341. pSpace = (u8*)(&p->a[mxSample+nCol]);
  342. for(i=0; i<(mxSample+nCol); i++){
  343. p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
  344. p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
  345. p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
  346. }
  347. assert( (pSpace - (u8*)p)==n );
  348. for(i=0; i<nCol; i++){
  349. p->aBest[i].iCol = i;
  350. }
  351. }
  352. #endif
  353. /* Return a pointer to the allocated object to the caller */
  354. sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
  355. }
  356. static const FuncDef statInitFuncdef = {
  357. 1+IsStat34, /* nArg */
  358. SQLITE_UTF8, /* funcFlags */
  359. 0, /* pUserData */
  360. 0, /* pNext */
  361. statInit, /* xFunc */
  362. 0, /* xStep */
  363. 0, /* xFinalize */
  364. "stat_init", /* zName */
  365. 0, /* pHash */
  366. 0 /* pDestructor */
  367. };
  368. #ifdef SQLITE_ENABLE_STAT4
  369. /*
  370. ** pNew and pOld are both candidate non-periodic samples selected for
  371. ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
  372. ** considering only any trailing columns and the sample hash value, this
  373. ** function returns true if sample pNew is to be preferred over pOld.
  374. ** In other words, if we assume that the cardinalities of the selected
  375. ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
  376. **
  377. ** This function assumes that for each argument sample, the contents of
  378. ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
  379. */
  380. static int sampleIsBetterPost(
  381. Stat4Accum *pAccum,
  382. Stat4Sample *pNew,
  383. Stat4Sample *pOld
  384. ){
  385. int nCol = pAccum->nCol;
  386. int i;
  387. assert( pNew->iCol==pOld->iCol );
  388. for(i=pNew->iCol+1; i<nCol; i++){
  389. if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
  390. if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
  391. }
  392. if( pNew->iHash>pOld->iHash ) return 1;
  393. return 0;
  394. }
  395. #endif
  396. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  397. /*
  398. ** Return true if pNew is to be preferred over pOld.
  399. **
  400. ** This function assumes that for each argument sample, the contents of
  401. ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
  402. */
  403. static int sampleIsBetter(
  404. Stat4Accum *pAccum,
  405. Stat4Sample *pNew,
  406. Stat4Sample *pOld
  407. ){
  408. tRowcnt nEqNew = pNew->anEq[pNew->iCol];
  409. tRowcnt nEqOld = pOld->anEq[pOld->iCol];
  410. assert( pOld->isPSample==0 && pNew->isPSample==0 );
  411. assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
  412. if( (nEqNew>nEqOld) ) return 1;
  413. #ifdef SQLITE_ENABLE_STAT4
  414. if( nEqNew==nEqOld ){
  415. if( pNew->iCol<pOld->iCol ) return 1;
  416. return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
  417. }
  418. return 0;
  419. #else
  420. return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
  421. #endif
  422. }
  423. /*
  424. ** Copy the contents of object (*pFrom) into (*pTo).
  425. */
  426. static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
  427. pTo->iRowid = pFrom->iRowid;
  428. pTo->isPSample = pFrom->isPSample;
  429. pTo->iCol = pFrom->iCol;
  430. pTo->iHash = pFrom->iHash;
  431. memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
  432. memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
  433. memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
  434. }
  435. /*
  436. ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
  437. ** remove the least desirable sample from p->a[] to make room.
  438. */
  439. static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
  440. Stat4Sample *pSample;
  441. int i;
  442. assert( IsStat4 || nEqZero==0 );
  443. #ifdef SQLITE_ENABLE_STAT4
  444. if( pNew->isPSample==0 ){
  445. Stat4Sample *pUpgrade = 0;
  446. assert( pNew->anEq[pNew->iCol]>0 );
  447. /* This sample is being added because the prefix that ends in column
  448. ** iCol occurs many times in the table. However, if we have already
  449. ** added a sample that shares this prefix, there is no need to add
  450. ** this one. Instead, upgrade the priority of the highest priority
  451. ** existing sample that shares this prefix. */
  452. for(i=p->nSample-1; i>=0; i--){
  453. Stat4Sample *pOld = &p->a[i];
  454. if( pOld->anEq[pNew->iCol]==0 ){
  455. if( pOld->isPSample ) return;
  456. assert( pOld->iCol>pNew->iCol );
  457. assert( sampleIsBetter(p, pNew, pOld) );
  458. if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
  459. pUpgrade = pOld;
  460. }
  461. }
  462. }
  463. if( pUpgrade ){
  464. pUpgrade->iCol = pNew->iCol;
  465. pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
  466. goto find_new_min;
  467. }
  468. }
  469. #endif
  470. /* If necessary, remove sample iMin to make room for the new sample. */
  471. if( p->nSample>=p->mxSample ){
  472. Stat4Sample *pMin = &p->a[p->iMin];
  473. tRowcnt *anEq = pMin->anEq;
  474. tRowcnt *anLt = pMin->anLt;
  475. tRowcnt *anDLt = pMin->anDLt;
  476. memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
  477. pSample = &p->a[p->nSample-1];
  478. pSample->anEq = anEq;
  479. pSample->anDLt = anDLt;
  480. pSample->anLt = anLt;
  481. p->nSample = p->mxSample-1;
  482. }
  483. /* The "rows less-than" for the rowid column must be greater than that
  484. ** for the last sample in the p->a[] array. Otherwise, the samples would
  485. ** be out of order. */
  486. #ifdef SQLITE_ENABLE_STAT4
  487. assert( p->nSample==0
  488. || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
  489. #endif
  490. /* Insert the new sample */
  491. pSample = &p->a[p->nSample];
  492. sampleCopy(p, pSample, pNew);
  493. p->nSample++;
  494. /* Zero the first nEqZero entries in the anEq[] array. */
  495. memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
  496. #ifdef SQLITE_ENABLE_STAT4
  497. find_new_min:
  498. #endif
  499. if( p->nSample>=p->mxSample ){
  500. int iMin = -1;
  501. for(i=0; i<p->mxSample; i++){
  502. if( p->a[i].isPSample ) continue;
  503. if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
  504. iMin = i;
  505. }
  506. }
  507. assert( iMin>=0 );
  508. p->iMin = iMin;
  509. }
  510. }
  511. #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  512. /*
  513. ** Field iChng of the index being scanned has changed. So at this point
  514. ** p->current contains a sample that reflects the previous row of the
  515. ** index. The value of anEq[iChng] and subsequent anEq[] elements are
  516. ** correct at this point.
  517. */
  518. static void samplePushPrevious(Stat4Accum *p, int iChng){
  519. #ifdef SQLITE_ENABLE_STAT4
  520. int i;
  521. /* Check if any samples from the aBest[] array should be pushed
  522. ** into IndexSample.a[] at this point. */
  523. for(i=(p->nCol-2); i>=iChng; i--){
  524. Stat4Sample *pBest = &p->aBest[i];
  525. pBest->anEq[i] = p->current.anEq[i];
  526. if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
  527. sampleInsert(p, pBest, i);
  528. }
  529. }
  530. /* Update the anEq[] fields of any samples already collected. */
  531. for(i=p->nSample-1; i>=0; i--){
  532. int j;
  533. for(j=iChng; j<p->nCol; j++){
  534. if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
  535. }
  536. }
  537. #endif
  538. #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
  539. if( iChng==0 ){
  540. tRowcnt nLt = p->current.anLt[0];
  541. tRowcnt nEq = p->current.anEq[0];
  542. /* Check if this is to be a periodic sample. If so, add it. */
  543. if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
  544. p->current.isPSample = 1;
  545. sampleInsert(p, &p->current, 0);
  546. p->current.isPSample = 0;
  547. }else
  548. /* Or if it is a non-periodic sample. Add it in this case too. */
  549. if( p->nSample<p->mxSample
  550. || sampleIsBetter(p, &p->current, &p->a[p->iMin])
  551. ){
  552. sampleInsert(p, &p->current, 0);
  553. }
  554. }
  555. #endif
  556. #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
  557. UNUSED_PARAMETER( p );
  558. UNUSED_PARAMETER( iChng );
  559. #endif
  560. }
  561. /*
  562. ** Implementation of the stat_push SQL function: stat_push(P,R,C)
  563. ** Arguments:
  564. **
  565. ** P Pointer to the Stat4Accum object created by stat_init()
  566. ** C Index of left-most column to differ from previous row
  567. ** R Rowid for the current row
  568. **
  569. ** The SQL function always returns NULL.
  570. **
  571. ** The R parameter is only used for STAT3 and STAT4.
  572. */
  573. static void statPush(
  574. sqlite3_context *context,
  575. int argc,
  576. sqlite3_value **argv
  577. ){
  578. int i;
  579. /* The three function arguments */
  580. Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
  581. int iChng = sqlite3_value_int(argv[1]);
  582. UNUSED_PARAMETER( argc );
  583. UNUSED_PARAMETER( context );
  584. assert( p->nCol>1 ); /* Includes rowid field */
  585. assert( iChng<p->nCol );
  586. if( p->nRow==0 ){
  587. /* This is the first call to this function. Do initialization. */
  588. for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
  589. }else{
  590. /* Second and subsequent calls get processed here */
  591. samplePushPrevious(p, iChng);
  592. /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
  593. ** to the current row of the index. */
  594. for(i=0; i<iChng; i++){
  595. p->current.anEq[i]++;
  596. }
  597. for(i=iChng; i<p->nCol; i++){
  598. p->current.anDLt[i]++;
  599. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  600. p->current.anLt[i] += p->current.anEq[i];
  601. #endif
  602. p->current.anEq[i] = 1;
  603. }
  604. }
  605. p->nRow++;
  606. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  607. p->current.iRowid = sqlite3_value_int64(argv[2]);
  608. p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
  609. #endif
  610. #ifdef SQLITE_ENABLE_STAT4
  611. {
  612. tRowcnt nLt = p->current.anLt[p->nCol-1];
  613. /* Check if this is to be a periodic sample. If so, add it. */
  614. if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
  615. p->current.isPSample = 1;
  616. p->current.iCol = 0;
  617. sampleInsert(p, &p->current, p->nCol-1);
  618. p->current.isPSample = 0;
  619. }
  620. /* Update the aBest[] array. */
  621. for(i=0; i<(p->nCol-1); i++){
  622. p->current.iCol = i;
  623. if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
  624. sampleCopy(p, &p->aBest[i], &p->current);
  625. }
  626. }
  627. }
  628. #endif
  629. }
  630. static const FuncDef statPushFuncdef = {
  631. 2+IsStat34, /* nArg */
  632. SQLITE_UTF8, /* funcFlags */
  633. 0, /* pUserData */
  634. 0, /* pNext */
  635. statPush, /* xFunc */
  636. 0, /* xStep */
  637. 0, /* xFinalize */
  638. "stat_push", /* zName */
  639. 0, /* pHash */
  640. 0 /* pDestructor */
  641. };
  642. #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
  643. #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
  644. #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
  645. #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
  646. #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
  647. /*
  648. ** Implementation of the stat_get(P,J) SQL function. This routine is
  649. ** used to query the results. Content is returned for parameter J
  650. ** which is one of the STAT_GET_xxxx values defined above.
  651. **
  652. ** If neither STAT3 nor STAT4 are enabled, then J is always
  653. ** STAT_GET_STAT1 and is hence omitted and this routine becomes
  654. ** a one-parameter function, stat_get(P), that always returns the
  655. ** stat1 table entry information.
  656. */
  657. static void statGet(
  658. sqlite3_context *context,
  659. int argc,
  660. sqlite3_value **argv
  661. ){
  662. Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
  663. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  664. /* STAT3 and STAT4 have a parameter on this routine. */
  665. int eCall = sqlite3_value_int(argv[1]);
  666. assert( argc==2 );
  667. assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
  668. || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
  669. || eCall==STAT_GET_NDLT
  670. );
  671. if( eCall==STAT_GET_STAT1 )
  672. #else
  673. assert( argc==1 );
  674. #endif
  675. {
  676. /* Return the value to store in the "stat" column of the sqlite_stat1
  677. ** table for this index.
  678. **
  679. ** The value is a string composed of a list of integers describing
  680. ** the index. The first integer in the list is the total number of
  681. ** entries in the index. There is one additional integer in the list
  682. ** for each indexed column. This additional integer is an estimate of
  683. ** the number of rows matched by a stabbing query on the index using
  684. ** a key with the corresponding number of fields. In other words,
  685. ** if the index is on columns (a,b) and the sqlite_stat1 value is
  686. ** "100 10 2", then SQLite estimates that:
  687. **
  688. ** * the index contains 100 rows,
  689. ** * "WHERE a=?" matches 10 rows, and
  690. ** * "WHERE a=? AND b=?" matches 2 rows.
  691. **
  692. ** If D is the count of distinct values and K is the total number of
  693. ** rows, then each estimate is computed as:
  694. **
  695. ** I = (K+D-1)/D
  696. */
  697. char *z;
  698. int i;
  699. char *zRet = sqlite3MallocZero(p->nCol * 25);
  700. if( zRet==0 ){
  701. sqlite3_result_error_nomem(context);
  702. return;
  703. }
  704. sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
  705. z = zRet + sqlite3Strlen30(zRet);
  706. for(i=0; i<(p->nCol-1); i++){
  707. u64 nDistinct = p->current.anDLt[i] + 1;
  708. u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
  709. sqlite3_snprintf(24, z, " %llu", iVal);
  710. z += sqlite3Strlen30(z);
  711. assert( p->current.anEq[i] );
  712. }
  713. assert( z[0]=='\0' && z>zRet );
  714. sqlite3_result_text(context, zRet, -1, sqlite3_free);
  715. }
  716. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  717. else if( eCall==STAT_GET_ROWID ){
  718. if( p->iGet<0 ){
  719. samplePushPrevious(p, 0);
  720. p->iGet = 0;
  721. }
  722. if( p->iGet<p->nSample ){
  723. sqlite3_result_int64(context, p->a[p->iGet].iRowid);
  724. }
  725. }else{
  726. tRowcnt *aCnt = 0;
  727. assert( p->iGet<p->nSample );
  728. switch( eCall ){
  729. case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
  730. case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
  731. default: {
  732. aCnt = p->a[p->iGet].anDLt;
  733. p->iGet++;
  734. break;
  735. }
  736. }
  737. if( IsStat3 ){
  738. sqlite3_result_int64(context, (i64)aCnt[0]);
  739. }else{
  740. char *zRet = sqlite3MallocZero(p->nCol * 25);
  741. if( zRet==0 ){
  742. sqlite3_result_error_nomem(context);
  743. }else{
  744. int i;
  745. char *z = zRet;
  746. for(i=0; i<p->nCol; i++){
  747. sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
  748. z += sqlite3Strlen30(z);
  749. }
  750. assert( z[0]=='\0' && z>zRet );
  751. z[-1] = '\0';
  752. sqlite3_result_text(context, zRet, -1, sqlite3_free);
  753. }
  754. }
  755. }
  756. #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  757. #ifndef SQLITE_DEBUG
  758. UNUSED_PARAMETER( argc );
  759. #endif
  760. }
  761. static const FuncDef statGetFuncdef = {
  762. 1+IsStat34, /* nArg */
  763. SQLITE_UTF8, /* funcFlags */
  764. 0, /* pUserData */
  765. 0, /* pNext */
  766. statGet, /* xFunc */
  767. 0, /* xStep */
  768. 0, /* xFinalize */
  769. "stat_get", /* zName */
  770. 0, /* pHash */
  771. 0 /* pDestructor */
  772. };
  773. static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
  774. assert( regOut!=regStat4 && regOut!=regStat4+1 );
  775. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  776. sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
  777. #elif SQLITE_DEBUG
  778. assert( iParam==STAT_GET_STAT1 );
  779. #else
  780. UNUSED_PARAMETER( iParam );
  781. #endif
  782. sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
  783. sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
  784. sqlite3VdbeChangeP5(v, 1 + IsStat34);
  785. }
  786. /*
  787. ** Generate code to do an analysis of all indices associated with
  788. ** a single table.
  789. */
  790. static void analyzeOneTable(
  791. Parse *pParse, /* Parser context */
  792. Table *pTab, /* Table whose indices are to be analyzed */
  793. Index *pOnlyIdx, /* If not NULL, only analyze this one index */
  794. int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
  795. int iMem, /* Available memory locations begin here */
  796. int iTab /* Next available cursor */
  797. ){
  798. sqlite3 *db = pParse->db; /* Database handle */
  799. Index *pIdx; /* An index to being analyzed */
  800. int iIdxCur; /* Cursor open on index being analyzed */
  801. int iTabCur; /* Table cursor */
  802. Vdbe *v; /* The virtual machine being built up */
  803. int i; /* Loop counter */
  804. int jZeroRows = -1; /* Jump from here if number of rows is zero */
  805. int iDb; /* Index of database containing pTab */
  806. u8 needTableCnt = 1; /* True to count the table */
  807. int regNewRowid = iMem++; /* Rowid for the inserted record */
  808. int regStat4 = iMem++; /* Register to hold Stat4Accum object */
  809. int regChng = iMem++; /* Index of changed index field */
  810. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  811. int regRowid = iMem++; /* Rowid argument passed to stat_push() */
  812. #endif
  813. int regTemp = iMem++; /* Temporary use register */
  814. int regTabname = iMem++; /* Register containing table name */
  815. int regIdxname = iMem++; /* Register containing index name */
  816. int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
  817. int regPrev = iMem; /* MUST BE LAST (see below) */
  818. pParse->nMem = MAX(pParse->nMem, iMem);
  819. v = sqlite3GetVdbe(pParse);
  820. if( v==0 || NEVER(pTab==0) ){
  821. return;
  822. }
  823. if( pTab->tnum==0 ){
  824. /* Do not gather statistics on views or virtual tables */
  825. return;
  826. }
  827. if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
  828. /* Do not gather statistics on system tables */
  829. return;
  830. }
  831. assert( sqlite3BtreeHoldsAllMutexes(db) );
  832. iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  833. assert( iDb>=0 );
  834. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  835. #ifndef SQLITE_OMIT_AUTHORIZATION
  836. if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
  837. db->aDb[iDb].zName ) ){
  838. return;
  839. }
  840. #endif
  841. /* Establish a read-lock on the table at the shared-cache level.
  842. ** Open a read-only cursor on the table. Also allocate a cursor number
  843. ** to use for scanning indexes (iIdxCur). No index cursor is opened at
  844. ** this time though. */
  845. sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  846. iTabCur = iTab++;
  847. iIdxCur = iTab++;
  848. pParse->nTab = MAX(pParse->nTab, iTab);
  849. sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
  850. sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
  851. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  852. int nCol; /* Number of columns indexed by pIdx */
  853. KeyInfo *pKey; /* KeyInfo structure for pIdx */
  854. int *aGotoChng; /* Array of jump instruction addresses */
  855. int addrRewind; /* Address of "OP_Rewind iIdxCur" */
  856. int addrGotoChng0; /* Address of "Goto addr_chng_0" */
  857. int addrNextRow; /* Address of "next_row:" */
  858. if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
  859. if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
  860. VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
  861. nCol = pIdx->nColumn;
  862. aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
  863. if( aGotoChng==0 ) continue;
  864. pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  865. /* Populate the register containing the index name. */
  866. sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
  867. /*
  868. ** Pseudo-code for loop that calls stat_push():
  869. **
  870. ** Rewind csr
  871. ** if eof(csr) goto end_of_scan;
  872. ** regChng = 0
  873. ** goto chng_addr_0;
  874. **
  875. ** next_row:
  876. ** regChng = 0
  877. ** if( idx(0) != regPrev(0) ) goto chng_addr_0
  878. ** regChng = 1
  879. ** if( idx(1) != regPrev(1) ) goto chng_addr_1
  880. ** ...
  881. ** regChng = N
  882. ** goto chng_addr_N
  883. **
  884. ** chng_addr_0:
  885. ** regPrev(0) = idx(0)
  886. ** chng_addr_1:
  887. ** regPrev(1) = idx(1)
  888. ** ...
  889. **
  890. ** chng_addr_N:
  891. ** regRowid = idx(rowid)
  892. ** stat_push(P, regChng, regRowid)
  893. ** Next csr
  894. ** if !eof(csr) goto next_row;
  895. **
  896. ** end_of_scan:
  897. */
  898. /* Make sure there are enough memory cells allocated to accommodate
  899. ** the regPrev array and a trailing rowid (the rowid slot is required
  900. ** when building a record to insert into the sample column of
  901. ** the sqlite_stat4 table. */
  902. pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
  903. /* Open a read-only cursor on the index being analyzed. */
  904. assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
  905. sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
  906. sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
  907. VdbeComment((v, "%s", pIdx->zName));
  908. /* Invoke the stat_init() function. The arguments are:
  909. **
  910. ** (1) the number of columns in the index including the rowid,
  911. ** (2) the number of rows in the index,
  912. **
  913. ** The second argument is only used for STAT3 and STAT4
  914. */
  915. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  916. sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
  917. #endif
  918. sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
  919. sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
  920. sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
  921. sqlite3VdbeChangeP5(v, 1+IsStat34);
  922. /* Implementation of the following:
  923. **
  924. ** Rewind csr
  925. ** if eof(csr) goto end_of_scan;
  926. ** regChng = 0
  927. ** goto next_push_0;
  928. **
  929. */
  930. addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
  931. sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
  932. addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
  933. /*
  934. ** next_row:
  935. ** regChng = 0
  936. ** if( idx(0) != regPrev(0) ) goto chng_addr_0
  937. ** regChng = 1
  938. ** if( idx(1) != regPrev(1) ) goto chng_addr_1
  939. ** ...
  940. ** regChng = N
  941. ** goto chng_addr_N
  942. */
  943. addrNextRow = sqlite3VdbeCurrentAddr(v);
  944. for(i=0; i<nCol; i++){
  945. char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
  946. sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
  947. sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
  948. aGotoChng[i] =
  949. sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
  950. sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
  951. }
  952. sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
  953. aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
  954. /*
  955. ** chng_addr_0:
  956. ** regPrev(0) = idx(0)
  957. ** chng_addr_1:
  958. ** regPrev(1) = idx(1)
  959. ** ...
  960. */
  961. sqlite3VdbeJumpHere(v, addrGotoChng0);
  962. for(i=0; i<nCol; i++){
  963. sqlite3VdbeJumpHere(v, aGotoChng[i]);
  964. sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
  965. }
  966. /*
  967. ** chng_addr_N:
  968. ** regRowid = idx(rowid) // STAT34 only
  969. ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
  970. ** Next csr
  971. ** if !eof(csr) goto next_row;
  972. */
  973. sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
  974. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  975. sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  976. assert( regRowid==(regStat4+2) );
  977. #endif
  978. assert( regChng==(regStat4+1) );
  979. sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
  980. sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  981. sqlite3VdbeChangeP5(v, 2+IsStat34);
  982. sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
  983. /* Add the entry to the stat1 table. */
  984. callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  985. sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
  986. sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  987. sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  988. sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  989. /* Add the entries to the stat3 or stat4 table. */
  990. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  991. {
  992. int regEq = regStat1;
  993. int regLt = regStat1+1;
  994. int regDLt = regStat1+2;
  995. int regSample = regStat1+3;
  996. int regCol = regStat1+4;
  997. int regSampleRowid = regCol + nCol;
  998. int addrNext;
  999. int addrIsNull;
  1000. pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
  1001. addrNext = sqlite3VdbeCurrentAddr(v);
  1002. callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
  1003. addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
  1004. callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1005. callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1006. callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1007. sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
  1008. #ifdef SQLITE_ENABLE_STAT3
  1009. sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
  1010. pIdx->aiColumn[0], regSample);
  1011. #else
  1012. for(i=0; i<nCol; i++){
  1013. int iCol = pIdx->aiColumn[i];
  1014. sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
  1015. }
  1016. sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
  1017. #endif
  1018. sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
  1019. sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1020. sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
  1021. sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
  1022. sqlite3VdbeJumpHere(v, addrIsNull);
  1023. }
  1024. #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1025. /* End of analysis */
  1026. sqlite3VdbeJumpHere(v, addrRewind);
  1027. sqlite3DbFree(db, aGotoChng);
  1028. }
  1029. /* Create a single sqlite_stat1 entry containing NULL as the index
  1030. ** name and the row count as the content.
  1031. */
  1032. if( pOnlyIdx==0 && needTableCnt ){
  1033. VdbeComment((v, "%s", pTab->zName));
  1034. sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
  1035. jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
  1036. sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
  1037. sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
  1038. sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1039. sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1040. sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1041. sqlite3VdbeJumpHere(v, jZeroRows);
  1042. }
  1043. }
  1044. /*
  1045. ** Generate code that will cause the most recent index analysis to
  1046. ** be loaded into internal hash tables where is can be used.
  1047. */
  1048. static void loadAnalysis(Parse *pParse, int iDb){
  1049. Vdbe *v = sqlite3GetVdbe(pParse);
  1050. if( v ){
  1051. sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
  1052. }
  1053. }
  1054. /*
  1055. ** Generate code that will do an analysis of an entire database
  1056. */
  1057. static void analyzeDatabase(Parse *pParse, int iDb){
  1058. sqlite3 *db = pParse->db;
  1059. Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
  1060. HashElem *k;
  1061. int iStatCur;
  1062. int iMem;
  1063. int iTab;
  1064. sqlite3BeginWriteOperation(pParse, 0, iDb);
  1065. iStatCur = pParse->nTab;
  1066. pParse->nTab += 3;
  1067. openStatTable(pParse, iDb, iStatCur, 0, 0);
  1068. iMem = pParse->nMem+1;
  1069. iTab = pParse->nTab;
  1070. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1071. for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
  1072. Table *pTab = (Table*)sqliteHashData(k);
  1073. analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
  1074. }
  1075. loadAnalysis(pParse, iDb);
  1076. }
  1077. /*
  1078. ** Generate code that will do an analysis of a single table in
  1079. ** a database. If pOnlyIdx is not NULL then it is a single index
  1080. ** in pTab that should be analyzed.
  1081. */
  1082. static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
  1083. int iDb;
  1084. int iStatCur;
  1085. assert( pTab!=0 );
  1086. assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  1087. iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1088. sqlite3BeginWriteOperation(pParse, 0, iDb);
  1089. iStatCur = pParse->nTab;
  1090. pParse->nTab += 3;
  1091. if( pOnlyIdx ){
  1092. openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
  1093. }else{
  1094. openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
  1095. }
  1096. analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
  1097. loadAnalysis(pParse, iDb);
  1098. }
  1099. /*
  1100. ** Generate code for the ANALYZE command. The parser calls this routine
  1101. ** when it recognizes an ANALYZE command.
  1102. **
  1103. ** ANALYZE -- 1
  1104. ** ANALYZE <database> -- 2
  1105. ** ANALYZE ?<database>.?<tablename> -- 3
  1106. **
  1107. ** Form 1 causes all indices in all attached databases to be analyzed.
  1108. ** Form 2 analyzes all indices the single database named.
  1109. ** Form 3 analyzes all indices associated with the named table.
  1110. */
  1111. void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
  1112. sqlite3 *db = pParse->db;
  1113. int iDb;
  1114. int i;
  1115. char *z, *zDb;
  1116. Table *pTab;
  1117. Index *pIdx;
  1118. Token *pTableName;
  1119. /* Read the database schema. If an error occurs, leave an error message
  1120. ** and code in pParse and return NULL. */
  1121. assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  1122. if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  1123. return;
  1124. }
  1125. assert( pName2!=0 || pName1==0 );
  1126. if( pName1==0 ){
  1127. /* Form 1: Analyze everything */
  1128. for(i=0; i<db->nDb; i++){
  1129. if( i==1 ) continue; /* Do not analyze the TEMP database */
  1130. analyzeDatabase(pParse, i);
  1131. }
  1132. }else if( pName2->n==0 ){
  1133. /* Form 2: Analyze the database or table named */
  1134. iDb = sqlite3FindDb(db, pName1);
  1135. if( iDb>=0 ){
  1136. analyzeDatabase(pParse, iDb);
  1137. }else{
  1138. z = sqlite3NameFromToken(db, pName1);
  1139. if( z ){
  1140. if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
  1141. analyzeTable(pParse, pIdx->pTable, pIdx);
  1142. }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
  1143. analyzeTable(pParse, pTab, 0);
  1144. }
  1145. sqlite3DbFree(db, z);
  1146. }
  1147. }
  1148. }else{
  1149. /* Form 3: Analyze the fully qualified table name */
  1150. iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
  1151. if( iDb>=0 ){
  1152. zDb = db->aDb[iDb].zName;
  1153. z = sqlite3NameFromToken(db, pTableName);
  1154. if( z ){
  1155. if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
  1156. analyzeTable(pParse, pIdx->pTable, pIdx);
  1157. }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
  1158. analyzeTable(pParse, pTab, 0);
  1159. }
  1160. sqlite3DbFree(db, z);
  1161. }
  1162. }
  1163. }
  1164. }
  1165. /*
  1166. ** Used to pass information from the analyzer reader through to the
  1167. ** callback routine.
  1168. */
  1169. typedef struct analysisInfo analysisInfo;
  1170. struct analysisInfo {
  1171. sqlite3 *db;
  1172. const char *zDatabase;
  1173. };
  1174. /*
  1175. ** The first argument points to a nul-terminated string containing a
  1176. ** list of space separated integers. Read the first nOut of these into
  1177. ** the array aOut[].
  1178. */
  1179. static void decodeIntArray(
  1180. char *zIntArray, /* String containing int array to decode */
  1181. int nOut, /* Number of slots in aOut[] */
  1182. tRowcnt *aOut, /* Store integers here */
  1183. Index *pIndex /* Handle extra flags for this index, if not NULL */
  1184. ){
  1185. char *z = zIntArray;
  1186. int c;
  1187. int i;
  1188. tRowcnt v;
  1189. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1190. if( z==0 ) z = "";
  1191. #else
  1192. if( NEVER(z==0) ) z = "";
  1193. #endif
  1194. for(i=0; *z && i<nOut; i++){
  1195. v = 0;
  1196. while( (c=z[0])>='0' && c<='9' ){
  1197. v = v*10 + c - '0';
  1198. z++;
  1199. }
  1200. aOut[i] = v;
  1201. if( *z==' ' ) z++;
  1202. }
  1203. #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
  1204. assert( pIndex!=0 );
  1205. #else
  1206. if( pIndex )
  1207. #endif
  1208. {
  1209. if( strcmp(z, "unordered")==0 ){
  1210. pIndex->bUnordered = 1;
  1211. }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
  1212. int v32 = 0;
  1213. sqlite3GetInt32(z+3, &v32);
  1214. pIndex->szIdxRow = sqlite3LogEst(v32);
  1215. }
  1216. }
  1217. }
  1218. /*
  1219. ** This callback is invoked once for each index when reading the
  1220. ** sqlite_stat1 table.
  1221. **
  1222. ** argv[0] = name of the table
  1223. ** argv[1] = name of the index (might be NULL)
  1224. ** argv[2] = results of analysis - on integer for each column
  1225. **
  1226. ** Entries for which argv[1]==NULL simply record the number of rows in
  1227. ** the table.
  1228. */
  1229. static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
  1230. analysisInfo *pInfo = (analysisInfo*)pData;
  1231. Index *pIndex;
  1232. Table *pTable;
  1233. const char *z;
  1234. assert( argc==3 );
  1235. UNUSED_PARAMETER2(NotUsed, argc);
  1236. if( argv==0 || argv[0]==0 || argv[2]==0 ){
  1237. return 0;
  1238. }
  1239. pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
  1240. if( pTable==0 ){
  1241. return 0;
  1242. }
  1243. if( argv[1] ){
  1244. pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1245. }else{
  1246. pIndex = 0;
  1247. }
  1248. z = argv[2];
  1249. if( pIndex ){
  1250. decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst, pIndex);
  1251. if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
  1252. }else{
  1253. Index fakeIdx;
  1254. fakeIdx.szIdxRow = pTable->szTabRow;
  1255. decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
  1256. pTable->szTabRow = fakeIdx.szIdxRow;
  1257. }
  1258. return 0;
  1259. }
  1260. /*
  1261. ** If the Index.aSample variable is not NULL, delete the aSample[] array
  1262. ** and its contents.
  1263. */
  1264. void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
  1265. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1266. if( pIdx->aSample ){
  1267. int j;
  1268. for(j=0; j<pIdx->nSample; j++){
  1269. IndexSample *p = &pIdx->aSample[j];
  1270. sqlite3DbFree(db, p->p);
  1271. }
  1272. sqlite3DbFree(db, pIdx->aSample);
  1273. }
  1274. if( db && db->pnBytesFreed==0 ){
  1275. pIdx->nSample = 0;
  1276. pIdx->aSample = 0;
  1277. }
  1278. #else
  1279. UNUSED_PARAMETER(db);
  1280. UNUSED_PARAMETER(pIdx);
  1281. #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1282. }
  1283. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1284. /*
  1285. ** Populate the pIdx->aAvgEq[] array based on the samples currently
  1286. ** stored in pIdx->aSample[].
  1287. */
  1288. static void initAvgEq(Index *pIdx){
  1289. if( pIdx ){
  1290. IndexSample *aSample = pIdx->aSample;
  1291. IndexSample *pFinal = &aSample[pIdx->nSample-1];
  1292. int iCol;
  1293. for(iCol=0; iCol<pIdx->nColumn; iCol++){
  1294. int i; /* Used to iterate through samples */
  1295. tRowcnt sumEq = 0; /* Sum of the nEq values */
  1296. tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
  1297. tRowcnt avgEq = 0;
  1298. tRowcnt nDLt = pFinal->anDLt[iCol];
  1299. /* Set nSum to the number of distinct (iCol+1) field prefixes that
  1300. ** occur in the stat4 table for this index before pFinal. Set
  1301. ** sumEq to the sum of the nEq values for column iCol for the same
  1302. ** set (adding the value only once where there exist dupicate
  1303. ** prefixes). */
  1304. for(i=0; i<(pIdx->nSample-1); i++){
  1305. if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
  1306. sumEq += aSample[i].anEq[iCol];
  1307. nSum++;
  1308. }
  1309. }
  1310. if( nDLt>nSum ){
  1311. avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
  1312. }
  1313. if( avgEq==0 ) avgEq = 1;
  1314. pIdx->aAvgEq[iCol] = avgEq;
  1315. if( pIdx->nSampleCol==1 ) break;
  1316. }
  1317. }
  1318. }
  1319. /*
  1320. ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
  1321. ** into the relevant Index.aSample[] arrays.
  1322. **
  1323. ** Arguments zSql1 and zSql2 must point to SQL statements that return
  1324. ** data equivalent to the following (statements are different for stat3,
  1325. ** see the caller of this function for details):
  1326. **
  1327. ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
  1328. ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
  1329. **
  1330. ** where %Q is replaced with the database name before the SQL is executed.
  1331. */
  1332. static int loadStatTbl(
  1333. sqlite3 *db, /* Database handle */
  1334. int bStat3, /* Assume single column records only */
  1335. const char *zSql1, /* SQL statement 1 (see above) */
  1336. const char *zSql2, /* SQL statement 2 (see above) */
  1337. const char *zDb /* Database name (e.g. "main") */
  1338. ){
  1339. int rc; /* Result codes from subroutines */
  1340. sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
  1341. char *zSql; /* Text of the SQL statement */
  1342. Index *pPrevIdx = 0; /* Previous index in the loop */
  1343. IndexSample *pSample; /* A slot in pIdx->aSample[] */
  1344. assert( db->lookaside.bEnabled==0 );
  1345. zSql = sqlite3MPrintf(db, zSql1, zDb);
  1346. if( !zSql ){
  1347. return SQLITE_NOMEM;
  1348. }
  1349. rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  1350. sqlite3DbFree(db, zSql);
  1351. if( rc ) return rc;
  1352. while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1353. int nIdxCol = 1; /* Number of columns in stat4 records */
  1354. int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
  1355. char *zIndex; /* Index name */
  1356. Index *pIdx; /* Pointer to the index object */
  1357. int nSample; /* Number of samples */
  1358. int nByte; /* Bytes of space required */
  1359. int i; /* Bytes of space required */
  1360. tRowcnt *pSpace;
  1361. zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1362. if( zIndex==0 ) continue;
  1363. nSample = sqlite3_column_int(pStmt, 1);
  1364. pIdx = sqlite3FindIndex(db, zIndex, zDb);
  1365. assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
  1366. /* Index.nSample is non-zero at this point if data has already been
  1367. ** loaded from the stat4 table. In this case ignore stat3 data. */
  1368. if( pIdx==0 || pIdx->nSample ) continue;
  1369. if( bStat3==0 ){
  1370. nIdxCol = pIdx->nColumn+1;
  1371. nAvgCol = pIdx->nColumn;
  1372. }
  1373. pIdx->nSampleCol = nIdxCol;
  1374. nByte = sizeof(IndexSample) * nSample;
  1375. nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
  1376. nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
  1377. pIdx->aSample = sqlite3DbMallocZero(db, nByte);
  1378. if( pIdx->aSample==0 ){
  1379. sqlite3_finalize(pStmt);
  1380. return SQLITE_NOMEM;
  1381. }
  1382. pSpace = (tRowcnt*)&pIdx->aSample[nSample];
  1383. pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
  1384. for(i=0; i<nSample; i++){
  1385. pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
  1386. pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
  1387. pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
  1388. }
  1389. assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
  1390. }
  1391. rc = sqlite3_finalize(pStmt);
  1392. if( rc ) return rc;
  1393. zSql = sqlite3MPrintf(db, zSql2, zDb);
  1394. if( !zSql ){
  1395. return SQLITE_NOMEM;
  1396. }
  1397. rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  1398. sqlite3DbFree(db, zSql);
  1399. if( rc ) return rc;
  1400. while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1401. char *zIndex; /* Index name */
  1402. Index *pIdx; /* Pointer to the index object */
  1403. int nCol = 1; /* Number of columns in index */
  1404. zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1405. if( zIndex==0 ) continue;
  1406. pIdx = sqlite3FindIndex(db, zIndex, zDb);
  1407. if( pIdx==0 ) continue;
  1408. /* This next condition is true if data has already been loaded from
  1409. ** the sqlite_stat4 table. In this case ignore stat3 data. */
  1410. nCol = pIdx->nSampleCol;
  1411. if( bStat3 && nCol>1 ) continue;
  1412. if( pIdx!=pPrevIdx ){
  1413. initAvgEq(pPrevIdx);
  1414. pPrevIdx = pIdx;
  1415. }
  1416. pSample = &pIdx->aSample[pIdx->nSample];
  1417. decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
  1418. decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
  1419. decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
  1420. /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
  1421. ** This is in case the sample record is corrupted. In that case, the
  1422. ** sqlite3VdbeRecordCompare() may read up to two varints past the
  1423. ** end of the allocated buffer before it realizes it is dealing with
  1424. ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
  1425. ** a buffer overread. */
  1426. pSample->n = sqlite3_column_bytes(pStmt, 4);
  1427. pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
  1428. if( pSample->p==0 ){
  1429. sqlite3_finalize(pStmt);
  1430. return SQLITE_NOMEM;
  1431. }
  1432. memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
  1433. pIdx->nSample++;
  1434. }
  1435. rc = sqlite3_finalize(pStmt);
  1436. if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
  1437. return rc;
  1438. }
  1439. /*
  1440. ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
  1441. ** the Index.aSample[] arrays of all indices.
  1442. */
  1443. static int loadStat4(sqlite3 *db, const char *zDb){
  1444. int rc = SQLITE_OK; /* Result codes from subroutines */
  1445. assert( db->lookaside.bEnabled==0 );
  1446. if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
  1447. rc = loadStatTbl(db, 0,
  1448. "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
  1449. "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
  1450. zDb
  1451. );
  1452. }
  1453. if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
  1454. rc = loadStatTbl(db, 1,
  1455. "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
  1456. "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
  1457. zDb
  1458. );
  1459. }
  1460. return rc;
  1461. }
  1462. #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1463. /*
  1464. ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
  1465. ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
  1466. ** arrays. The contents of sqlite_stat3/4 are used to populate the
  1467. ** Index.aSample[] arrays.
  1468. **
  1469. ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
  1470. ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
  1471. ** during compilation and the sqlite_stat3/4 table is present, no data is
  1472. ** read from it.
  1473. **
  1474. ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
  1475. ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
  1476. ** returned. However, in this case, data is read from the sqlite_stat1
  1477. ** table (if it is present) before returning.
  1478. **
  1479. ** If an OOM error occurs, this function always sets db->mallocFailed.
  1480. ** This means if the caller does not care about other errors, the return
  1481. ** code may be ignored.
  1482. */
  1483. int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
  1484. analysisInfo sInfo;
  1485. HashElem *i;
  1486. char *zSql;
  1487. int rc;
  1488. assert( iDb>=0 && iDb<db->nDb );
  1489. assert( db->aDb[iDb].pBt!=0 );
  1490. /* Clear any prior statistics */
  1491. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1492. for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
  1493. Index *pIdx = sqliteHashData(i);
  1494. sqlite3DefaultRowEst(pIdx);
  1495. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1496. sqlite3DeleteIndexSamples(db, pIdx);
  1497. pIdx->aSample = 0;
  1498. #endif
  1499. }
  1500. /* Check to make sure the sqlite_stat1 table exists */
  1501. sInfo.db = db;
  1502. sInfo.zDatabase = db->aDb[iDb].zName;
  1503. if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
  1504. return SQLITE_ERROR;
  1505. }
  1506. /* Load new statistics out of the sqlite_stat1 table */
  1507. zSql = sqlite3MPrintf(db,
  1508. "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
  1509. if( zSql==0 ){
  1510. rc = SQLITE_NOMEM;
  1511. }else{
  1512. rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
  1513. sqlite3DbFree(db, zSql);
  1514. }
  1515. /* Load the statistics from the sqlite_stat4 table. */
  1516. #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1517. if( rc==SQLITE_OK ){
  1518. int lookasideEnabled = db->lookaside.bEnabled;
  1519. db->lookaside.bEnabled = 0;
  1520. rc = loadStat4(db, sInfo.zDatabase);
  1521. db->lookaside.bEnabled = lookasideEnabled;
  1522. }
  1523. #endif
  1524. if( rc==SQLITE_NOMEM ){
  1525. db->mallocFailed = 1;
  1526. }
  1527. return rc;
  1528. }
  1529. #endif /* SQLITE_OMIT_ANALYZE */