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. |
| 13 | ** |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame^] | 14 | ** @(#) $Id: analyze.c,v 1.13 2006/01/07 13:21:04 danielk1977 Exp $ |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 15 | */ |
| 16 | #ifndef SQLITE_OMIT_ANALYZE |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 20 | ** This routine generates code that opens the sqlite_stat1 table on cursor |
| 21 | ** iStatCur. |
| 22 | ** |
| 23 | ** If the sqlite_stat1 tables does not previously exist, it is created. |
| 24 | ** If it does previously exist, all entires associated with table zWhere |
| 25 | ** are removed. If zWhere==0 then all entries are removed. |
| 26 | */ |
| 27 | static void openStatTable( |
| 28 | Parse *pParse, /* Parsing context */ |
| 29 | int iDb, /* The database we are looking in */ |
| 30 | int iStatCur, /* Open the sqlite_stat1 table on this cursor */ |
| 31 | const char *zWhere /* Delete entries associated with this table */ |
| 32 | ){ |
| 33 | sqlite3 *db = pParse->db; |
| 34 | Db *pDb; |
| 35 | int iRootPage; |
| 36 | Table *pStat; |
| 37 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 38 | |
| 39 | pDb = &db->aDb[iDb]; |
| 40 | if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){ |
| 41 | /* The sqlite_stat1 tables does not exist. Create it. |
| 42 | ** Note that a side-effect of the CREATE TABLE statement is to leave |
| 43 | ** the rootpage of the new table on the top of the stack. This is |
| 44 | ** important because the OpenWrite opcode below will be needing it. */ |
| 45 | sqlite3NestedParse(pParse, |
| 46 | "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)", |
| 47 | pDb->zName |
| 48 | ); |
| 49 | iRootPage = 0; /* Cause rootpage to be taken from top of stack */ |
| 50 | }else if( zWhere ){ |
| 51 | /* The sqlite_stat1 table exists. Delete all entries associated with |
| 52 | ** the table zWhere. */ |
| 53 | sqlite3NestedParse(pParse, |
| 54 | "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", |
| 55 | pDb->zName, zWhere |
| 56 | ); |
| 57 | iRootPage = pStat->tnum; |
| 58 | }else{ |
| 59 | /* The sqlite_stat1 table already exists. Delete all rows. */ |
| 60 | iRootPage = pStat->tnum; |
| 61 | sqlite3VdbeAddOp(v, OP_Clear, pStat->tnum, iDb); |
| 62 | } |
| 63 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame^] | 64 | /* Open the sqlite_stat1 table for writing. Unless it was created |
| 65 | ** by this vdbe program, lock it for writing at the shared-cache level. |
| 66 | ** If this vdbe did create the sqlite_stat1 table, then it must have |
| 67 | ** already obtained a schema-lock, making the write-lock redundant. |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 68 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame^] | 69 | if( iRootPage>0 ){ |
| 70 | sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1"); |
| 71 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 72 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 73 | sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 74 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iStatCur, 3); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | ** Generate code to do an analysis of all indices associated with |
| 79 | ** a single table. |
| 80 | */ |
| 81 | static void analyzeOneTable( |
| 82 | Parse *pParse, /* Parser context */ |
| 83 | Table *pTab, /* Table whose indices are to be analyzed */ |
| 84 | int iStatCur, /* Cursor that writes to the sqlite_stat1 table */ |
| 85 | int iMem /* Available memory locations begin here */ |
| 86 | ){ |
| 87 | Index *pIdx; /* An index to being analyzed */ |
| 88 | int iIdxCur; /* Cursor number for index being analyzed */ |
| 89 | int nCol; /* Number of columns in the index */ |
| 90 | Vdbe *v; /* The virtual machine being built up */ |
| 91 | int i; /* Loop counter */ |
| 92 | int topOfLoop; /* The top of the loop */ |
| 93 | int endOfLoop; /* The end of the loop */ |
| 94 | int addr; /* The address of an instruction */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 95 | int iDb; /* Index of database containing pTab */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 96 | |
| 97 | v = sqlite3GetVdbe(pParse); |
drh | 0c35667 | 2005-09-10 22:40:53 +0000 | [diff] [blame] | 98 | if( pTab==0 || pTab->pIndex==0 ){ |
| 99 | /* Do no analysis for tables that have no indices */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 102 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 103 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 104 | assert( iDb>=0 ); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 105 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 106 | if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 107 | pParse->db->aDb[iDb].zName ) ){ |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | #endif |
| 111 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame^] | 112 | /* Establish a read-lock on the table at the shared-cache level. */ |
| 113 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 114 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 115 | iIdxCur = pParse->nTab; |
| 116 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 117 | /* Open a cursor to the index to be analyzed |
| 118 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 119 | assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) ); |
| 120 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 121 | VdbeComment((v, "# %s", pIdx->zName)); |
| 122 | sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, |
| 123 | (char*)&pIdx->keyInfo, P3_KEYINFO); |
| 124 | nCol = pIdx->nColumn; |
| 125 | if( iMem+nCol*2>=pParse->nMem ){ |
| 126 | pParse->nMem = iMem+nCol*2+1; |
| 127 | } |
| 128 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, nCol+1); |
| 129 | |
| 130 | /* Memory cells are used as follows: |
| 131 | ** |
| 132 | ** mem[iMem]: The total number of rows in the table. |
| 133 | ** mem[iMem+1]: Number of distinct values in column 1 |
| 134 | ** ... |
| 135 | ** mem[iMem+nCol]: Number of distinct values in column N |
| 136 | ** mem[iMem+nCol+1] Last observed value of column 1 |
| 137 | ** ... |
| 138 | ** mem[iMem+nCol+nCol]: Last observed value of column N |
| 139 | ** |
| 140 | ** Cells iMem through iMem+nCol are initialized to 0. The others |
| 141 | ** are initialized to NULL. |
| 142 | */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 143 | for(i=0; i<=nCol; i++){ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 144 | sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 145 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 146 | for(i=0; i<nCol; i++){ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 147 | sqlite3VdbeAddOp(v, OP_MemNull, iMem+nCol+i+1, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* Do the analysis. |
| 151 | */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 152 | endOfLoop = sqlite3VdbeMakeLabel(v); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 153 | sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, endOfLoop); |
| 154 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 155 | sqlite3VdbeAddOp(v, OP_MemIncr, iMem, 0); |
| 156 | for(i=0; i<nCol; i++){ |
| 157 | sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i); |
| 158 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem+nCol+i+1, 0); |
| 159 | sqlite3VdbeAddOp(v, OP_Ne, 0x100, 0); |
| 160 | } |
| 161 | sqlite3VdbeAddOp(v, OP_Goto, 0, endOfLoop); |
| 162 | for(i=0; i<nCol; i++){ |
| 163 | addr = sqlite3VdbeAddOp(v, OP_MemIncr, iMem+i+1, 0); |
| 164 | sqlite3VdbeChangeP2(v, topOfLoop + 3*i + 3, addr); |
| 165 | sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i); |
| 166 | sqlite3VdbeAddOp(v, OP_MemStore, iMem+nCol+i+1, 1); |
| 167 | } |
| 168 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 169 | sqlite3VdbeAddOp(v, OP_Next, iIdxCur, topOfLoop); |
| 170 | sqlite3VdbeAddOp(v, OP_Close, iIdxCur, 0); |
| 171 | |
| 172 | /* Store the results. |
| 173 | ** |
| 174 | ** The result is a single row of the sqlite_stmt1 table. The first |
| 175 | ** two columns are the names of the table and index. The third column |
| 176 | ** is a string composed of a list of integer statistics about the |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 177 | ** index. The first integer in the list is the total number of entires |
| 178 | ** in the index. There is one additional integer in the list for each |
| 179 | ** column of the table. This additional integer is a guess of how many |
| 180 | ** rows of the table the index will select. If D is the count of distinct |
| 181 | ** values and K is the total number of rows, then the integer is computed |
| 182 | ** as: |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 183 | ** |
| 184 | ** I = (K+D-1)/D |
| 185 | ** |
| 186 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 187 | ** If K>0 then it is always the case the D>0 so division by zero |
| 188 | ** is never possible. |
| 189 | */ |
| 190 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); |
| 191 | addr = sqlite3VdbeAddOp(v, OP_IfNot, 0, 0); |
| 192 | sqlite3VdbeAddOp(v, OP_NewRowid, iStatCur, 0); |
| 193 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); |
| 194 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 195 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); |
| 196 | sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 197 | for(i=0; i<nCol; i++){ |
| 198 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); |
| 199 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0); |
| 200 | sqlite3VdbeAddOp(v, OP_Add, 0, 0); |
| 201 | sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); |
| 202 | sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0); |
| 203 | sqlite3VdbeAddOp(v, OP_Divide, 0, 0); |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 204 | sqlite3VdbeAddOp(v, OP_ToInt, 0, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 205 | if( i==nCol-1 ){ |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 206 | sqlite3VdbeAddOp(v, OP_Concat, nCol*2-1, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 207 | }else{ |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 208 | sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 211 | sqlite3VdbeOp3(v, OP_MakeRecord, 3, 0, "aaa", 0); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 212 | sqlite3VdbeAddOp(v, OP_Insert, iStatCur, 0); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 213 | sqlite3VdbeJumpHere(v, addr); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 218 | ** Generate code that will cause the most recent index analysis to |
| 219 | ** be laoded into internal hash tables where is can be used. |
| 220 | */ |
| 221 | static void loadAnalysis(Parse *pParse, int iDb){ |
| 222 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 223 | sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0); |
| 224 | } |
| 225 | |
| 226 | /* |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 227 | ** Generate code that will do an analysis of an entire database |
| 228 | */ |
| 229 | static void analyzeDatabase(Parse *pParse, int iDb){ |
| 230 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 231 | DbSchema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 232 | HashElem *k; |
| 233 | int iStatCur; |
| 234 | int iMem; |
| 235 | |
| 236 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 237 | iStatCur = pParse->nTab++; |
| 238 | openStatTable(pParse, iDb, iStatCur, 0); |
| 239 | iMem = pParse->nMem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 240 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 241 | Table *pTab = (Table*)sqliteHashData(k); |
| 242 | analyzeOneTable(pParse, pTab, iStatCur, iMem); |
| 243 | } |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 244 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | ** Generate code that will do an analysis of a single table in |
| 249 | ** a database. |
| 250 | */ |
| 251 | static void analyzeTable(Parse *pParse, Table *pTab){ |
| 252 | int iDb; |
| 253 | int iStatCur; |
| 254 | |
| 255 | assert( pTab!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 256 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 257 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 258 | iStatCur = pParse->nTab++; |
| 259 | openStatTable(pParse, iDb, iStatCur, pTab->zName); |
| 260 | analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 261 | loadAnalysis(pParse, iDb); |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | ** Generate code for the ANALYZE command. The parser calls this routine |
| 266 | ** when it recognizes an ANALYZE command. |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 267 | ** |
| 268 | ** ANALYZE -- 1 |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 269 | ** ANALYZE <database> -- 2 |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 270 | ** ANALYZE ?<database>.?<tablename> -- 3 |
| 271 | ** |
| 272 | ** Form 1 causes all indices in all attached databases to be analyzed. |
| 273 | ** Form 2 analyzes all indices the single database named. |
| 274 | ** Form 3 analyzes all indices associated with the named table. |
| 275 | */ |
| 276 | void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 277 | sqlite3 *db = pParse->db; |
| 278 | int iDb; |
| 279 | int i; |
| 280 | char *z, *zDb; |
| 281 | Table *pTab; |
| 282 | Token *pTableName; |
| 283 | |
| 284 | /* Read the database schema. If an error occurs, leave an error message |
| 285 | ** and code in pParse and return NULL. */ |
| 286 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | if( pName1==0 ){ |
| 291 | /* Form 1: Analyze everything */ |
| 292 | for(i=0; i<db->nDb; i++){ |
| 293 | if( i==1 ) continue; /* Do not analyze the TEMP database */ |
| 294 | analyzeDatabase(pParse, i); |
| 295 | } |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 296 | }else if( pName2==0 || pName2->n==0 ){ |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 297 | /* Form 2: Analyze the database or table named */ |
| 298 | iDb = sqlite3FindDb(db, pName1); |
| 299 | if( iDb>=0 ){ |
| 300 | analyzeDatabase(pParse, iDb); |
drh | e6e0496 | 2005-07-23 02:17:03 +0000 | [diff] [blame] | 301 | }else{ |
| 302 | z = sqlite3NameFromToken(pName1); |
| 303 | pTab = sqlite3LocateTable(pParse, z, 0); |
| 304 | sqliteFree(z); |
| 305 | if( pTab ){ |
| 306 | analyzeTable(pParse, pTab); |
| 307 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 308 | } |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 309 | }else{ |
| 310 | /* Form 3: Analyze the fully qualified table name */ |
| 311 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); |
| 312 | if( iDb>=0 ){ |
| 313 | zDb = db->aDb[iDb].zName; |
| 314 | z = sqlite3NameFromToken(pTableName); |
| 315 | pTab = sqlite3LocateTable(pParse, z, zDb); |
| 316 | sqliteFree(z); |
| 317 | if( pTab ){ |
| 318 | analyzeTable(pParse, pTab); |
| 319 | } |
| 320 | } |
| 321 | } |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 322 | } |
| 323 | |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 324 | /* |
| 325 | ** Used to pass information from the analyzer reader through to the |
| 326 | ** callback routine. |
| 327 | */ |
| 328 | typedef struct analysisInfo analysisInfo; |
| 329 | struct analysisInfo { |
| 330 | sqlite3 *db; |
| 331 | const char *zDatabase; |
| 332 | }; |
| 333 | |
| 334 | /* |
| 335 | ** This callback is invoked once for each index when reading the |
| 336 | ** sqlite_stat1 table. |
| 337 | ** |
| 338 | ** argv[0] = name of the index |
| 339 | ** argv[1] = results of analysis - on integer for each column |
| 340 | */ |
| 341 | static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){ |
| 342 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 343 | Index *pIndex; |
| 344 | int i, c; |
| 345 | unsigned int v; |
| 346 | const char *z; |
| 347 | |
| 348 | assert( argc==2 ); |
drh | 1ec43c9 | 2005-09-06 10:26:47 +0000 | [diff] [blame] | 349 | if( argv==0 || argv[0]==0 || argv[1]==0 ){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase); |
| 353 | if( pIndex==0 ){ |
| 354 | return 0; |
| 355 | } |
| 356 | z = argv[1]; |
drh | 17a18f2 | 2005-07-23 14:52:12 +0000 | [diff] [blame] | 357 | for(i=0; *z && i<=pIndex->nColumn; i++){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 358 | v = 0; |
| 359 | while( (c=z[0])>='0' && c<='9' ){ |
| 360 | v = v*10 + c - '0'; |
| 361 | z++; |
| 362 | } |
| 363 | pIndex->aiRowEst[i] = v; |
| 364 | if( *z==' ' ) z++; |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | ** Load the content of the sqlite_stat1 table into the index hash tables. |
| 371 | */ |
| 372 | void sqlite3AnalysisLoad(sqlite3 *db, int iDb){ |
| 373 | analysisInfo sInfo; |
| 374 | HashElem *i; |
| 375 | char *zSql; |
| 376 | |
| 377 | /* Clear any prior statistics */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 378 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 379 | Index *pIdx = sqliteHashData(i); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 380 | sqlite3DefaultRowEst(pIdx); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* Check to make sure the sqlite_stat1 table existss */ |
| 384 | sInfo.db = db; |
| 385 | sInfo.zDatabase = db->aDb[iDb].zName; |
| 386 | if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* Load new statistics out of the sqlite_stat1 table */ |
| 392 | zSql = sqlite3MPrintf("SELECT idx, stat FROM %Q.sqlite_stat1", |
| 393 | sInfo.zDatabase); |
| 394 | sqlite3SafetyOff(db); |
| 395 | sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 396 | sqlite3SafetyOn(db); |
| 397 | sqliteFree(zSql); |
| 398 | } |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 399 | |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 400 | |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 401 | #endif /* SQLITE_OMIT_ANALYZE */ |