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