blob: 4669c733b350c14af97b3790f34aa67cb9c02230 [file] [log] [blame]
drh9f18e8a2005-07-08 12:13:04 +00001/*
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**
danielk1977c00da102006-01-07 13:21:04 +000014** @(#) $Id: analyze.c,v 1.13 2006/01/07 13:21:04 danielk1977 Exp $
drh9f18e8a2005-07-08 12:13:04 +000015*/
16#ifndef SQLITE_OMIT_ANALYZE
17#include "sqliteInt.h"
18
19/*
drhff2d5ea2005-07-23 00:41:48 +000020** 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*/
27static 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
danielk1977c00da102006-01-07 13:21:04 +000064 /* 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.
drhff2d5ea2005-07-23 00:41:48 +000068 */
danielk1977c00da102006-01-07 13:21:04 +000069 if( iRootPage>0 ){
70 sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
71 }
drhff2d5ea2005-07-23 00:41:48 +000072 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhe6e04962005-07-23 02:17:03 +000073 sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage);
drhff2d5ea2005-07-23 00:41:48 +000074 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*/
81static 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 */
danielk1977da184232006-01-05 11:34:32 +000095 int iDb; /* Index of database containing pTab */
drhff2d5ea2005-07-23 00:41:48 +000096
97 v = sqlite3GetVdbe(pParse);
drh0c356672005-09-10 22:40:53 +000098 if( pTab==0 || pTab->pIndex==0 ){
99 /* Do no analysis for tables that have no indices */
drhff2d5ea2005-07-23 00:41:48 +0000100 return;
101 }
drhe6e04962005-07-23 02:17:03 +0000102
danielk1977da184232006-01-05 11:34:32 +0000103 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104 assert( iDb>=0 );
drhe6e04962005-07-23 02:17:03 +0000105#ifndef SQLITE_OMIT_AUTHORIZATION
106 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
danielk1977da184232006-01-05 11:34:32 +0000107 pParse->db->aDb[iDb].zName ) ){
drhe6e04962005-07-23 02:17:03 +0000108 return;
109 }
110#endif
111
danielk1977c00da102006-01-07 13:21:04 +0000112 /* Establish a read-lock on the table at the shared-cache level. */
113 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
114
drhff2d5ea2005-07-23 00:41:48 +0000115 iIdxCur = pParse->nTab;
116 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117 /* Open a cursor to the index to be analyzed
118 */
danielk1977da184232006-01-05 11:34:32 +0000119 assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
120 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhff2d5ea2005-07-23 00:41:48 +0000121 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 */
drhff2d5ea2005-07-23 00:41:48 +0000143 for(i=0; i<=nCol; i++){
drhd654be82005-09-20 17:42:23 +0000144 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i);
drhff2d5ea2005-07-23 00:41:48 +0000145 }
drhff2d5ea2005-07-23 00:41:48 +0000146 for(i=0; i<nCol; i++){
drhd654be82005-09-20 17:42:23 +0000147 sqlite3VdbeAddOp(v, OP_MemNull, iMem+nCol+i+1, 0);
drhff2d5ea2005-07-23 00:41:48 +0000148 }
149
150 /* Do the analysis.
151 */
drhff2d5ea2005-07-23 00:41:48 +0000152 endOfLoop = sqlite3VdbeMakeLabel(v);
drhe6e04962005-07-23 02:17:03 +0000153 sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, endOfLoop);
154 topOfLoop = sqlite3VdbeCurrentAddr(v);
drhff2d5ea2005-07-23 00:41:48 +0000155 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
drh17a18f22005-07-23 14:52:12 +0000177 ** 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:
drhff2d5ea2005-07-23 00:41:48 +0000183 **
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);
drh17a18f22005-07-23 14:52:12 +0000195 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
196 sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0);
drhff2d5ea2005-07-23 00:41:48 +0000197 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);
drh8df447f2005-11-01 15:48:24 +0000204 sqlite3VdbeAddOp(v, OP_ToInt, 0, 0);
drhff2d5ea2005-07-23 00:41:48 +0000205 if( i==nCol-1 ){
drh17a18f22005-07-23 14:52:12 +0000206 sqlite3VdbeAddOp(v, OP_Concat, nCol*2-1, 0);
drhff2d5ea2005-07-23 00:41:48 +0000207 }else{
drh17a18f22005-07-23 14:52:12 +0000208 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
drhff2d5ea2005-07-23 00:41:48 +0000209 }
210 }
drh8a512562005-11-14 22:29:05 +0000211 sqlite3VdbeOp3(v, OP_MakeRecord, 3, 0, "aaa", 0);
drhff2d5ea2005-07-23 00:41:48 +0000212 sqlite3VdbeAddOp(v, OP_Insert, iStatCur, 0);
drhd654be82005-09-20 17:42:23 +0000213 sqlite3VdbeJumpHere(v, addr);
drhff2d5ea2005-07-23 00:41:48 +0000214 }
215}
216
217/*
drh497e4462005-07-23 03:18:40 +0000218** Generate code that will cause the most recent index analysis to
219** be laoded into internal hash tables where is can be used.
220*/
221static void loadAnalysis(Parse *pParse, int iDb){
222 Vdbe *v = sqlite3GetVdbe(pParse);
223 sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0);
224}
225
226/*
drhff2d5ea2005-07-23 00:41:48 +0000227** Generate code that will do an analysis of an entire database
228*/
229static void analyzeDatabase(Parse *pParse, int iDb){
230 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:32 +0000231 DbSchema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
drhff2d5ea2005-07-23 00:41:48 +0000232 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;
danielk1977da184232006-01-05 11:34:32 +0000240 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
drhff2d5ea2005-07-23 00:41:48 +0000241 Table *pTab = (Table*)sqliteHashData(k);
242 analyzeOneTable(pParse, pTab, iStatCur, iMem);
243 }
drh497e4462005-07-23 03:18:40 +0000244 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000245}
246
247/*
248** Generate code that will do an analysis of a single table in
249** a database.
250*/
251static void analyzeTable(Parse *pParse, Table *pTab){
252 int iDb;
253 int iStatCur;
254
255 assert( pTab!=0 );
danielk1977da184232006-01-05 11:34:32 +0000256 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhff2d5ea2005-07-23 00:41:48 +0000257 sqlite3BeginWriteOperation(pParse, 0, iDb);
258 iStatCur = pParse->nTab++;
259 openStatTable(pParse, iDb, iStatCur, pTab->zName);
260 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem);
drh497e4462005-07-23 03:18:40 +0000261 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000262}
263
264/*
265** Generate code for the ANALYZE command. The parser calls this routine
266** when it recognizes an ANALYZE command.
drh9f18e8a2005-07-08 12:13:04 +0000267**
268** ANALYZE -- 1
drhff2d5ea2005-07-23 00:41:48 +0000269** ANALYZE <database> -- 2
drh9f18e8a2005-07-08 12:13:04 +0000270** 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*/
276void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
drhff2d5ea2005-07-23 00:41:48 +0000277 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 }
drhe6e04962005-07-23 02:17:03 +0000296 }else if( pName2==0 || pName2->n==0 ){
drhff2d5ea2005-07-23 00:41:48 +0000297 /* Form 2: Analyze the database or table named */
298 iDb = sqlite3FindDb(db, pName1);
299 if( iDb>=0 ){
300 analyzeDatabase(pParse, iDb);
drhe6e04962005-07-23 02:17:03 +0000301 }else{
302 z = sqlite3NameFromToken(pName1);
303 pTab = sqlite3LocateTable(pParse, z, 0);
304 sqliteFree(z);
305 if( pTab ){
306 analyzeTable(pParse, pTab);
307 }
drhff2d5ea2005-07-23 00:41:48 +0000308 }
drhff2d5ea2005-07-23 00:41:48 +0000309 }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 }
drh9f18e8a2005-07-08 12:13:04 +0000322}
323
drh497e4462005-07-23 03:18:40 +0000324/*
325** Used to pass information from the analyzer reader through to the
326** callback routine.
327*/
328typedef struct analysisInfo analysisInfo;
329struct 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*/
341static 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 );
drh1ec43c92005-09-06 10:26:47 +0000349 if( argv==0 || argv[0]==0 || argv[1]==0 ){
drh497e4462005-07-23 03:18:40 +0000350 return 0;
351 }
352 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
353 if( pIndex==0 ){
354 return 0;
355 }
356 z = argv[1];
drh17a18f22005-07-23 14:52:12 +0000357 for(i=0; *z && i<=pIndex->nColumn; i++){
drh497e4462005-07-23 03:18:40 +0000358 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*/
372void sqlite3AnalysisLoad(sqlite3 *db, int iDb){
373 analysisInfo sInfo;
374 HashElem *i;
375 char *zSql;
376
377 /* Clear any prior statistics */
danielk1977da184232006-01-05 11:34:32 +0000378 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
drh497e4462005-07-23 03:18:40 +0000379 Index *pIdx = sqliteHashData(i);
drh51147ba2005-07-23 22:59:55 +0000380 sqlite3DefaultRowEst(pIdx);
drh497e4462005-07-23 03:18:40 +0000381 }
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}
drh9f18e8a2005-07-08 12:13:04 +0000399
drhff2d5ea2005-07-23 00:41:48 +0000400
drh9f18e8a2005-07-08 12:13:04 +0000401#endif /* SQLITE_OMIT_ANALYZE */