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 | 0ae4f14 | 2013-10-14 14:21:59 +0000 | [diff] [blame] | 34 | ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 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 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 38 | ** For most applications, sqlite_stat1 provides all the statistics required |
drh | c8af850 | 2013-08-09 14:07:55 +0000 | [diff] [blame] | 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 |
drh | 0ae4f14 | 2013-10-14 14:21:59 +0000 | [diff] [blame] | 110 | ** binary encoding of a key from the index. The nEq column is a |
| 111 | ** list of integers. The first integer is the approximate number |
| 112 | ** of entries in the index whose left-most column exactly matches |
| 113 | ** the left-most column of the sample. The second integer in nEq |
| 114 | ** is the approximate number of entries in the index where the |
| 115 | ** first two columns match the first two columns of the sample. |
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) ); |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 247 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 248 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 249 | VdbeComment((v, aTable[i].zName)); |
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 */ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 272 | union { |
| 273 | i64 iRowid; /* Rowid in main table of the key */ |
| 274 | u8 *aRowid; /* Key for WITHOUT ROWID tables */ |
| 275 | } u; |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 276 | u32 nRowid; /* Sizeof aRowid[] */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 277 | u8 isPSample; /* True if a periodic sample */ |
| 278 | int iCol; /* If !isPSample, the reason for inclusion */ |
| 279 | u32 iHash; /* Tiebreaker hash */ |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 280 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 281 | }; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 282 | struct Stat4Accum { |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 283 | tRowcnt nRow; /* Number of rows in the entire table */ |
| 284 | tRowcnt nPSample; /* How often to do a periodic sample */ |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 285 | int nCol; /* Number of columns in index + pk/rowid */ |
| 286 | int nKeyCol; /* Number of index columns w/o the pk/rowid */ |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 287 | int mxSample; /* Maximum number of samples to accumulate */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 288 | Stat4Sample current; /* Current row as a Stat4Sample */ |
drh | f404c86 | 2011-08-13 15:25:10 +0000 | [diff] [blame] | 289 | u32 iPrn; /* Pseudo-random number used for sampling */ |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 290 | Stat4Sample *aBest; /* Array of nCol best samples */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 291 | int iMin; /* Index in a[] of entry with minimum score */ |
| 292 | int nSample; /* Current number of samples */ |
| 293 | int iGet; /* Index of current sample accessed by stat_get() */ |
| 294 | Stat4Sample *a; /* Array of mxSample Stat4Sample objects */ |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 295 | sqlite3 *db; /* Database connection, for malloc() */ |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 298 | /* Reclaim memory used by a Stat4Sample |
| 299 | */ |
| 300 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 301 | static void sampleClear(sqlite3 *db, Stat4Sample *p){ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 302 | assert( db!=0 ); |
| 303 | if( p->nRowid ){ |
| 304 | sqlite3DbFree(db, p->u.aRowid); |
| 305 | p->nRowid = 0; |
| 306 | } |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 307 | } |
| 308 | #endif |
| 309 | |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 310 | /* Initialize the BLOB value of a ROWID |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 311 | */ |
| 312 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 313 | static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){ |
| 314 | assert( db!=0 ); |
| 315 | if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 316 | p->u.aRowid = sqlite3DbMallocRawNN(db, n); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 317 | if( p->u.aRowid ){ |
| 318 | p->nRowid = n; |
| 319 | memcpy(p->u.aRowid, pData, n); |
| 320 | }else{ |
| 321 | p->nRowid = 0; |
| 322 | } |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | /* Initialize the INTEGER value of a ROWID. |
| 327 | */ |
| 328 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 329 | static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){ |
| 330 | assert( db!=0 ); |
| 331 | if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); |
| 332 | p->nRowid = 0; |
| 333 | p->u.iRowid = iRowid; |
| 334 | } |
| 335 | #endif |
| 336 | |
| 337 | |
| 338 | /* |
| 339 | ** Copy the contents of object (*pFrom) into (*pTo). |
| 340 | */ |
| 341 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 342 | static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){ |
| 343 | pTo->isPSample = pFrom->isPSample; |
| 344 | pTo->iCol = pFrom->iCol; |
| 345 | pTo->iHash = pFrom->iHash; |
| 346 | memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol); |
| 347 | memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol); |
| 348 | memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol); |
| 349 | if( pFrom->nRowid ){ |
| 350 | sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid); |
| 351 | }else{ |
| 352 | sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | #endif |
| 356 | |
| 357 | /* |
| 358 | ** Reclaim all memory of a Stat4Accum structure. |
| 359 | */ |
| 360 | static void stat4Destructor(void *pOld){ |
| 361 | Stat4Accum *p = (Stat4Accum*)pOld; |
| 362 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 363 | int i; |
| 364 | for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i); |
| 365 | for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 366 | sampleClear(p->db, &p->current); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 367 | #endif |
| 368 | sqlite3DbFree(p->db, p); |
| 369 | } |
| 370 | |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 371 | /* |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 372 | ** Implementation of the stat_init(N,K,C) SQL function. The three parameters |
| 373 | ** are: |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 374 | ** N: The number of columns in the index including the rowid/pk (note 1) |
| 375 | ** K: The number of columns in the index excluding the rowid/pk. |
| 376 | ** C: The number of rows in the index (note 2) |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 377 | ** |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 378 | ** Note 1: In the special case of the covering index that implements a |
| 379 | ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the |
| 380 | ** total number of columns in the table. |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 381 | ** |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 382 | ** Note 2: C is only used for STAT3 and STAT4. |
| 383 | ** |
| 384 | ** For indexes on ordinary rowid tables, N==K+1. But for indexes on |
| 385 | ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the |
| 386 | ** PRIMARY KEY of the table. The covering index that implements the |
| 387 | ** original WITHOUT ROWID table as N==K as a special case. |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 388 | ** |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 389 | ** This routine allocates the Stat4Accum object in heap memory. The return |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 390 | ** value is a pointer to the Stat4Accum object. The datatype of the |
drh | f8ede57 | 2014-09-01 23:06:44 +0000 | [diff] [blame] | 391 | ** return value is BLOB, but it is really just a pointer to the Stat4Accum |
| 392 | ** object. |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 393 | */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 394 | static void statInit( |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 395 | sqlite3_context *context, |
| 396 | int argc, |
| 397 | sqlite3_value **argv |
| 398 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 399 | Stat4Accum *p; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 400 | int nCol; /* Number of columns in index being sampled */ |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 401 | int nKeyCol; /* Number of key columns */ |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 402 | int nColUp; /* nCol rounded up for alignment */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 403 | int n; /* Bytes of space to allocate */ |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 404 | sqlite3 *db; /* Database connection */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 405 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 406 | int mxSample = SQLITE_STAT4_SAMPLES; |
| 407 | #endif |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 408 | |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 409 | /* Decode the three function arguments */ |
drh | 6825719 | 2011-08-16 17:06:21 +0000 | [diff] [blame] | 410 | UNUSED_PARAMETER(argc); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 411 | nCol = sqlite3_value_int(argv[0]); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 412 | assert( nCol>0 ); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 413 | nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 414 | nKeyCol = sqlite3_value_int(argv[1]); |
| 415 | assert( nKeyCol<=nCol ); |
| 416 | assert( nKeyCol>0 ); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 417 | |
| 418 | /* Allocate the space required for the Stat4Accum object */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 419 | n = sizeof(*p) |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 420 | + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */ |
| 421 | + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 422 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 423 | + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */ |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 424 | + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */ |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 425 | + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample) |
| 426 | #endif |
| 427 | ; |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 428 | db = sqlite3_context_db_handle(context); |
| 429 | p = sqlite3DbMallocZero(db, n); |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 430 | if( p==0 ){ |
| 431 | sqlite3_result_error_nomem(context); |
| 432 | return; |
| 433 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 434 | |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 435 | p->db = db; |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 436 | p->nRow = 0; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 437 | p->nCol = nCol; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 438 | p->nKeyCol = nKeyCol; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 439 | p->current.anDLt = (tRowcnt*)&p[1]; |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 440 | p->current.anEq = &p->current.anDLt[nColUp]; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 441 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 442 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 443 | { |
| 444 | u8 *pSpace; /* Allocated space not yet assigned */ |
| 445 | int i; /* Used to iterate through p->aSample[] */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 446 | |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 447 | p->iGet = -1; |
| 448 | p->mxSample = mxSample; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 449 | p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 450 | p->current.anLt = &p->current.anEq[nColUp]; |
drh | 4081d5d | 2015-01-01 23:02:01 +0000 | [diff] [blame] | 451 | p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 452 | |
| 453 | /* Set up the Stat4Accum.a[] and aBest[] arrays */ |
| 454 | p->a = (struct Stat4Sample*)&p->current.anLt[nColUp]; |
| 455 | p->aBest = &p->a[mxSample]; |
| 456 | pSpace = (u8*)(&p->a[mxSample+nCol]); |
| 457 | for(i=0; i<(mxSample+nCol); i++){ |
| 458 | p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
| 459 | p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
| 460 | p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
| 461 | } |
| 462 | assert( (pSpace - (u8*)p)==n ); |
| 463 | |
| 464 | for(i=0; i<nCol; i++){ |
| 465 | p->aBest[i].iCol = i; |
| 466 | } |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 467 | } |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 468 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 469 | |
drh | f8ede57 | 2014-09-01 23:06:44 +0000 | [diff] [blame] | 470 | /* Return a pointer to the allocated object to the caller. Note that |
| 471 | ** only the pointer (the 2nd parameter) matters. The size of the object |
| 472 | ** (given by the 3rd parameter) is never used and can be any positive |
| 473 | ** value. */ |
drh | 975e076 | 2014-09-01 22:34:54 +0000 | [diff] [blame] | 474 | sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor); |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 475 | } |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 476 | static const FuncDef statInitFuncdef = { |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 477 | 2+IsStat34, /* nArg */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 478 | SQLITE_UTF8, /* funcFlags */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 479 | 0, /* pUserData */ |
| 480 | 0, /* pNext */ |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 481 | statInit, /* xSFunc */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 482 | 0, /* xFinalize */ |
| 483 | "stat_init", /* zName */ |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 484 | {0} |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 487 | #ifdef SQLITE_ENABLE_STAT4 |
| 488 | /* |
| 489 | ** pNew and pOld are both candidate non-periodic samples selected for |
| 490 | ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and |
| 491 | ** considering only any trailing columns and the sample hash value, this |
| 492 | ** function returns true if sample pNew is to be preferred over pOld. |
| 493 | ** In other words, if we assume that the cardinalities of the selected |
| 494 | ** column for pNew and pOld are equal, is pNew to be preferred over pOld. |
| 495 | ** |
| 496 | ** This function assumes that for each argument sample, the contents of |
| 497 | ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. |
| 498 | */ |
| 499 | static int sampleIsBetterPost( |
| 500 | Stat4Accum *pAccum, |
| 501 | Stat4Sample *pNew, |
| 502 | Stat4Sample *pOld |
| 503 | ){ |
| 504 | int nCol = pAccum->nCol; |
| 505 | int i; |
| 506 | assert( pNew->iCol==pOld->iCol ); |
| 507 | for(i=pNew->iCol+1; i<nCol; i++){ |
| 508 | if( pNew->anEq[i]>pOld->anEq[i] ) return 1; |
| 509 | if( pNew->anEq[i]<pOld->anEq[i] ) return 0; |
| 510 | } |
| 511 | if( pNew->iHash>pOld->iHash ) return 1; |
| 512 | return 0; |
| 513 | } |
| 514 | #endif |
| 515 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 516 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 517 | /* |
| 518 | ** Return true if pNew is to be preferred over pOld. |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 519 | ** |
| 520 | ** This function assumes that for each argument sample, the contents of |
| 521 | ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 522 | */ |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 523 | static int sampleIsBetter( |
| 524 | Stat4Accum *pAccum, |
| 525 | Stat4Sample *pNew, |
| 526 | Stat4Sample *pOld |
| 527 | ){ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 528 | tRowcnt nEqNew = pNew->anEq[pNew->iCol]; |
| 529 | tRowcnt nEqOld = pOld->anEq[pOld->iCol]; |
| 530 | |
| 531 | assert( pOld->isPSample==0 && pNew->isPSample==0 ); |
| 532 | assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) ); |
| 533 | |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 534 | if( (nEqNew>nEqOld) ) return 1; |
| 535 | #ifdef SQLITE_ENABLE_STAT4 |
| 536 | if( nEqNew==nEqOld ){ |
| 537 | if( pNew->iCol<pOld->iCol ) return 1; |
| 538 | return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld)); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 539 | } |
| 540 | return 0; |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 541 | #else |
| 542 | return (nEqNew==nEqOld && pNew->iHash>pOld->iHash); |
| 543 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 544 | } |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 545 | |
| 546 | /* |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 547 | ** Copy the contents of sample *pNew into the p->a[] array. If necessary, |
| 548 | ** remove the least desirable sample from p->a[] to make room. |
| 549 | */ |
| 550 | static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ |
drh | d269461 | 2013-11-04 22:04:17 +0000 | [diff] [blame] | 551 | Stat4Sample *pSample = 0; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 552 | int i; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 553 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 554 | assert( IsStat4 || nEqZero==0 ); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 555 | |
drh | 30f0704 | 2013-09-04 02:07:38 +0000 | [diff] [blame] | 556 | #ifdef SQLITE_ENABLE_STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 557 | if( pNew->isPSample==0 ){ |
dan | 1f616ad | 2013-08-15 14:39:09 +0000 | [diff] [blame] | 558 | Stat4Sample *pUpgrade = 0; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 559 | assert( pNew->anEq[pNew->iCol]>0 ); |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 560 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 561 | /* This sample is being added because the prefix that ends in column |
| 562 | ** iCol occurs many times in the table. However, if we have already |
| 563 | ** added a sample that shares this prefix, there is no need to add |
dan | 1f616ad | 2013-08-15 14:39:09 +0000 | [diff] [blame] | 564 | ** this one. Instead, upgrade the priority of the highest priority |
| 565 | ** existing sample that shares this prefix. */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 566 | for(i=p->nSample-1; i>=0; i--){ |
| 567 | Stat4Sample *pOld = &p->a[i]; |
| 568 | if( pOld->anEq[pNew->iCol]==0 ){ |
dan | 1f616ad | 2013-08-15 14:39:09 +0000 | [diff] [blame] | 569 | if( pOld->isPSample ) return; |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 570 | assert( pOld->iCol>pNew->iCol ); |
| 571 | assert( sampleIsBetter(p, pNew, pOld) ); |
| 572 | if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){ |
dan | 1f616ad | 2013-08-15 14:39:09 +0000 | [diff] [blame] | 573 | pUpgrade = pOld; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 574 | } |
dan | 1f28ead | 2013-08-07 16:15:32 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
dan | 1f616ad | 2013-08-15 14:39:09 +0000 | [diff] [blame] | 577 | if( pUpgrade ){ |
| 578 | pUpgrade->iCol = pNew->iCol; |
| 579 | pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol]; |
| 580 | goto find_new_min; |
| 581 | } |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 582 | } |
drh | 30f0704 | 2013-09-04 02:07:38 +0000 | [diff] [blame] | 583 | #endif |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 584 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 585 | /* If necessary, remove sample iMin to make room for the new sample. */ |
| 586 | if( p->nSample>=p->mxSample ){ |
| 587 | Stat4Sample *pMin = &p->a[p->iMin]; |
dan | c55521a | 2013-08-05 05:34:30 +0000 | [diff] [blame] | 588 | tRowcnt *anEq = pMin->anEq; |
dan | c55521a | 2013-08-05 05:34:30 +0000 | [diff] [blame] | 589 | tRowcnt *anLt = pMin->anLt; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 590 | tRowcnt *anDLt = pMin->anDLt; |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 591 | sampleClear(p->db, pMin); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 592 | memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1)); |
drh | f404c86 | 2011-08-13 15:25:10 +0000 | [diff] [blame] | 593 | pSample = &p->a[p->nSample-1]; |
drh | d269461 | 2013-11-04 22:04:17 +0000 | [diff] [blame] | 594 | pSample->nRowid = 0; |
dan | c55521a | 2013-08-05 05:34:30 +0000 | [diff] [blame] | 595 | pSample->anEq = anEq; |
| 596 | pSample->anDLt = anDLt; |
| 597 | pSample->anLt = anLt; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 598 | p->nSample = p->mxSample-1; |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 599 | } |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 600 | |
dan | b49d104 | 2013-09-02 18:58:11 +0000 | [diff] [blame] | 601 | /* The "rows less-than" for the rowid column must be greater than that |
| 602 | ** for the last sample in the p->a[] array. Otherwise, the samples would |
| 603 | ** be out of order. */ |
| 604 | #ifdef SQLITE_ENABLE_STAT4 |
| 605 | assert( p->nSample==0 |
| 606 | || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] ); |
| 607 | #endif |
| 608 | |
| 609 | /* Insert the new sample */ |
| 610 | pSample = &p->a[p->nSample]; |
| 611 | sampleCopy(p, pSample, pNew); |
| 612 | p->nSample++; |
| 613 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 614 | /* Zero the first nEqZero entries in the anEq[] array. */ |
| 615 | memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero); |
| 616 | |
mistachkin | c2cfb51 | 2013-09-04 04:04:08 +0000 | [diff] [blame] | 617 | #ifdef SQLITE_ENABLE_STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 618 | find_new_min: |
mistachkin | c2cfb51 | 2013-09-04 04:04:08 +0000 | [diff] [blame] | 619 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 620 | if( p->nSample>=p->mxSample ){ |
| 621 | int iMin = -1; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 622 | for(i=0; i<p->mxSample; i++){ |
| 623 | if( p->a[i].isPSample ) continue; |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 624 | if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){ |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 625 | iMin = i; |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 628 | assert( iMin>=0 ); |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 629 | p->iMin = iMin; |
| 630 | } |
| 631 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 632 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | ** Field iChng of the index being scanned has changed. So at this point |
| 636 | ** p->current contains a sample that reflects the previous row of the |
| 637 | ** index. The value of anEq[iChng] and subsequent anEq[] elements are |
| 638 | ** correct at this point. |
| 639 | */ |
| 640 | static void samplePushPrevious(Stat4Accum *p, int iChng){ |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 641 | #ifdef SQLITE_ENABLE_STAT4 |
| 642 | int i; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 643 | |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 644 | /* Check if any samples from the aBest[] array should be pushed |
| 645 | ** into IndexSample.a[] at this point. */ |
| 646 | for(i=(p->nCol-2); i>=iChng; i--){ |
| 647 | Stat4Sample *pBest = &p->aBest[i]; |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 648 | pBest->anEq[i] = p->current.anEq[i]; |
| 649 | if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){ |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 650 | sampleInsert(p, pBest, i); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 654 | /* Update the anEq[] fields of any samples already collected. */ |
| 655 | for(i=p->nSample-1; i>=0; i--){ |
| 656 | int j; |
| 657 | for(j=iChng; j<p->nCol; j++){ |
| 658 | if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j]; |
| 659 | } |
| 660 | } |
| 661 | #endif |
| 662 | |
| 663 | #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4) |
| 664 | if( iChng==0 ){ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 665 | tRowcnt nLt = p->current.anLt[0]; |
| 666 | tRowcnt nEq = p->current.anEq[0]; |
| 667 | |
| 668 | /* Check if this is to be a periodic sample. If so, add it. */ |
| 669 | if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){ |
| 670 | p->current.isPSample = 1; |
| 671 | sampleInsert(p, &p->current, 0); |
| 672 | p->current.isPSample = 0; |
| 673 | }else |
| 674 | |
| 675 | /* 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] | 676 | if( p->nSample<p->mxSample |
| 677 | || sampleIsBetter(p, &p->current, &p->a[p->iMin]) |
| 678 | ){ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 679 | sampleInsert(p, &p->current, 0); |
| 680 | } |
| 681 | } |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 682 | #endif |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 683 | |
| 684 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 685 | UNUSED_PARAMETER( p ); |
| 686 | UNUSED_PARAMETER( iChng ); |
| 687 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 691 | ** Implementation of the stat_push SQL function: stat_push(P,C,R) |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 692 | ** Arguments: |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 693 | ** |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 694 | ** P Pointer to the Stat4Accum object created by stat_init() |
| 695 | ** C Index of left-most column to differ from previous row |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 696 | ** R Rowid for the current row. Might be a key record for |
| 697 | ** WITHOUT ROWID tables. |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 698 | ** |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 699 | ** This SQL function always returns NULL. It's purpose it to accumulate |
| 700 | ** statistical data and/or samples in the Stat4Accum object about the |
| 701 | ** index being analyzed. The stat_get() SQL function will later be used to |
| 702 | ** extract relevant information for constructing the sqlite_statN tables. |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 703 | ** |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 704 | ** The R parameter is only used for STAT3 and STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 705 | */ |
| 706 | static void statPush( |
| 707 | sqlite3_context *context, |
| 708 | int argc, |
| 709 | sqlite3_value **argv |
| 710 | ){ |
| 711 | int i; |
| 712 | |
| 713 | /* The three function arguments */ |
| 714 | Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 715 | int iChng = sqlite3_value_int(argv[1]); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 716 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 717 | UNUSED_PARAMETER( argc ); |
| 718 | UNUSED_PARAMETER( context ); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 719 | assert( p->nCol>0 ); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 720 | assert( iChng<p->nCol ); |
| 721 | |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 722 | if( p->nRow==0 ){ |
dan | b49d104 | 2013-09-02 18:58:11 +0000 | [diff] [blame] | 723 | /* This is the first call to this function. Do initialization. */ |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 724 | for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1; |
| 725 | }else{ |
| 726 | /* Second and subsequent calls get processed here */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 727 | samplePushPrevious(p, iChng); |
| 728 | |
| 729 | /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply |
| 730 | ** to the current row of the index. */ |
| 731 | for(i=0; i<iChng; i++){ |
| 732 | p->current.anEq[i]++; |
| 733 | } |
| 734 | for(i=iChng; i<p->nCol; i++){ |
| 735 | p->current.anDLt[i]++; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 736 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 737 | p->current.anLt[i] += p->current.anEq[i]; |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 738 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 739 | p->current.anEq[i] = 1; |
| 740 | } |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 741 | } |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 742 | p->nRow++; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 743 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 744 | if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 745 | sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 746 | }else{ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 747 | sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), |
| 748 | sqlite3_value_blob(argv[2])); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 749 | } |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 750 | p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; |
| 751 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 752 | |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 753 | #ifdef SQLITE_ENABLE_STAT4 |
| 754 | { |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 755 | tRowcnt nLt = p->current.anLt[p->nCol-1]; |
| 756 | |
| 757 | /* Check if this is to be a periodic sample. If so, add it. */ |
| 758 | if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ |
| 759 | p->current.isPSample = 1; |
| 760 | p->current.iCol = 0; |
| 761 | sampleInsert(p, &p->current, p->nCol-1); |
| 762 | p->current.isPSample = 0; |
| 763 | } |
| 764 | |
| 765 | /* Update the aBest[] array. */ |
| 766 | for(i=0; i<(p->nCol-1); i++){ |
| 767 | p->current.iCol = i; |
dan | b13af4c | 2013-09-03 14:43:12 +0000 | [diff] [blame] | 768 | if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 769 | sampleCopy(p, &p->aBest[i], &p->current); |
| 770 | } |
| 771 | } |
| 772 | } |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 773 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 774 | } |
| 775 | static const FuncDef statPushFuncdef = { |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 776 | 2+IsStat34, /* nArg */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 777 | SQLITE_UTF8, /* funcFlags */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 778 | 0, /* pUserData */ |
| 779 | 0, /* pNext */ |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 780 | statPush, /* xSFunc */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 781 | 0, /* xFinalize */ |
| 782 | "stat_push", /* zName */ |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 783 | {0} |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 784 | }; |
| 785 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 786 | #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */ |
| 787 | #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */ |
| 788 | #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */ |
| 789 | #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */ |
| 790 | #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */ |
| 791 | |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 792 | /* |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 793 | ** Implementation of the stat_get(P,J) SQL function. This routine is |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 794 | ** used to query statistical information that has been gathered into |
| 795 | ** the Stat4Accum object by prior calls to stat_push(). The P parameter |
drh | f8ede57 | 2014-09-01 23:06:44 +0000 | [diff] [blame] | 796 | ** has type BLOB but it is really just a pointer to the Stat4Accum object. |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 797 | ** The content to returned is determined by the parameter J |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 798 | ** which is one of the STAT_GET_xxxx values defined above. |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 799 | ** |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 800 | ** If neither STAT3 nor STAT4 are enabled, then J is always |
| 801 | ** STAT_GET_STAT1 and is hence omitted and this routine becomes |
| 802 | ** a one-parameter function, stat_get(P), that always returns the |
| 803 | ** stat1 table entry information. |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 804 | */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 805 | static void statGet( |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 806 | sqlite3_context *context, |
| 807 | int argc, |
| 808 | sqlite3_value **argv |
| 809 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 810 | Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 811 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 812 | /* STAT3 and STAT4 have a parameter on this routine. */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 813 | int eCall = sqlite3_value_int(argv[1]); |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 814 | assert( argc==2 ); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 815 | assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ |
| 816 | || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT |
| 817 | || eCall==STAT_GET_NDLT |
| 818 | ); |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 819 | if( eCall==STAT_GET_STAT1 ) |
| 820 | #else |
| 821 | assert( argc==1 ); |
| 822 | #endif |
| 823 | { |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 824 | /* Return the value to store in the "stat" column of the sqlite_stat1 |
| 825 | ** table for this index. |
| 826 | ** |
| 827 | ** The value is a string composed of a list of integers describing |
| 828 | ** the index. The first integer in the list is the total number of |
| 829 | ** entries in the index. There is one additional integer in the list |
| 830 | ** for each indexed column. This additional integer is an estimate of |
| 831 | ** the number of rows matched by a stabbing query on the index using |
| 832 | ** a key with the corresponding number of fields. In other words, |
| 833 | ** if the index is on columns (a,b) and the sqlite_stat1 value is |
| 834 | ** "100 10 2", then SQLite estimates that: |
| 835 | ** |
| 836 | ** * the index contains 100 rows, |
| 837 | ** * "WHERE a=?" matches 10 rows, and |
| 838 | ** * "WHERE a=? AND b=?" matches 2 rows. |
| 839 | ** |
| 840 | ** If D is the count of distinct values and K is the total number of |
| 841 | ** rows, then each estimate is computed as: |
| 842 | ** |
| 843 | ** I = (K+D-1)/D |
| 844 | */ |
| 845 | char *z; |
| 846 | int i; |
| 847 | |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 848 | char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 ); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 849 | if( zRet==0 ){ |
| 850 | sqlite3_result_error_nomem(context); |
| 851 | return; |
| 852 | } |
| 853 | |
drh | 26586e7 | 2013-10-09 19:07:22 +0000 | [diff] [blame] | 854 | sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 855 | z = zRet + sqlite3Strlen30(zRet); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 856 | for(i=0; i<p->nKeyCol; i++){ |
drh | 26586e7 | 2013-10-09 19:07:22 +0000 | [diff] [blame] | 857 | u64 nDistinct = p->current.anDLt[i] + 1; |
| 858 | u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; |
| 859 | sqlite3_snprintf(24, z, " %llu", iVal); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 860 | z += sqlite3Strlen30(z); |
| 861 | assert( p->current.anEq[i] ); |
| 862 | } |
| 863 | assert( z[0]=='\0' && z>zRet ); |
| 864 | |
| 865 | sqlite3_result_text(context, zRet, -1, sqlite3_free); |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 866 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 867 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 868 | else if( eCall==STAT_GET_ROWID ){ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 869 | if( p->iGet<0 ){ |
| 870 | samplePushPrevious(p, 0); |
| 871 | p->iGet = 0; |
| 872 | } |
| 873 | if( p->iGet<p->nSample ){ |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 874 | Stat4Sample *pS = p->a + p->iGet; |
| 875 | if( pS->nRowid==0 ){ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 876 | sqlite3_result_int64(context, pS->u.iRowid); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 877 | }else{ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 878 | sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid, |
| 879 | SQLITE_TRANSIENT); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 880 | } |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 881 | } |
| 882 | }else{ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 883 | tRowcnt *aCnt = 0; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 884 | |
| 885 | assert( p->iGet<p->nSample ); |
| 886 | switch( eCall ){ |
| 887 | case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break; |
| 888 | case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break; |
| 889 | default: { |
| 890 | aCnt = p->a[p->iGet].anDLt; |
| 891 | p->iGet++; |
| 892 | break; |
| 893 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 894 | } |
| 895 | |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 896 | if( IsStat3 ){ |
| 897 | sqlite3_result_int64(context, (i64)aCnt[0]); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 898 | }else{ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 899 | char *zRet = sqlite3MallocZero(p->nCol * 25); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 900 | if( zRet==0 ){ |
| 901 | sqlite3_result_error_nomem(context); |
| 902 | }else{ |
| 903 | int i; |
| 904 | char *z = zRet; |
| 905 | for(i=0; i<p->nCol; i++){ |
drh | 26586e7 | 2013-10-09 19:07:22 +0000 | [diff] [blame] | 906 | sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 907 | z += sqlite3Strlen30(z); |
| 908 | } |
| 909 | assert( z[0]=='\0' && z>zRet ); |
| 910 | z[-1] = '\0'; |
| 911 | sqlite3_result_text(context, zRet, -1, sqlite3_free); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 912 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 913 | } |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 914 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 915 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 916 | #ifndef SQLITE_DEBUG |
| 917 | UNUSED_PARAMETER( argc ); |
| 918 | #endif |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 919 | } |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 920 | static const FuncDef statGetFuncdef = { |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 921 | 1+IsStat34, /* nArg */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 922 | SQLITE_UTF8, /* funcFlags */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 923 | 0, /* pUserData */ |
| 924 | 0, /* pNext */ |
drh | 2d80151 | 2016-01-14 22:19:58 +0000 | [diff] [blame] | 925 | statGet, /* xSFunc */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 926 | 0, /* xFinalize */ |
| 927 | "stat_get", /* zName */ |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 928 | {0} |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 929 | }; |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 930 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 931 | static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){ |
| 932 | assert( regOut!=regStat4 && regOut!=regStat4+1 ); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 933 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 934 | sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1); |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 935 | #elif SQLITE_DEBUG |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 936 | assert( iParam==STAT_GET_STAT1 ); |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 937 | #else |
| 938 | UNUSED_PARAMETER( iParam ); |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 939 | #endif |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 940 | sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut, |
| 941 | (char*)&statGetFuncdef, P4_FUNCDEF); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 942 | sqlite3VdbeChangeP5(v, 1 + IsStat34); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 943 | } |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 944 | |
| 945 | /* |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 946 | ** Generate code to do an analysis of all indices associated with |
| 947 | ** a single table. |
| 948 | */ |
| 949 | static void analyzeOneTable( |
| 950 | Parse *pParse, /* Parser context */ |
| 951 | Table *pTab, /* Table whose indices are to be analyzed */ |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 952 | Index *pOnlyIdx, /* If not NULL, only analyze this one index */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 953 | int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 954 | int iMem, /* Available memory locations begin here */ |
| 955 | int iTab /* Next available cursor */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 956 | ){ |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 957 | sqlite3 *db = pParse->db; /* Database handle */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 958 | Index *pIdx; /* An index to being analyzed */ |
| 959 | int iIdxCur; /* Cursor open on index being analyzed */ |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 960 | int iTabCur; /* Table cursor */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 961 | Vdbe *v; /* The virtual machine being built up */ |
| 962 | int i; /* Loop counter */ |
drh | f6cf1ff | 2011-03-30 14:54:05 +0000 | [diff] [blame] | 963 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 964 | int iDb; /* Index of database containing pTab */ |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 965 | u8 needTableCnt = 1; /* True to count the table */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 966 | int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 967 | int regStat4 = iMem++; /* Register to hold Stat4Accum object */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 968 | int regChng = iMem++; /* Index of changed index field */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 969 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 970 | int regRowid = iMem++; /* Rowid argument passed to stat_push() */ |
| 971 | #endif |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 972 | int regTemp = iMem++; /* Temporary use register */ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 973 | int regTabname = iMem++; /* Register containing table name */ |
| 974 | int regIdxname = iMem++; /* Register containing index name */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 975 | int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ |
| 976 | int regPrev = iMem; /* MUST BE LAST (see below) */ |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 977 | |
drh | f0459fc | 2013-08-15 16:15:00 +0000 | [diff] [blame] | 978 | pParse->nMem = MAX(pParse->nMem, iMem); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 979 | v = sqlite3GetVdbe(pParse); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 980 | if( v==0 || NEVER(pTab==0) ){ |
| 981 | return; |
| 982 | } |
drh | f39d29c | 2010-09-28 17:34:46 +0000 | [diff] [blame] | 983 | if( pTab->tnum==0 ){ |
| 984 | /* Do not gather statistics on views or virtual tables */ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
drh | 8b4a94a | 2015-11-24 21:23:59 +0000 | [diff] [blame] | 987 | if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 988 | /* Do not gather statistics on system tables */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 991 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 992 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 993 | assert( iDb>=0 ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 994 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 995 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 996 | if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 997 | db->aDb[iDb].zName ) ){ |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | #endif |
| 1001 | |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1002 | /* Establish a read-lock on the table at the shared-cache level. |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1003 | ** Open a read-only cursor on the table. Also allocate a cursor number |
| 1004 | ** to use for scanning indexes (iIdxCur). No index cursor is opened at |
| 1005 | ** this time though. */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 1006 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1007 | iTabCur = iTab++; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1008 | iIdxCur = iTab++; |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1009 | pParse->nTab = MAX(pParse->nTab, iTab); |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1010 | sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1011 | sqlite3VdbeLoadString(v, regTabname, pTab->zName); |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1012 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1013 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1014 | int nCol; /* Number of columns in pIdx. "N" */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1015 | int addrRewind; /* Address of "OP_Rewind iIdxCur" */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1016 | int addrNextRow; /* Address of "next_row:" */ |
drh | ce95d11 | 2013-11-02 19:34:38 +0000 | [diff] [blame] | 1017 | const char *zIdxName; /* Name of the index */ |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1018 | int nColTest; /* Number of columns to test for changes */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1019 | |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1020 | if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1021 | if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1022 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 1023 | nCol = pIdx->nKeyCol; |
| 1024 | zIdxName = pTab->zName; |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1025 | nColTest = nCol - 1; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1026 | }else{ |
| 1027 | nCol = pIdx->nColumn; |
| 1028 | zIdxName = pIdx->zName; |
drh | 6861b8a | 2014-07-23 19:37:21 +0000 | [diff] [blame] | 1029 | nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1; |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1030 | } |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1031 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1032 | /* Populate the register containing the index name. */ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1033 | sqlite3VdbeLoadString(v, regIdxname, zIdxName); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1034 | VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName)); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 1035 | |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1036 | /* |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1037 | ** Pseudo-code for loop that calls stat_push(): |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1038 | ** |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1039 | ** Rewind csr |
| 1040 | ** if eof(csr) goto end_of_scan; |
| 1041 | ** regChng = 0 |
| 1042 | ** goto chng_addr_0; |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1043 | ** |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 1044 | ** next_row: |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1045 | ** regChng = 0 |
| 1046 | ** if( idx(0) != regPrev(0) ) goto chng_addr_0 |
| 1047 | ** regChng = 1 |
| 1048 | ** if( idx(1) != regPrev(1) ) goto chng_addr_1 |
| 1049 | ** ... |
| 1050 | ** regChng = N |
| 1051 | ** goto chng_addr_N |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1052 | ** |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1053 | ** chng_addr_0: |
| 1054 | ** regPrev(0) = idx(0) |
| 1055 | ** chng_addr_1: |
| 1056 | ** regPrev(1) = idx(1) |
| 1057 | ** ... |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1058 | ** |
drh | 9d79332 | 2014-07-24 19:54:20 +0000 | [diff] [blame] | 1059 | ** endDistinctTest: |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1060 | ** regRowid = idx(rowid) |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1061 | ** stat_push(P, regChng, regRowid) |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1062 | ** Next csr |
| 1063 | ** if !eof(csr) goto next_row; |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1064 | ** |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1065 | ** end_of_scan: |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1066 | */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* Make sure there are enough memory cells allocated to accommodate |
| 1069 | ** the regPrev array and a trailing rowid (the rowid slot is required |
| 1070 | ** when building a record to insert into the sample column of |
| 1071 | ** the sqlite_stat4 table. */ |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1072 | pParse->nMem = MAX(pParse->nMem, regPrev+nColTest); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1073 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1074 | /* Open a read-only cursor on the index being analyzed. */ |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1075 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1076 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1077 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1078 | VdbeComment((v, "%s", pIdx->zName)); |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1079 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1080 | /* Invoke the stat_init() function. The arguments are: |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1081 | ** |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1082 | ** (1) the number of columns in the index including the rowid |
| 1083 | ** (or for a WITHOUT ROWID table, the number of PK columns), |
| 1084 | ** (2) the number of columns in the key without the rowid/pk |
| 1085 | ** (3) the number of rows in the index, |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1086 | ** |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1087 | ** |
| 1088 | ** The third argument is only used for STAT3 and STAT4 |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1089 | */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1090 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1091 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1092 | #endif |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1093 | sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); |
| 1094 | sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 1095 | sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4, |
| 1096 | (char*)&statInitFuncdef, P4_FUNCDEF); |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1097 | sqlite3VdbeChangeP5(v, 2+IsStat34); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1098 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1099 | /* Implementation of the following: |
| 1100 | ** |
| 1101 | ** Rewind csr |
| 1102 | ** if eof(csr) goto end_of_scan; |
| 1103 | ** regChng = 0 |
| 1104 | ** goto next_push_0; |
| 1105 | ** |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 1106 | */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1107 | addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1108 | VdbeCoverage(v); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1109 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1110 | addrNextRow = sqlite3VdbeCurrentAddr(v); |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1111 | |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1112 | if( nColTest>0 ){ |
drh | 9d79332 | 2014-07-24 19:54:20 +0000 | [diff] [blame] | 1113 | int endDistinctTest = sqlite3VdbeMakeLabel(v); |
drh | b12879f | 2014-07-24 20:25:16 +0000 | [diff] [blame] | 1114 | int *aGotoChng; /* Array of jump instruction addresses */ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1115 | aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest); |
drh | b12879f | 2014-07-24 20:25:16 +0000 | [diff] [blame] | 1116 | if( aGotoChng==0 ) continue; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1117 | |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1118 | /* |
| 1119 | ** next_row: |
| 1120 | ** regChng = 0 |
| 1121 | ** if( idx(0) != regPrev(0) ) goto chng_addr_0 |
| 1122 | ** regChng = 1 |
| 1123 | ** if( idx(1) != regPrev(1) ) goto chng_addr_1 |
| 1124 | ** ... |
| 1125 | ** regChng = N |
drh | 9d79332 | 2014-07-24 19:54:20 +0000 | [diff] [blame] | 1126 | ** goto endDistinctTest |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1127 | */ |
| 1128 | sqlite3VdbeAddOp0(v, OP_Goto); |
| 1129 | addrNextRow = sqlite3VdbeCurrentAddr(v); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 1130 | if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){ |
drh | 9d79332 | 2014-07-24 19:54:20 +0000 | [diff] [blame] | 1131 | /* For a single-column UNIQUE index, once we have found a non-NULL |
| 1132 | ** row, we know that all the rest will be distinct, so skip |
| 1133 | ** subsequent distinctness tests. */ |
| 1134 | sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest); |
| 1135 | VdbeCoverage(v); |
| 1136 | } |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1137 | for(i=0; i<nColTest; i++){ |
| 1138 | char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 1139 | sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); |
| 1140 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); |
| 1141 | aGotoChng[i] = |
| 1142 | sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); |
| 1143 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 1144 | VdbeCoverage(v); |
| 1145 | } |
| 1146 | sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1147 | sqlite3VdbeGoto(v, endDistinctTest); |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1148 | |
| 1149 | |
| 1150 | /* |
| 1151 | ** chng_addr_0: |
| 1152 | ** regPrev(0) = idx(0) |
| 1153 | ** chng_addr_1: |
| 1154 | ** regPrev(1) = idx(1) |
| 1155 | ** ... |
| 1156 | */ |
| 1157 | sqlite3VdbeJumpHere(v, addrNextRow-1); |
| 1158 | for(i=0; i<nColTest; i++){ |
| 1159 | sqlite3VdbeJumpHere(v, aGotoChng[i]); |
| 1160 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i); |
| 1161 | } |
drh | 9d79332 | 2014-07-24 19:54:20 +0000 | [diff] [blame] | 1162 | sqlite3VdbeResolveLabel(v, endDistinctTest); |
drh | b12879f | 2014-07-24 20:25:16 +0000 | [diff] [blame] | 1163 | sqlite3DbFree(db, aGotoChng); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1164 | } |
drh | 553818a | 2014-07-23 18:36:55 +0000 | [diff] [blame] | 1165 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1166 | /* |
| 1167 | ** chng_addr_N: |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1168 | ** regRowid = idx(rowid) // STAT34 only |
| 1169 | ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1170 | ** Next csr |
| 1171 | ** if !eof(csr) goto next_row; |
| 1172 | */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1173 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1174 | assert( regRowid==(regStat4+2) ); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 1175 | if( HasRowid(pTab) ){ |
| 1176 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); |
| 1177 | }else{ |
| 1178 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 1179 | int j, k, regKey; |
| 1180 | regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 1181 | for(j=0; j<pPk->nKeyCol; j++){ |
| 1182 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1183 | assert( k>=0 && k<pTab->nCol ); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 1184 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); |
| 1185 | VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName)); |
| 1186 | } |
| 1187 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
| 1188 | sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
| 1189 | } |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1190 | #endif |
| 1191 | assert( regChng==(regStat4+1) ); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 1192 | sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp, |
| 1193 | (char*)&statPushFuncdef, P4_FUNCDEF); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1194 | sqlite3VdbeChangeP5(v, 2+IsStat34); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1195 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* Add the entry to the stat1 table. */ |
| 1198 | callStatGet(v, regStat4, STAT_GET_STAT1, regStat1); |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 1199 | assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
| 1200 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 1201 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1202 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1203 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* Add the entries to the stat3 or stat4 table. */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1206 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1207 | { |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1208 | int regEq = regStat1; |
| 1209 | int regLt = regStat1+1; |
| 1210 | int regDLt = regStat1+2; |
| 1211 | int regSample = regStat1+3; |
| 1212 | int regCol = regStat1+4; |
| 1213 | int regSampleRowid = regCol + nCol; |
| 1214 | int addrNext; |
| 1215 | int addrIsNull; |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 1216 | u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1217 | |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1218 | pParse->nMem = MAX(pParse->nMem, regCol+nCol); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1219 | |
| 1220 | addrNext = sqlite3VdbeCurrentAddr(v); |
| 1221 | callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid); |
| 1222 | addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1223 | VdbeCoverage(v); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1224 | callStatGet(v, regStat4, STAT_GET_NEQ, regEq); |
| 1225 | callStatGet(v, regStat4, STAT_GET_NLT, regLt); |
| 1226 | callStatGet(v, regStat4, STAT_GET_NDLT, regDLt); |
drh | ebe25af | 2013-11-02 18:46:04 +0000 | [diff] [blame] | 1227 | sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 1228 | /* We know that the regSampleRowid row exists because it was read by |
| 1229 | ** the previous loop. Thus the not-found jump of seekOp will never |
| 1230 | ** be taken */ |
| 1231 | VdbeCoverageNeverTaken(v); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1232 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1233 | sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1234 | #else |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1235 | for(i=0; i<nCol; i++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1236 | sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1237 | } |
drh | ec9e55d | 2014-06-30 17:07:39 +0000 | [diff] [blame] | 1238 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample); |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1239 | #endif |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1240 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1241 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); |
| 1242 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid); |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 1243 | sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1244 | sqlite3VdbeJumpHere(v, addrIsNull); |
| 1245 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1246 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1247 | |
drh | 9fecc54 | 2013-08-27 20:16:48 +0000 | [diff] [blame] | 1248 | /* End of analysis */ |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1249 | sqlite3VdbeJumpHere(v, addrRewind); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1252 | |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1253 | /* Create a single sqlite_stat1 entry containing NULL as the index |
| 1254 | ** name and the row count as the content. |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1255 | */ |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1256 | if( pOnlyIdx==0 && needTableCnt ){ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1257 | VdbeComment((v, "%s", pTab->zName)); |
dan | e043201 | 2013-08-05 18:00:56 +0000 | [diff] [blame] | 1258 | sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1259 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v); |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1260 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 1261 | assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
| 1262 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1263 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
dan | f00e902 | 2013-08-14 19:54:12 +0000 | [diff] [blame] | 1264 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
drh | 721dfcf | 2013-08-01 04:39:17 +0000 | [diff] [blame] | 1265 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1266 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 1267 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1270 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1271 | /* |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1272 | ** Generate code that will cause the most recent index analysis to |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1273 | ** be loaded into internal hash tables where is can be used. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1274 | */ |
| 1275 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 1276 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1277 | if( v ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1278 | sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1279 | } |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /* |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1283 | ** Generate code that will do an analysis of an entire database |
| 1284 | */ |
| 1285 | static void analyzeDatabase(Parse *pParse, int iDb){ |
| 1286 | sqlite3 *db = pParse->db; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 1287 | Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1288 | HashElem *k; |
| 1289 | int iStatCur; |
| 1290 | int iMem; |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1291 | int iTab; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1292 | |
| 1293 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1294 | iStatCur = pParse->nTab; |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 1295 | pParse->nTab += 3; |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1296 | openStatTable(pParse, iDb, iStatCur, 0, 0); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1297 | iMem = pParse->nMem+1; |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1298 | iTab = pParse->nTab; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 1299 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1300 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1301 | Table *pTab = (Table*)sqliteHashData(k); |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1302 | analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1303 | } |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1304 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | ** Generate code that will do an analysis of a single table in |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1309 | ** a database. If pOnlyIdx is not NULL then it is a single index |
| 1310 | ** in pTab that should be analyzed. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1311 | */ |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1312 | static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1313 | int iDb; |
| 1314 | int iStatCur; |
| 1315 | |
| 1316 | assert( pTab!=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1317 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1318 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1319 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1320 | iStatCur = pParse->nTab; |
drh | ade3add | 2011-08-13 00:58:05 +0000 | [diff] [blame] | 1321 | pParse->nTab += 3; |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1322 | if( pOnlyIdx ){ |
| 1323 | openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
| 1324 | }else{ |
| 1325 | openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
| 1326 | } |
dan | c612970 | 2013-08-05 19:04:07 +0000 | [diff] [blame] | 1327 | analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1328 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | ** Generate code for the ANALYZE command. The parser calls this routine |
| 1333 | ** when it recognizes an ANALYZE command. |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1334 | ** |
| 1335 | ** ANALYZE -- 1 |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1336 | ** ANALYZE <database> -- 2 |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1337 | ** ANALYZE ?<database>.?<tablename> -- 3 |
| 1338 | ** |
| 1339 | ** Form 1 causes all indices in all attached databases to be analyzed. |
| 1340 | ** Form 2 analyzes all indices the single database named. |
| 1341 | ** Form 3 analyzes all indices associated with the named table. |
| 1342 | */ |
| 1343 | void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1344 | sqlite3 *db = pParse->db; |
| 1345 | int iDb; |
| 1346 | int i; |
| 1347 | char *z, *zDb; |
| 1348 | Table *pTab; |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1349 | Index *pIdx; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1350 | Token *pTableName; |
drh | 358406f | 2014-07-22 14:42:16 +0000 | [diff] [blame] | 1351 | Vdbe *v; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1352 | |
| 1353 | /* Read the database schema. If an error occurs, leave an error message |
| 1354 | ** and code in pParse and return NULL. */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1355 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1356 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 1357 | return; |
| 1358 | } |
| 1359 | |
drh | 05800a1 | 2009-04-16 17:45:47 +0000 | [diff] [blame] | 1360 | assert( pName2!=0 || pName1==0 ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1361 | if( pName1==0 ){ |
| 1362 | /* Form 1: Analyze everything */ |
| 1363 | for(i=0; i<db->nDb; i++){ |
| 1364 | if( i==1 ) continue; /* Do not analyze the TEMP database */ |
| 1365 | analyzeDatabase(pParse, i); |
| 1366 | } |
drh | 05800a1 | 2009-04-16 17:45:47 +0000 | [diff] [blame] | 1367 | }else if( pName2->n==0 ){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1368 | /* Form 2: Analyze the database or table named */ |
| 1369 | iDb = sqlite3FindDb(db, pName1); |
| 1370 | if( iDb>=0 ){ |
| 1371 | analyzeDatabase(pParse, iDb); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 1372 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1373 | z = sqlite3NameFromToken(db, pName1); |
danielk1977 | b8b4bfa | 2007-11-15 13:10:22 +0000 | [diff] [blame] | 1374 | if( z ){ |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1375 | if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ |
| 1376 | analyzeTable(pParse, pIdx->pTable, pIdx); |
| 1377 | }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ |
| 1378 | analyzeTable(pParse, pTab, 0); |
danielk1977 | b8b4bfa | 2007-11-15 13:10:22 +0000 | [diff] [blame] | 1379 | } |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1380 | sqlite3DbFree(db, z); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 1381 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1382 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1383 | }else{ |
| 1384 | /* Form 3: Analyze the fully qualified table name */ |
| 1385 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); |
| 1386 | if( iDb>=0 ){ |
| 1387 | zDb = db->aDb[iDb].zName; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1388 | z = sqlite3NameFromToken(db, pTableName); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1389 | if( z ){ |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1390 | if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ |
| 1391 | analyzeTable(pParse, pIdx->pTable, pIdx); |
| 1392 | }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){ |
| 1393 | analyzeTable(pParse, pTab, 0); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1394 | } |
drh | a071bc5 | 2011-03-31 02:03:28 +0000 | [diff] [blame] | 1395 | sqlite3DbFree(db, z); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | } |
drh | 358406f | 2014-07-22 14:42:16 +0000 | [diff] [blame] | 1399 | v = sqlite3GetVdbe(pParse); |
| 1400 | if( v ) sqlite3VdbeAddOp0(v, OP_Expire); |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1403 | /* |
| 1404 | ** Used to pass information from the analyzer reader through to the |
| 1405 | ** callback routine. |
| 1406 | */ |
| 1407 | typedef struct analysisInfo analysisInfo; |
| 1408 | struct analysisInfo { |
| 1409 | sqlite3 *db; |
| 1410 | const char *zDatabase; |
| 1411 | }; |
| 1412 | |
| 1413 | /* |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1414 | ** The first argument points to a nul-terminated string containing a |
| 1415 | ** list of space separated integers. Read the first nOut of these into |
| 1416 | ** the array aOut[]. |
| 1417 | */ |
| 1418 | static void decodeIntArray( |
drh | c28c4e5 | 2013-10-03 19:21:41 +0000 | [diff] [blame] | 1419 | char *zIntArray, /* String containing int array to decode */ |
| 1420 | int nOut, /* Number of slots in aOut[] */ |
| 1421 | tRowcnt *aOut, /* Store integers here */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1422 | LogEst *aLog, /* Or, if aOut==0, here */ |
drh | c28c4e5 | 2013-10-03 19:21:41 +0000 | [diff] [blame] | 1423 | Index *pIndex /* Handle extra flags for this index, if not NULL */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1424 | ){ |
| 1425 | char *z = zIntArray; |
| 1426 | int c; |
| 1427 | int i; |
| 1428 | tRowcnt v; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1429 | |
drh | 6d57bbf | 2013-08-28 11:43:49 +0000 | [diff] [blame] | 1430 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 1431 | if( z==0 ) z = ""; |
| 1432 | #else |
drh | 85d117b | 2014-10-06 18:33:49 +0000 | [diff] [blame] | 1433 | assert( z!=0 ); |
drh | 6d57bbf | 2013-08-28 11:43:49 +0000 | [diff] [blame] | 1434 | #endif |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1435 | for(i=0; *z && i<nOut; i++){ |
| 1436 | v = 0; |
| 1437 | while( (c=z[0])>='0' && c<='9' ){ |
| 1438 | v = v*10 + c - '0'; |
| 1439 | z++; |
| 1440 | } |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 1441 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 85d117b | 2014-10-06 18:33:49 +0000 | [diff] [blame] | 1442 | if( aOut ) aOut[i] = v; |
| 1443 | if( aLog ) aLog[i] = sqlite3LogEst(v); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 1444 | #else |
| 1445 | assert( aOut==0 ); |
| 1446 | UNUSED_PARAMETER(aOut); |
drh | 85d117b | 2014-10-06 18:33:49 +0000 | [diff] [blame] | 1447 | assert( aLog!=0 ); |
| 1448 | aLog[i] = sqlite3LogEst(v); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 1449 | #endif |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1450 | if( *z==' ' ) z++; |
| 1451 | } |
drh | 1f1fc0c | 2013-10-08 23:16:48 +0000 | [diff] [blame] | 1452 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0da10d32 | 2014-11-22 21:37:00 +0000 | [diff] [blame] | 1453 | assert( pIndex!=0 ); { |
drh | 1f1fc0c | 2013-10-08 23:16:48 +0000 | [diff] [blame] | 1454 | #else |
drh | 0da10d32 | 2014-11-22 21:37:00 +0000 | [diff] [blame] | 1455 | if( pIndex ){ |
drh | 1f1fc0c | 2013-10-08 23:16:48 +0000 | [diff] [blame] | 1456 | #endif |
drh | 0da10d32 | 2014-11-22 21:37:00 +0000 | [diff] [blame] | 1457 | pIndex->bUnordered = 0; |
| 1458 | pIndex->noSkipScan = 0; |
| 1459 | while( z[0] ){ |
| 1460 | if( sqlite3_strglob("unordered*", z)==0 ){ |
| 1461 | pIndex->bUnordered = 1; |
| 1462 | }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
| 1463 | pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
| 1464 | }else if( sqlite3_strglob("noskipscan*", z)==0 ){ |
| 1465 | pIndex->noSkipScan = 1; |
| 1466 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 1467 | #ifdef SQLITE_ENABLE_COSTMULT |
drh | 0da10d32 | 2014-11-22 21:37:00 +0000 | [diff] [blame] | 1468 | else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
| 1469 | pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
| 1470 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 1471 | #endif |
drh | 0da10d32 | 2014-11-22 21:37:00 +0000 | [diff] [blame] | 1472 | while( z[0]!=0 && z[0]!=' ' ) z++; |
| 1473 | while( z[0]==' ' ) z++; |
| 1474 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1479 | ** This callback is invoked once for each index when reading the |
| 1480 | ** sqlite_stat1 table. |
| 1481 | ** |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1482 | ** argv[0] = name of the table |
| 1483 | ** argv[1] = name of the index (might be NULL) |
| 1484 | ** argv[2] = results of analysis - on integer for each column |
| 1485 | ** |
| 1486 | ** Entries for which argv[1]==NULL simply record the number of rows in |
| 1487 | ** the table. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1488 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 1489 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1490 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 1491 | Index *pIndex; |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1492 | Table *pTable; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1493 | const char *z; |
| 1494 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1495 | assert( argc==3 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 1496 | UNUSED_PARAMETER2(NotUsed, argc); |
| 1497 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1498 | if( argv==0 || argv[0]==0 || argv[2]==0 ){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1499 | return 0; |
| 1500 | } |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1501 | pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); |
| 1502 | if( pTable==0 ){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1503 | return 0; |
| 1504 | } |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 1505 | if( argv[1]==0 ){ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1506 | pIndex = 0; |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 1507 | }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){ |
| 1508 | pIndex = sqlite3PrimaryKeyIndex(pTable); |
| 1509 | }else{ |
| 1510 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1511 | } |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 1512 | z = argv[2]; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1513 | |
| 1514 | if( pIndex ){ |
dan | 4eed053 | 2015-04-20 15:13:08 +0000 | [diff] [blame] | 1515 | tRowcnt *aiRowEst = 0; |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1516 | int nCol = pIndex->nKeyCol+1; |
| 1517 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 4eed053 | 2015-04-20 15:13:08 +0000 | [diff] [blame] | 1518 | /* Index.aiRowEst may already be set here if there are duplicate |
| 1519 | ** sqlite_stat1 entries for this index. In that case just clobber |
| 1520 | ** the old data with the new instead of allocating a new array. */ |
| 1521 | if( pIndex->aiRowEst==0 ){ |
| 1522 | pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1523 | if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db); |
dan | 4eed053 | 2015-04-20 15:13:08 +0000 | [diff] [blame] | 1524 | } |
| 1525 | aiRowEst = pIndex->aiRowEst; |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1526 | #endif |
drh | 25df48d | 2014-07-22 14:58:12 +0000 | [diff] [blame] | 1527 | pIndex->bUnordered = 0; |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1528 | decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1529 | if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1530 | }else{ |
drh | 52f6c91 | 2013-10-06 22:12:41 +0000 | [diff] [blame] | 1531 | Index fakeIdx; |
| 1532 | fakeIdx.szIdxRow = pTable->szTabRow; |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 1533 | #ifdef SQLITE_ENABLE_COSTMULT |
| 1534 | fakeIdx.pTable = pTable; |
| 1535 | #endif |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1536 | decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx); |
drh | 52f6c91 | 2013-10-06 22:12:41 +0000 | [diff] [blame] | 1537 | pTable->szTabRow = fakeIdx.szIdxRow; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1538 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1539 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1544 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 1545 | ** and its contents. |
| 1546 | */ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 1547 | void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1548 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1549 | if( pIdx->aSample ){ |
| 1550 | int j; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1551 | for(j=0; j<pIdx->nSample; j++){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1552 | IndexSample *p = &pIdx->aSample[j]; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1553 | sqlite3DbFree(db, p->p); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1554 | } |
drh | 8e3937f | 2011-08-18 13:45:23 +0000 | [diff] [blame] | 1555 | sqlite3DbFree(db, pIdx->aSample); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1556 | } |
drh | 8e3937f | 2011-08-18 13:45:23 +0000 | [diff] [blame] | 1557 | if( db && db->pnBytesFreed==0 ){ |
| 1558 | pIdx->nSample = 0; |
| 1559 | pIdx->aSample = 0; |
| 1560 | } |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 1561 | #else |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 1562 | UNUSED_PARAMETER(db); |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 1563 | UNUSED_PARAMETER(pIdx); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1564 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1567 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1568 | /* |
| 1569 | ** Populate the pIdx->aAvgEq[] array based on the samples currently |
| 1570 | ** stored in pIdx->aSample[]. |
| 1571 | */ |
| 1572 | static void initAvgEq(Index *pIdx){ |
| 1573 | if( pIdx ){ |
| 1574 | IndexSample *aSample = pIdx->aSample; |
| 1575 | IndexSample *pFinal = &aSample[pIdx->nSample-1]; |
| 1576 | int iCol; |
dan | 39caccf | 2014-07-01 11:54:02 +0000 | [diff] [blame] | 1577 | int nCol = 1; |
| 1578 | if( pIdx->nSampleCol>1 ){ |
| 1579 | /* If this is stat4 data, then calculate aAvgEq[] values for all |
| 1580 | ** sample columns except the last. The last is always set to 1, as |
| 1581 | ** once the trailing PK fields are considered all index keys are |
| 1582 | ** unique. */ |
| 1583 | nCol = pIdx->nSampleCol-1; |
| 1584 | pIdx->aAvgEq[nCol] = 1; |
| 1585 | } |
| 1586 | for(iCol=0; iCol<nCol; iCol++){ |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1587 | int nSample = pIdx->nSample; |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1588 | int i; /* Used to iterate through samples */ |
| 1589 | tRowcnt sumEq = 0; /* Sum of the nEq values */ |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1590 | tRowcnt avgEq = 0; |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1591 | tRowcnt nRow; /* Number of rows in index */ |
| 1592 | i64 nSum100 = 0; /* Number of terms contributing to sumEq */ |
| 1593 | i64 nDist100; /* Number of distinct values in index */ |
| 1594 | |
dan | 5cca94e | 2014-12-05 21:04:26 +0000 | [diff] [blame] | 1595 | if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){ |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1596 | nRow = pFinal->anLt[iCol]; |
| 1597 | nDist100 = (i64)100 * pFinal->anDLt[iCol]; |
| 1598 | nSample--; |
| 1599 | }else{ |
| 1600 | nRow = pIdx->aiRowEst[0]; |
| 1601 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 1602 | } |
drh | 9f07cf7 | 2014-10-22 15:27:05 +0000 | [diff] [blame] | 1603 | pIdx->nRowEst0 = nRow; |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1604 | |
| 1605 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1606 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 1607 | ** the nEq values for column iCol for the same set (adding the value |
| 1608 | ** only once where there exist duplicate prefixes). */ |
| 1609 | for(i=0; i<nSample; i++){ |
| 1610 | if( i==(pIdx->nSample-1) |
| 1611 | || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] |
| 1612 | ){ |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1613 | sumEq += aSample[i].anEq[iCol]; |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1614 | nSum100 += 100; |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1615 | } |
| 1616 | } |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1617 | |
| 1618 | if( nDist100>nSum100 ){ |
| 1619 | avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1620 | } |
| 1621 | if( avgEq==0 ) avgEq = 1; |
| 1622 | pIdx->aAvgEq[iCol] = avgEq; |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1627 | /* |
drh | ce95d11 | 2013-11-02 19:34:38 +0000 | [diff] [blame] | 1628 | ** Look up an index by name. Or, if the name of a WITHOUT ROWID table |
| 1629 | ** is supplied instead, find the PRIMARY KEY index for that table. |
| 1630 | */ |
| 1631 | static Index *findIndexOrPrimaryKey( |
| 1632 | sqlite3 *db, |
| 1633 | const char *zName, |
| 1634 | const char *zDb |
| 1635 | ){ |
| 1636 | Index *pIdx = sqlite3FindIndex(db, zName, zDb); |
| 1637 | if( pIdx==0 ){ |
| 1638 | Table *pTab = sqlite3FindTable(db, zName, zDb); |
| 1639 | if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab); |
| 1640 | } |
| 1641 | return pIdx; |
| 1642 | } |
| 1643 | |
| 1644 | /* |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1645 | ** Load the content from either the sqlite_stat4 or sqlite_stat3 table |
| 1646 | ** into the relevant Index.aSample[] arrays. |
| 1647 | ** |
| 1648 | ** Arguments zSql1 and zSql2 must point to SQL statements that return |
| 1649 | ** data equivalent to the following (statements are different for stat3, |
| 1650 | ** see the caller of this function for details): |
| 1651 | ** |
| 1652 | ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx |
| 1653 | ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4 |
| 1654 | ** |
| 1655 | ** where %Q is replaced with the database name before the SQL is executed. |
| 1656 | */ |
| 1657 | static int loadStatTbl( |
| 1658 | sqlite3 *db, /* Database handle */ |
| 1659 | int bStat3, /* Assume single column records only */ |
| 1660 | const char *zSql1, /* SQL statement 1 (see above) */ |
| 1661 | const char *zSql2, /* SQL statement 2 (see above) */ |
| 1662 | const char *zDb /* Database name (e.g. "main") */ |
| 1663 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1664 | int rc; /* Result codes from subroutines */ |
| 1665 | sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ |
| 1666 | char *zSql; /* Text of the SQL statement */ |
| 1667 | Index *pPrevIdx = 0; /* Previous index in the loop */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1668 | IndexSample *pSample; /* A slot in pIdx->aSample[] */ |
| 1669 | |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1670 | assert( db->lookaside.bDisable ); |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1671 | zSql = sqlite3MPrintf(db, zSql1, zDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1672 | if( !zSql ){ |
| 1673 | return SQLITE_NOMEM; |
| 1674 | } |
| 1675 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 1676 | sqlite3DbFree(db, zSql); |
| 1677 | if( rc ) return rc; |
| 1678 | |
| 1679 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1680 | int nIdxCol = 1; /* Number of columns in stat4 records */ |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1681 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1682 | char *zIndex; /* Index name */ |
| 1683 | Index *pIdx; /* Pointer to the index object */ |
| 1684 | int nSample; /* Number of samples */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1685 | int nByte; /* Bytes of space required */ |
| 1686 | int i; /* Bytes of space required */ |
| 1687 | tRowcnt *pSpace; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1688 | |
| 1689 | zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 1690 | if( zIndex==0 ) continue; |
| 1691 | nSample = sqlite3_column_int(pStmt, 1); |
drh | ce95d11 | 2013-11-02 19:34:38 +0000 | [diff] [blame] | 1692 | pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); |
dan | 86f69d9 | 2013-08-12 17:31:32 +0000 | [diff] [blame] | 1693 | assert( pIdx==0 || bStat3 || pIdx->nSample==0 ); |
| 1694 | /* Index.nSample is non-zero at this point if data has already been |
| 1695 | ** loaded from the stat4 table. In this case ignore stat3 data. */ |
| 1696 | if( pIdx==0 || pIdx->nSample ) continue; |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1697 | if( bStat3==0 ){ |
dan | 39caccf | 2014-07-01 11:54:02 +0000 | [diff] [blame] | 1698 | assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 ); |
| 1699 | if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ |
| 1700 | nIdxCol = pIdx->nKeyCol; |
| 1701 | }else{ |
| 1702 | nIdxCol = pIdx->nColumn; |
| 1703 | } |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1704 | } |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 1705 | pIdx->nSampleCol = nIdxCol; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1706 | nByte = sizeof(IndexSample) * nSample; |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1707 | nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample; |
dan | 39caccf | 2014-07-01 11:54:02 +0000 | [diff] [blame] | 1708 | nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1709 | |
| 1710 | pIdx->aSample = sqlite3DbMallocZero(db, nByte); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1711 | if( pIdx->aSample==0 ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1712 | sqlite3_finalize(pStmt); |
| 1713 | return SQLITE_NOMEM; |
| 1714 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1715 | pSpace = (tRowcnt*)&pIdx->aSample[nSample]; |
dan | 39caccf | 2014-07-01 11:54:02 +0000 | [diff] [blame] | 1716 | pIdx->aAvgEq = pSpace; pSpace += nIdxCol; |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1717 | for(i=0; i<nSample; i++){ |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1718 | pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol; |
| 1719 | pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol; |
| 1720 | pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1721 | } |
| 1722 | assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1723 | } |
drh | 8e3937f | 2011-08-18 13:45:23 +0000 | [diff] [blame] | 1724 | rc = sqlite3_finalize(pStmt); |
| 1725 | if( rc ) return rc; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1726 | |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1727 | zSql = sqlite3MPrintf(db, zSql2, zDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1728 | if( !zSql ){ |
| 1729 | return SQLITE_NOMEM; |
| 1730 | } |
| 1731 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 1732 | sqlite3DbFree(db, zSql); |
| 1733 | if( rc ) return rc; |
| 1734 | |
| 1735 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1736 | char *zIndex; /* Index name */ |
| 1737 | Index *pIdx; /* Pointer to the index object */ |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1738 | int nCol = 1; /* Number of columns in index */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1739 | |
| 1740 | zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 1741 | if( zIndex==0 ) continue; |
drh | ce95d11 | 2013-11-02 19:34:38 +0000 | [diff] [blame] | 1742 | pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1743 | if( pIdx==0 ) continue; |
dan | 86f69d9 | 2013-08-12 17:31:32 +0000 | [diff] [blame] | 1744 | /* This next condition is true if data has already been loaded from |
| 1745 | ** the sqlite_stat4 table. In this case ignore stat3 data. */ |
dan | 0adbed8 | 2013-08-15 19:56:32 +0000 | [diff] [blame] | 1746 | nCol = pIdx->nSampleCol; |
| 1747 | if( bStat3 && nCol>1 ) continue; |
| 1748 | if( pIdx!=pPrevIdx ){ |
| 1749 | initAvgEq(pPrevIdx); |
| 1750 | pPrevIdx = pIdx; |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1751 | } |
dan | c367d4c | 2013-08-16 14:09:43 +0000 | [diff] [blame] | 1752 | pSample = &pIdx->aSample[pIdx->nSample]; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1753 | decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0); |
| 1754 | decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0); |
| 1755 | decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1756 | |
dan | c367d4c | 2013-08-16 14:09:43 +0000 | [diff] [blame] | 1757 | /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer. |
| 1758 | ** This is in case the sample record is corrupted. In that case, the |
| 1759 | ** sqlite3VdbeRecordCompare() may read up to two varints past the |
| 1760 | ** end of the allocated buffer before it realizes it is dealing with |
| 1761 | ** a corrupt record. Adding the two 0x00 bytes prevents this from causing |
| 1762 | ** a buffer overread. */ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1763 | pSample->n = sqlite3_column_bytes(pStmt, 4); |
dan | c367d4c | 2013-08-16 14:09:43 +0000 | [diff] [blame] | 1764 | pSample->p = sqlite3DbMallocZero(db, pSample->n + 2); |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1765 | if( pSample->p==0 ){ |
| 1766 | sqlite3_finalize(pStmt); |
| 1767 | return SQLITE_NOMEM; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1768 | } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1769 | memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n); |
dan | c367d4c | 2013-08-16 14:09:43 +0000 | [diff] [blame] | 1770 | pIdx->nSample++; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1771 | } |
drh | 61b3440 | 2013-08-16 13:34:50 +0000 | [diff] [blame] | 1772 | rc = sqlite3_finalize(pStmt); |
| 1773 | if( rc==SQLITE_OK ) initAvgEq(pPrevIdx); |
| 1774 | return rc; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1775 | } |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1776 | |
| 1777 | /* |
| 1778 | ** Load content from the sqlite_stat4 and sqlite_stat3 tables into |
| 1779 | ** the Index.aSample[] arrays of all indices. |
| 1780 | */ |
| 1781 | static int loadStat4(sqlite3 *db, const char *zDb){ |
| 1782 | int rc = SQLITE_OK; /* Result codes from subroutines */ |
| 1783 | |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1784 | assert( db->lookaside.bDisable ); |
dan | 0106e37 | 2013-08-12 16:34:32 +0000 | [diff] [blame] | 1785 | if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){ |
| 1786 | rc = loadStatTbl(db, 0, |
| 1787 | "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", |
| 1788 | "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4", |
| 1789 | zDb |
| 1790 | ); |
| 1791 | } |
| 1792 | |
| 1793 | if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){ |
| 1794 | rc = loadStatTbl(db, 1, |
| 1795 | "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", |
| 1796 | "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3", |
| 1797 | zDb |
| 1798 | ); |
| 1799 | } |
| 1800 | |
| 1801 | return rc; |
| 1802 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1803 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1804 | |
| 1805 | /* |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 1806 | ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1807 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 1808 | ** arrays. The contents of sqlite_stat3/4 are used to populate the |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1809 | ** Index.aSample[] arrays. |
| 1810 | ** |
| 1811 | ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 1812 | ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined |
| 1813 | ** during compilation and the sqlite_stat3/4 table is present, no data is |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1814 | ** read from it. |
| 1815 | ** |
drh | 92707ac | 2013-08-17 18:57:15 +0000 | [diff] [blame] | 1816 | ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1817 | ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1818 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 1819 | ** table (if it is present) before returning. |
| 1820 | ** |
| 1821 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 1822 | ** This means if the caller does not care about other errors, the return |
| 1823 | ** code may be ignored. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1824 | */ |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1825 | int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1826 | analysisInfo sInfo; |
| 1827 | HashElem *i; |
| 1828 | char *zSql; |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1829 | int rc; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1830 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1831 | assert( iDb>=0 && iDb<db->nDb ); |
| 1832 | assert( db->aDb[iDb].pBt!=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1833 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1834 | /* Clear any prior statistics */ |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 1835 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1836 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1837 | Index *pIdx = sqliteHashData(i); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1838 | sqlite3DefaultRowEst(pIdx); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1839 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 1840 | sqlite3DeleteIndexSamples(db, pIdx); |
| 1841 | pIdx->aSample = 0; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1842 | #endif |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1845 | /* Check to make sure the sqlite_stat1 table exists */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1846 | sInfo.db = db; |
| 1847 | sInfo.zDatabase = db->aDb[iDb].zName; |
| 1848 | if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1849 | return SQLITE_ERROR; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1852 | /* Load new statistics out of the sqlite_stat1 table */ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1853 | zSql = sqlite3MPrintf(db, |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1854 | "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 1855 | if( zSql==0 ){ |
| 1856 | rc = SQLITE_NOMEM; |
| 1857 | }else{ |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 1858 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 1859 | sqlite3DbFree(db, zSql); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 1860 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1861 | |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 1862 | |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1863 | /* Load the statistics from the sqlite_stat4 table. */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1864 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d7d7147 | 2014-10-22 19:57:16 +0000 | [diff] [blame] | 1865 | if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1866 | db->lookaside.bDisable++; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 1867 | rc = loadStat4(db, sInfo.zDatabase); |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1868 | db->lookaside.bDisable--; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1869 | } |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1870 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 1871 | Index *pIdx = sqliteHashData(i); |
drh | 75b170b | 2014-10-04 00:07:44 +0000 | [diff] [blame] | 1872 | sqlite3_free(pIdx->aiRowEst); |
dan | 43085d7 | 2014-10-03 19:16:53 +0000 | [diff] [blame] | 1873 | pIdx->aiRowEst = 0; |
| 1874 | } |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 1875 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 1876 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1877 | if( rc==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1878 | sqlite3OomFault(db); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 1879 | } |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 1880 | return rc; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 1881 | } |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1882 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1883 | |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1884 | #endif /* SQLITE_OMIT_ANALYZE */ |