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