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