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