drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 July 8 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code associated with the ANALYZE command. |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 13 | */ |
| 14 | #ifndef SQLITE_OMIT_ANALYZE |
| 15 | #include "sqliteInt.h" |
| 16 | |
| 17 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 18 | ** This routine generates code that opens the sqlite_stat1 table for |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 19 | ** writing with cursor iStatCur. If the library was built with the |
| 20 | ** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is |
| 21 | ** opened for writing using cursor (iStatCur+1) |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 22 | ** |
| 23 | ** If the sqlite_stat1 tables does not previously exist, it is created. |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 24 | ** Similarly, if the sqlite_stat2 table does not exist and the library |
| 25 | ** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. |
| 26 | ** |
| 27 | ** Argument zWhere may be a pointer to a buffer containing a table name, |
| 28 | ** or it may be a NULL pointer. If it is not NULL, then all entries in |
| 29 | ** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated |
| 30 | ** with the named table are deleted. If zWhere==0, then code is generated |
| 31 | ** to delete all stat table entries. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 32 | */ |
| 33 | static void openStatTable( |
| 34 | Parse *pParse, /* Parsing context */ |
| 35 | int iDb, /* The database we are looking in */ |
| 36 | int iStatCur, /* Open the sqlite_stat1 table on this cursor */ |
| 37 | const char *zWhere /* Delete entries associated with this table */ |
| 38 | ){ |
dan | 558814f | 2010-06-02 05:53:53 +0000 | [diff] [blame] | 39 | static const struct { |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 40 | const char *zName; |
| 41 | const char *zCols; |
| 42 | } aTable[] = { |
| 43 | { "sqlite_stat1", "tbl,idx,stat" }, |
| 44 | #ifdef SQLITE_ENABLE_STAT2 |
| 45 | { "sqlite_stat2", "tbl,idx,sampleno,sample" }, |
| 46 | #endif |
| 47 | }; |
| 48 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 49 | int aRoot[] = {0, 0}; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 50 | u8 aCreateTbl[] = {0, 0}; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 51 | |
| 52 | int i; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 53 | sqlite3 *db = pParse->db; |
| 54 | Db *pDb; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 55 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 56 | if( v==0 ) return; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 57 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 58 | assert( sqlite3VdbeDb(v)==db ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 59 | pDb = &db->aDb[iDb]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 60 | |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 61 | for(i=0; i<ArraySize(aTable); i++){ |
| 62 | const char *zTab = aTable[i].zName; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 63 | Table *pStat; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 64 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 65 | /* The sqlite_stat[12] table does not exist. Create it. Note that a |
| 66 | ** side-effect of the CREATE TABLE statement is to leave the rootpage |
| 67 | ** of the new table in register pParse->regRoot. This is important |
| 68 | ** because the OpenWrite opcode below will be needing it. */ |
| 69 | sqlite3NestedParse(pParse, |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 70 | "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 71 | ); |
| 72 | aRoot[i] = pParse->regRoot; |
| 73 | aCreateTbl[i] = 1; |
| 74 | }else{ |
| 75 | /* The table already exists. If zWhere is not NULL, delete all entries |
| 76 | ** associated with the table zWhere. If zWhere is NULL, delete the |
| 77 | ** entire contents of the table. */ |
| 78 | aRoot[i] = pStat->tnum; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 79 | sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 80 | if( zWhere ){ |
| 81 | sqlite3NestedParse(pParse, |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 82 | "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 83 | ); |
| 84 | }else{ |
| 85 | /* The sqlite_stat[12] table already exists. Delete all rows. */ |
| 86 | sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
| 87 | } |
| 88 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 91 | /* Open the sqlite_stat[12] tables for writing. */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 92 | for(i=0; i<ArraySize(aTable); i++){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 93 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); |
| 94 | sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); |
| 95 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 96 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | ** Generate code to do an analysis of all indices associated with |
| 101 | ** a single table. |
| 102 | */ |
| 103 | static void analyzeOneTable( |
| 104 | Parse *pParse, /* Parser context */ |
| 105 | Table *pTab, /* Table whose indices are to be analyzed */ |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 106 | int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 107 | int iMem /* Available memory locations begin here */ |
| 108 | ){ |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 109 | sqlite3 *db = pParse->db; /* Database handle */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 110 | Index *pIdx; /* An index to being analyzed */ |
| 111 | int iIdxCur; /* Cursor open on index being analyzed */ |
| 112 | Vdbe *v; /* The virtual machine being built up */ |
| 113 | int i; /* Loop counter */ |
| 114 | int topOfLoop; /* The top of the loop */ |
| 115 | int endOfLoop; /* The end of the loop */ |
drh | 6ac78a0 | 2010-09-28 14:26:36 +0000 | [diff] [blame] | 116 | int addr = 0; /* The address of an instruction */ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 117 | int jZeroRows = 0; /* Jump from here if number of rows is zero */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 118 | int iDb; /* Index of database containing pTab */ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 119 | int regTabname = iMem++; /* Register containing table name */ |
| 120 | int regIdxname = iMem++; /* Register containing index name */ |
| 121 | int regSampleno = iMem++; /* Register containing next sample number */ |
| 122 | int regCol = iMem++; /* Content of a column analyzed table */ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 123 | int regRec = iMem++; /* Register holding completed record */ |
| 124 | int regTemp = iMem++; /* Temporary use register */ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 125 | int regRowid = iMem++; /* Rowid for the inserted record */ |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 126 | |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 127 | #ifdef SQLITE_ENABLE_STAT2 |
| 128 | int regTemp2 = iMem++; /* Temporary use register */ |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 129 | int regSamplerecno = iMem++; /* Index of next sample to record */ |
| 130 | int regRecno = iMem++; /* Current sample index */ |
| 131 | int regLast = iMem++; /* Index of last sample to record */ |
| 132 | int regFirst = iMem++; /* Index of first sample to record */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 133 | #endif |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 134 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 135 | v = sqlite3GetVdbe(pParse); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 136 | if( v==0 || NEVER(pTab==0) ){ |
| 137 | return; |
| 138 | } |
drh | f39d29c | 2010-09-28 17:34:46 +0000 | [diff] [blame] | 139 | if( pTab->tnum==0 ){ |
| 140 | /* Do not gather statistics on views or virtual tables */ |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 144 | /* Do not gather statistics on system tables */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 145 | return; |
| 146 | } |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 147 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 148 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 149 | assert( iDb>=0 ); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 150 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 151 | if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 152 | db->aDb[iDb].zName ) ){ |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | #endif |
| 156 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 157 | /* Establish a read-lock on the table at the shared-cache level. */ |
| 158 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 159 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 160 | iIdxCur = pParse->nTab++; |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 161 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 162 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 163 | int nCol = pIdx->nColumn; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 164 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 165 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 166 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| 167 | pParse->nMem = iMem+1+(nCol*2); |
| 168 | } |
| 169 | |
| 170 | /* Open a cursor to the index to be analyzed. */ |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 171 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 172 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 173 | (char *)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 174 | VdbeComment((v, "%s", pIdx->zName)); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 175 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 176 | /* Populate the register containing the index name. */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 177 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 178 | |
| 179 | #ifdef SQLITE_ENABLE_STAT2 |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 180 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 181 | /* If this iteration of the loop is generating code to analyze the |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 182 | ** first index in the pTab->pIndex list, then register regLast has |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 183 | ** not been populated. In this case populate it now. */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 184 | if( pTab->pIndex==pIdx ){ |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 185 | sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno); |
| 186 | sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp); |
| 187 | sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2); |
| 188 | |
| 189 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast); |
| 190 | sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst); |
| 191 | addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast); |
| 192 | sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst); |
| 193 | sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast); |
| 194 | sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2); |
| 195 | sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast); |
| 196 | sqlite3VdbeJumpHere(v, addr); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 197 | } |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 198 | |
| 199 | /* Zero the regSampleno and regRecno registers. */ |
| 200 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno); |
| 201 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno); |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 202 | sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 203 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 204 | |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 205 | /* The block of memory cells initialized here is used as follows. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 206 | ** |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 207 | ** iMem: |
| 208 | ** The total number of rows in the table. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 209 | ** |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 210 | ** iMem+1 .. iMem+nCol: |
| 211 | ** Number of distinct entries in index considering the |
| 212 | ** left-most N columns only, where N is between 1 and nCol, |
| 213 | ** inclusive. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 214 | ** |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 215 | ** iMem+nCol+1 .. Mem+2*nCol: |
| 216 | ** Previous value of indexed columns, from left to right. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 217 | ** |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 218 | ** Cells iMem through iMem+nCol are initialized to 0. The others are |
| 219 | ** initialized to contain an SQL NULL. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 220 | */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 221 | for(i=0; i<=nCol; i++){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 222 | sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 223 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 224 | for(i=0; i<nCol; i++){ |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 225 | sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 226 | } |
| 227 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 228 | /* Start the analysis loop. This loop runs through all the entries in |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 229 | ** the index b-tree. */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 230 | endOfLoop = sqlite3VdbeMakeLabel(v); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 231 | sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 232 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 233 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 234 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 235 | for(i=0; i<nCol; i++){ |
drh | 4856698 | 2011-01-04 19:01:26 +0000 | [diff] [blame] | 236 | CollSeq *pColl; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 237 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 238 | if( i==0 ){ |
drh | 8e93b10 | 2011-01-04 17:57:53 +0000 | [diff] [blame] | 239 | #ifdef SQLITE_ENABLE_STAT2 |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 240 | /* Check if the record that cursor iIdxCur points to contains a |
| 241 | ** value that should be stored in the sqlite_stat2 table. If so, |
| 242 | ** store it. */ |
| 243 | int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno); |
| 244 | assert( regTabname+1==regIdxname |
| 245 | && regTabname+2==regSampleno |
| 246 | && regTabname+3==regCol |
| 247 | ); |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 248 | sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 249 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0); |
| 250 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid); |
| 251 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid); |
| 252 | |
| 253 | /* Calculate new values for regSamplerecno and regSampleno. |
| 254 | ** |
| 255 | ** sampleno = sampleno + 1 |
| 256 | ** samplerecno = samplerecno+(remaining records)/(remaining samples) |
| 257 | */ |
| 258 | sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1); |
dan | 68c4dbb | 2009-08-20 09:11:06 +0000 | [diff] [blame] | 259 | sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 260 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 261 | sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2); |
| 262 | sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2); |
| 263 | sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp); |
| 264 | sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno); |
| 265 | |
| 266 | sqlite3VdbeJumpHere(v, ne); |
| 267 | sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
drh | 8e93b10 | 2011-01-04 17:57:53 +0000 | [diff] [blame] | 270 | /* Always record the very first row */ |
| 271 | sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 272 | } |
drh | 4856698 | 2011-01-04 19:01:26 +0000 | [diff] [blame] | 273 | assert( pIdx->azColl!=0 ); |
| 274 | assert( pIdx->azColl[i]!=0 ); |
| 275 | pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 276 | sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 277 | (char*)pColl, P4_COLLSEQ); |
drh | 8e93b10 | 2011-01-04 17:57:53 +0000 | [diff] [blame] | 278 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 279 | } |
dan | 0f9a34e | 2009-08-19 16:34:31 +0000 | [diff] [blame] | 280 | if( db->mallocFailed ){ |
| 281 | /* If a malloc failure has occurred, then the result of the expression |
| 282 | ** passed as the second argument to the call to sqlite3VdbeJumpHere() |
| 283 | ** below may be negative. Which causes an assert() to fail (or an |
| 284 | ** out-of-bounds write if SQLITE_DEBUG is not defined). */ |
| 285 | return; |
| 286 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 287 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 288 | for(i=0; i<nCol; i++){ |
drh | 906b662 | 2011-01-24 19:14:06 +0000 | [diff] [blame] | 289 | int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
drh | 8e93b10 | 2011-01-04 17:57:53 +0000 | [diff] [blame] | 290 | if( i==0 ){ |
drh | 906b662 | 2011-01-24 19:14:06 +0000 | [diff] [blame] | 291 | sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */ |
drh | 8e93b10 | 2011-01-04 17:57:53 +0000 | [diff] [blame] | 292 | } |
drh | 906b662 | 2011-01-24 19:14:06 +0000 | [diff] [blame] | 293 | sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 294 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
drh | b1fdb2a | 2008-01-05 04:06:03 +0000 | [diff] [blame] | 295 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 296 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 297 | |
| 298 | /* End of the analysis loop. */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 299 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 300 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); |
| 301 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 302 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 303 | /* Store the results in sqlite_stat1. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 304 | ** |
drh | 4c10383 | 2007-06-20 13:37:31 +0000 | [diff] [blame] | 305 | ** The result is a single row of the sqlite_stat1 table. The first |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 306 | ** two columns are the names of the table and index. The third column |
| 307 | ** is a string composed of a list of integer statistics about the |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 308 | ** index. The first integer in the list is the total number of entries |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 309 | ** in the index. There is one additional integer in the list for each |
| 310 | ** column of the table. This additional integer is a guess of how many |
| 311 | ** rows of the table the index will select. If D is the count of distinct |
| 312 | ** values and K is the total number of rows, then the integer is computed |
| 313 | ** as: |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 314 | ** |
| 315 | ** I = (K+D-1)/D |
| 316 | ** |
| 317 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 318 | ** If K>0 then it is always the case the D>0 so division by zero |
| 319 | ** is never possible. |
| 320 | */ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 321 | sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 322 | if( jZeroRows==0 ){ |
| 323 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 324 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 325 | for(i=0; i<nCol; i++){ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 326 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 327 | sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 328 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 329 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 330 | sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); |
| 331 | sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 332 | sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 333 | } |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 334 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 335 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 336 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 337 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 341 | ** containing NULL as the index name and the row count as the content. |
| 342 | */ |
| 343 | if( pTab->pIndex==0 ){ |
| 344 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 345 | VdbeComment((v, "%s", pTab->zName)); |
| 346 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 347 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 348 | }else{ |
| 349 | assert( jZeroRows>0 ); |
| 350 | addr = sqlite3VdbeAddOp0(v, OP_Goto); |
| 351 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 352 | } |
| 353 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 354 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 355 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 356 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 357 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 358 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 359 | if( jZeroRows ){ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 360 | sqlite3VdbeJumpHere(v, addr); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | /* |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 365 | ** Generate code that will cause the most recent index analysis to |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 366 | ** be loaded into internal hash tables where is can be used. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 367 | */ |
| 368 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 369 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 370 | if( v ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 371 | sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 372 | } |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 376 | ** Generate code that will do an analysis of an entire database |
| 377 | */ |
| 378 | static void analyzeDatabase(Parse *pParse, int iDb){ |
| 379 | sqlite3 *db = pParse->db; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 380 | Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 381 | HashElem *k; |
| 382 | int iStatCur; |
| 383 | int iMem; |
| 384 | |
| 385 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 386 | iStatCur = pParse->nTab; |
| 387 | pParse->nTab += 2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 388 | openStatTable(pParse, iDb, iStatCur, 0); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 389 | iMem = pParse->nMem+1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 390 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 391 | Table *pTab = (Table*)sqliteHashData(k); |
| 392 | analyzeOneTable(pParse, pTab, iStatCur, iMem); |
| 393 | } |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 394 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | ** Generate code that will do an analysis of a single table in |
| 399 | ** a database. |
| 400 | */ |
| 401 | static void analyzeTable(Parse *pParse, Table *pTab){ |
| 402 | int iDb; |
| 403 | int iStatCur; |
| 404 | |
| 405 | assert( pTab!=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 406 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 407 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 408 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 409 | iStatCur = pParse->nTab; |
| 410 | pParse->nTab += 2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 411 | openStatTable(pParse, iDb, iStatCur, pTab->zName); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 412 | analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 413 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | ** Generate code for the ANALYZE command. The parser calls this routine |
| 418 | ** when it recognizes an ANALYZE command. |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 419 | ** |
| 420 | ** ANALYZE -- 1 |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 421 | ** ANALYZE <database> -- 2 |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 422 | ** ANALYZE ?<database>.?<tablename> -- 3 |
| 423 | ** |
| 424 | ** Form 1 causes all indices in all attached databases to be analyzed. |
| 425 | ** Form 2 analyzes all indices the single database named. |
| 426 | ** Form 3 analyzes all indices associated with the named table. |
| 427 | */ |
| 428 | void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 429 | sqlite3 *db = pParse->db; |
| 430 | int iDb; |
| 431 | int i; |
| 432 | char *z, *zDb; |
| 433 | Table *pTab; |
| 434 | Token *pTableName; |
| 435 | |
| 436 | /* Read the database schema. If an error occurs, leave an error message |
| 437 | ** and code in pParse and return NULL. */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 438 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 439 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 440 | return; |
| 441 | } |
| 442 | |
drh | 05800a1 | 2009-04-16 17:45:47 +0000 | [diff] [blame] | 443 | assert( pName2!=0 || pName1==0 ); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 444 | if( pName1==0 ){ |
| 445 | /* Form 1: Analyze everything */ |
| 446 | for(i=0; i<db->nDb; i++){ |
| 447 | if( i==1 ) continue; /* Do not analyze the TEMP database */ |
| 448 | analyzeDatabase(pParse, i); |
| 449 | } |
drh | 05800a1 | 2009-04-16 17:45:47 +0000 | [diff] [blame] | 450 | }else if( pName2->n==0 ){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 451 | /* Form 2: Analyze the database or table named */ |
| 452 | iDb = sqlite3FindDb(db, pName1); |
| 453 | if( iDb>=0 ){ |
| 454 | analyzeDatabase(pParse, iDb); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 455 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 456 | z = sqlite3NameFromToken(db, pName1); |
danielk1977 | b8b4bfa | 2007-11-15 13:10:22 +0000 | [diff] [blame] | 457 | if( z ){ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 458 | pTab = sqlite3LocateTable(pParse, 0, z, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 459 | sqlite3DbFree(db, z); |
danielk1977 | b8b4bfa | 2007-11-15 13:10:22 +0000 | [diff] [blame] | 460 | if( pTab ){ |
| 461 | analyzeTable(pParse, pTab); |
| 462 | } |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 463 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 464 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 465 | }else{ |
| 466 | /* Form 3: Analyze the fully qualified table name */ |
| 467 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); |
| 468 | if( iDb>=0 ){ |
| 469 | zDb = db->aDb[iDb].zName; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 470 | z = sqlite3NameFromToken(db, pTableName); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 471 | if( z ){ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 472 | pTab = sqlite3LocateTable(pParse, 0, z, zDb); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 473 | sqlite3DbFree(db, z); |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 474 | if( pTab ){ |
| 475 | analyzeTable(pParse, pTab); |
| 476 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 480 | } |
| 481 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 482 | /* |
| 483 | ** Used to pass information from the analyzer reader through to the |
| 484 | ** callback routine. |
| 485 | */ |
| 486 | typedef struct analysisInfo analysisInfo; |
| 487 | struct analysisInfo { |
| 488 | sqlite3 *db; |
| 489 | const char *zDatabase; |
| 490 | }; |
| 491 | |
| 492 | /* |
| 493 | ** This callback is invoked once for each index when reading the |
| 494 | ** sqlite_stat1 table. |
| 495 | ** |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 496 | ** argv[0] = name of the table |
| 497 | ** argv[1] = name of the index (might be NULL) |
| 498 | ** argv[2] = results of analysis - on integer for each column |
| 499 | ** |
| 500 | ** Entries for which argv[1]==NULL simply record the number of rows in |
| 501 | ** the table. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 502 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 503 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 504 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 505 | Index *pIndex; |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 506 | Table *pTable; |
| 507 | int i, c, n; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 508 | unsigned int v; |
| 509 | const char *z; |
| 510 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 511 | assert( argc==3 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 512 | UNUSED_PARAMETER2(NotUsed, argc); |
| 513 | |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 514 | if( argv==0 || argv[0]==0 || argv[2]==0 ){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 515 | return 0; |
| 516 | } |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 517 | pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); |
| 518 | if( pTable==0 ){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 521 | if( argv[1] ){ |
| 522 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 523 | }else{ |
| 524 | pIndex = 0; |
| 525 | } |
| 526 | n = pIndex ? pIndex->nColumn : 0; |
| 527 | z = argv[2]; |
| 528 | for(i=0; *z && i<=n; i++){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 529 | v = 0; |
| 530 | while( (c=z[0])>='0' && c<='9' ){ |
| 531 | v = v*10 + c - '0'; |
| 532 | z++; |
| 533 | } |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 534 | if( i==0 ) pTable->nRowEst = v; |
| 535 | if( pIndex==0 ) break; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 536 | pIndex->aiRowEst[i] = v; |
| 537 | if( *z==' ' ) z++; |
| 538 | } |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | /* |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 543 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 544 | ** and its contents. |
| 545 | */ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 546 | void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 547 | #ifdef SQLITE_ENABLE_STAT2 |
| 548 | if( pIdx->aSample ){ |
| 549 | int j; |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 550 | for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 551 | IndexSample *p = &pIdx->aSample[j]; |
| 552 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 553 | sqlite3DbFree(db, p->u.z); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 556 | sqlite3DbFree(db, pIdx->aSample); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 557 | } |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 558 | #else |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 559 | UNUSED_PARAMETER(db); |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 560 | UNUSED_PARAMETER(pIdx); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 561 | #endif |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The |
| 566 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
| 567 | ** arrays. The contents of sqlite_stat2 are used to populate the |
| 568 | ** Index.aSample[] arrays. |
| 569 | ** |
| 570 | ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR |
| 571 | ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined |
| 572 | ** during compilation and the sqlite_stat2 table is present, no data is |
| 573 | ** read from it. |
| 574 | ** |
| 575 | ** If SQLITE_ENABLE_STAT2 was defined during compilation and the |
| 576 | ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is |
| 577 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 578 | ** table (if it is present) before returning. |
| 579 | ** |
| 580 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 581 | ** This means if the caller does not care about other errors, the return |
| 582 | ** code may be ignored. |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 583 | */ |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 584 | int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 585 | analysisInfo sInfo; |
| 586 | HashElem *i; |
| 587 | char *zSql; |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 588 | int rc; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 589 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 590 | assert( iDb>=0 && iDb<db->nDb ); |
| 591 | assert( db->aDb[iDb].pBt!=0 ); |
| 592 | assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 593 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 594 | /* Clear any prior statistics */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 595 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 596 | Index *pIdx = sqliteHashData(i); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 597 | sqlite3DefaultRowEst(pIdx); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 598 | sqlite3DeleteIndexSamples(db, pIdx); |
| 599 | pIdx->aSample = 0; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 600 | } |
| 601 | |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 602 | /* Check to make sure the sqlite_stat1 table exists */ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 603 | sInfo.db = db; |
| 604 | sInfo.zDatabase = db->aDb[iDb].zName; |
| 605 | if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 606 | return SQLITE_ERROR; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 607 | } |
| 608 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 609 | /* Load new statistics out of the sqlite_stat1 table */ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 610 | zSql = sqlite3MPrintf(db, |
drh | 1556405 | 2010-09-25 22:32:56 +0000 | [diff] [blame] | 611 | "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 612 | if( zSql==0 ){ |
| 613 | rc = SQLITE_NOMEM; |
| 614 | }else{ |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 615 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 616 | sqlite3DbFree(db, zSql); |
drh | f16ce3b | 2009-02-13 16:59:53 +0000 | [diff] [blame] | 617 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 618 | |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 619 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 620 | /* Load the statistics from the sqlite_stat2 table. */ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 621 | #ifdef SQLITE_ENABLE_STAT2 |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 622 | if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){ |
| 623 | rc = SQLITE_ERROR; |
| 624 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 625 | if( rc==SQLITE_OK ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 626 | sqlite3_stmt *pStmt = 0; |
| 627 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 628 | zSql = sqlite3MPrintf(db, |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 629 | "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 630 | if( !zSql ){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 631 | rc = SQLITE_NOMEM; |
| 632 | }else{ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 633 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 634 | sqlite3DbFree(db, zSql); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 635 | } |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 636 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 637 | if( rc==SQLITE_OK ){ |
| 638 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 65a0ce1 | 2011-01-04 20:06:33 +0000 | [diff] [blame] | 639 | char *zIndex; /* Index name */ |
| 640 | Index *pIdx; /* Pointer to the index object */ |
| 641 | |
| 642 | zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 643 | pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 644 | if( pIdx ){ |
| 645 | int iSample = sqlite3_column_int(pStmt, 1); |
| 646 | if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){ |
| 647 | int eType = sqlite3_column_type(pStmt, 2); |
| 648 | |
| 649 | if( pIdx->aSample==0 ){ |
dan | a898aac | 2009-08-19 09:09:38 +0000 | [diff] [blame] | 650 | static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 651 | pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 652 | if( pIdx->aSample==0 ){ |
dan | 85c165c | 2009-08-19 14:34:54 +0000 | [diff] [blame] | 653 | db->mallocFailed = 1; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 656 | memset(pIdx->aSample, 0, sz); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 657 | } |
| 658 | |
drh | 9e1fade | 2009-08-20 23:05:31 +0000 | [diff] [blame] | 659 | assert( pIdx->aSample ); |
| 660 | { |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 661 | IndexSample *pSample = &pIdx->aSample[iSample]; |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 662 | pSample->eType = (u8)eType; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 663 | if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 664 | pSample->u.r = sqlite3_column_double(pStmt, 2); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 665 | }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 666 | const char *z = (const char *)( |
| 667 | (eType==SQLITE_BLOB) ? |
| 668 | sqlite3_column_blob(pStmt, 2): |
| 669 | sqlite3_column_text(pStmt, 2) |
| 670 | ); |
| 671 | int n = sqlite3_column_bytes(pStmt, 2); |
| 672 | if( n>24 ){ |
| 673 | n = 24; |
| 674 | } |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 675 | pSample->nByte = (u8)n; |
shaneh | 1141ae2 | 2010-03-26 01:54:33 +0000 | [diff] [blame] | 676 | if( n < 1){ |
| 677 | pSample->u.z = 0; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 678 | }else{ |
drh | 174b9a1 | 2010-07-26 11:07:20 +0000 | [diff] [blame] | 679 | pSample->u.z = sqlite3DbStrNDup(0, z, n); |
| 680 | if( pSample->u.z==0 ){ |
shaneh | 1141ae2 | 2010-03-26 01:54:33 +0000 | [diff] [blame] | 681 | db->mallocFailed = 1; |
| 682 | break; |
| 683 | } |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 684 | } |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | rc = sqlite3_finalize(pStmt); |
| 691 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 692 | } |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 693 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 694 | |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 695 | if( rc==SQLITE_NOMEM ){ |
| 696 | db->mallocFailed = 1; |
| 697 | } |
drh | cf1be45 | 2007-05-12 12:08:51 +0000 | [diff] [blame] | 698 | return rc; |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 699 | } |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 700 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 701 | |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 702 | #endif /* SQLITE_OMIT_ANALYZE */ |