blob: 8a59c55f604703175294163858044480ebe7dc6b [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**
drh05800a12009-04-16 17:45:47 +000014** @(#) $Id: analyze.c,v 1.52 2009/04/16 17:45:48 drh Exp $
drh9f18e8a2005-07-08 12:13:04 +000015*/
16#ifndef SQLITE_OMIT_ANALYZE
17#include "sqliteInt.h"
18
19/*
dan02fa4692009-08-17 17:06:58 +000020** This routine generates code that opens the sqlite_stat1 table for
dan69188d92009-08-19 08:18:32 +000021** writing with cursor iStatCur. If the library was built with the
22** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
23** opened for writing using cursor (iStatCur+1)
drhff2d5ea2005-07-23 00:41:48 +000024**
25** If the sqlite_stat1 tables does not previously exist, it is created.
dan69188d92009-08-19 08:18:32 +000026** Similarly, if the sqlite_stat2 table does not exist and the library
27** is compiled with SQLITE_ENABLE_STAT2 defined, it is created.
28**
29** Argument zWhere may be a pointer to a buffer containing a table name,
30** or it may be a NULL pointer. If it is not NULL, then all entries in
31** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
32** with the named table are deleted. If zWhere==0, then code is generated
33** to delete all stat table entries.
drhff2d5ea2005-07-23 00:41:48 +000034*/
35static void openStatTable(
36 Parse *pParse, /* Parsing context */
37 int iDb, /* The database we are looking in */
38 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
39 const char *zWhere /* Delete entries associated with this table */
40){
dan69188d92009-08-19 08:18:32 +000041 static struct {
42 const char *zName;
43 const char *zCols;
44 } aTable[] = {
45 { "sqlite_stat1", "tbl,idx,stat" },
46#ifdef SQLITE_ENABLE_STAT2
47 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
48#endif
49 };
50
dan02fa4692009-08-17 17:06:58 +000051 int aRoot[] = {0, 0};
52 int aCreateTbl[] = {0, 0};
53
54 int i;
drhff2d5ea2005-07-23 00:41:48 +000055 sqlite3 *db = pParse->db;
56 Db *pDb;
drhff2d5ea2005-07-23 00:41:48 +000057 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +000058 if( v==0 ) return;
drh1fee73e2007-08-29 04:00:57 +000059 assert( sqlite3BtreeHoldsAllMutexes(db) );
60 assert( sqlite3VdbeDb(v)==db );
drhff2d5ea2005-07-23 00:41:48 +000061 pDb = &db->aDb[iDb];
dan02fa4692009-08-17 17:06:58 +000062
dan69188d92009-08-19 08:18:32 +000063 for(i=0; i<ArraySize(aTable); i++){
64 const char *zTab = aTable[i].zName;
dan02fa4692009-08-17 17:06:58 +000065 Table *pStat;
dan69188d92009-08-19 08:18:32 +000066 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
dan02fa4692009-08-17 17:06:58 +000067 /* The sqlite_stat[12] table does not exist. Create it. Note that a
68 ** side-effect of the CREATE TABLE statement is to leave the rootpage
69 ** of the new table in register pParse->regRoot. This is important
70 ** because the OpenWrite opcode below will be needing it. */
71 sqlite3NestedParse(pParse,
dan69188d92009-08-19 08:18:32 +000072 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
dan02fa4692009-08-17 17:06:58 +000073 );
74 aRoot[i] = pParse->regRoot;
75 aCreateTbl[i] = 1;
76 }else{
77 /* The table already exists. If zWhere is not NULL, delete all entries
78 ** associated with the table zWhere. If zWhere is NULL, delete the
79 ** entire contents of the table. */
80 aRoot[i] = pStat->tnum;
dan69188d92009-08-19 08:18:32 +000081 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
dan02fa4692009-08-17 17:06:58 +000082 if( zWhere ){
83 sqlite3NestedParse(pParse,
dan69188d92009-08-19 08:18:32 +000084 "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
dan02fa4692009-08-17 17:06:58 +000085 );
86 }else{
87 /* The sqlite_stat[12] table already exists. Delete all rows. */
88 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
89 }
90 }
drhff2d5ea2005-07-23 00:41:48 +000091 }
92
dan02fa4692009-08-17 17:06:58 +000093 /* Open the sqlite_stat[12] tables for writing. */
dan69188d92009-08-19 08:18:32 +000094 for(i=0; i<ArraySize(aTable); i++){
dan02fa4692009-08-17 17:06:58 +000095 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
96 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
97 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
danielk1977c00da102006-01-07 13:21:04 +000098 }
drhff2d5ea2005-07-23 00:41:48 +000099}
100
101/*
102** Generate code to do an analysis of all indices associated with
103** a single table.
104*/
105static void analyzeOneTable(
106 Parse *pParse, /* Parser context */
107 Table *pTab, /* Table whose indices are to be analyzed */
drhdfe88ec2008-11-03 20:55:06 +0000108 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
drhff2d5ea2005-07-23 00:41:48 +0000109 int iMem /* Available memory locations begin here */
110){
dan0f9a34e2009-08-19 16:34:31 +0000111 sqlite3 *db = pParse->db; /* Database handle */
dan69188d92009-08-19 08:18:32 +0000112 Index *pIdx; /* An index to being analyzed */
113 int iIdxCur; /* Cursor open on index being analyzed */
114 Vdbe *v; /* The virtual machine being built up */
115 int i; /* Loop counter */
116 int topOfLoop; /* The top of the loop */
117 int endOfLoop; /* The end of the loop */
118 int addr; /* The address of an instruction */
119 int iDb; /* Index of database containing pTab */
dane275dc32009-08-18 16:24:58 +0000120 int regTabname = iMem++; /* Register containing table name */
121 int regIdxname = iMem++; /* Register containing index name */
122 int regSampleno = iMem++; /* Register containing next sample number */
123 int regCol = iMem++; /* Content of a column analyzed table */
dane275dc32009-08-18 16:24:58 +0000124 int regRec = iMem++; /* Register holding completed record */
125 int regTemp = iMem++; /* Temporary use register */
dane275dc32009-08-18 16:24:58 +0000126 int regRowid = iMem++; /* Rowid for the inserted record */
dan68c4dbb2009-08-20 09:11:06 +0000127
dan69188d92009-08-19 08:18:32 +0000128#ifdef SQLITE_ENABLE_STAT2
129 int regTemp2 = iMem++; /* Temporary use register */
dan68c4dbb2009-08-20 09:11:06 +0000130 int regSamplerecno = iMem++; /* Index of next sample to record */
131 int regRecno = iMem++; /* Current sample index */
132 int regLast = iMem++; /* Index of last sample to record */
133 int regFirst = iMem++; /* Index of first sample to record */
dan69188d92009-08-19 08:18:32 +0000134#endif
dane275dc32009-08-18 16:24:58 +0000135
drhff2d5ea2005-07-23 00:41:48 +0000136 v = sqlite3GetVdbe(pParse);
drh05800a12009-04-16 17:45:47 +0000137 if( v==0 || NEVER(pTab==0) || pTab->pIndex==0 ){
drh0c356672005-09-10 22:40:53 +0000138 /* Do no analysis for tables that have no indices */
drhff2d5ea2005-07-23 00:41:48 +0000139 return;
140 }
dan0f9a34e2009-08-19 16:34:31 +0000141 assert( sqlite3BtreeHoldsAllMutexes(db) );
142 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977da184232006-01-05 11:34:32 +0000143 assert( iDb>=0 );
drhe6e04962005-07-23 02:17:03 +0000144#ifndef SQLITE_OMIT_AUTHORIZATION
145 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
dan0f9a34e2009-08-19 16:34:31 +0000146 db->aDb[iDb].zName ) ){
drhe6e04962005-07-23 02:17:03 +0000147 return;
148 }
149#endif
150
danielk1977c00da102006-01-07 13:21:04 +0000151 /* Establish a read-lock on the table at the shared-cache level. */
152 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
153
danielk19776ab3a2e2009-02-19 14:39:25 +0000154 iIdxCur = pParse->nTab++;
drhff2d5ea2005-07-23 00:41:48 +0000155 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
dane275dc32009-08-18 16:24:58 +0000156 int nCol = pIdx->nColumn;
danielk1977b3bf5562006-01-10 17:58:23 +0000157 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
158
dane275dc32009-08-18 16:24:58 +0000159 if( iMem+1+(nCol*2)>pParse->nMem ){
160 pParse->nMem = iMem+1+(nCol*2);
161 }
162
163 /* Open a cursor to the index to be analyzed. */
dan0f9a34e2009-08-19 16:34:31 +0000164 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
danielk1977207872a2008-01-03 07:54:23 +0000165 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000166 (char *)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +0000167 VdbeComment((v, "%s", pIdx->zName));
drhff2d5ea2005-07-23 00:41:48 +0000168
dan69188d92009-08-19 08:18:32 +0000169 /* Populate the registers containing the table and index names. */
170 if( pTab->pIndex==pIdx ){
171 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
172 }
173 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
174
175#ifdef SQLITE_ENABLE_STAT2
dan68c4dbb2009-08-20 09:11:06 +0000176
dane275dc32009-08-18 16:24:58 +0000177 /* If this iteration of the loop is generating code to analyze the
dan68c4dbb2009-08-20 09:11:06 +0000178 ** first index in the pTab->pIndex list, then register regLast has
dane275dc32009-08-18 16:24:58 +0000179 ** not been populated. In this case populate it now. */
dan02fa4692009-08-17 17:06:58 +0000180 if( pTab->pIndex==pIdx ){
dan68c4dbb2009-08-20 09:11:06 +0000181 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
182 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
183 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
184
185 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
186 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
187 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
188 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
189 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
190 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
191 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
192 sqlite3VdbeJumpHere(v, addr);
dan02fa4692009-08-17 17:06:58 +0000193 }
dane275dc32009-08-18 16:24:58 +0000194
195 /* Zero the regSampleno and regRecno registers. */
196 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
197 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
dan68c4dbb2009-08-20 09:11:06 +0000198 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
dan69188d92009-08-19 08:18:32 +0000199#endif
dan02fa4692009-08-17 17:06:58 +0000200
dan85c165c2009-08-19 14:34:54 +0000201 /* The block of memory cells initialized here is used as follows.
drhff2d5ea2005-07-23 00:41:48 +0000202 **
dan85c165c2009-08-19 14:34:54 +0000203 ** iMem:
204 ** The total number of rows in the table.
dan02fa4692009-08-17 17:06:58 +0000205 **
dan85c165c2009-08-19 14:34:54 +0000206 ** iMem+1 .. iMem+nCol:
207 ** Number of distinct entries in index considering the
208 ** left-most N columns only, where N is between 1 and nCol,
209 ** inclusive.
dan02fa4692009-08-17 17:06:58 +0000210 **
dan85c165c2009-08-19 14:34:54 +0000211 ** iMem+nCol+1 .. Mem+2*nCol:
212 ** Previous value of indexed columns, from left to right.
dan02fa4692009-08-17 17:06:58 +0000213 **
dan85c165c2009-08-19 14:34:54 +0000214 ** Cells iMem through iMem+nCol are initialized to 0. The others are
215 ** initialized to contain an SQL NULL.
drhff2d5ea2005-07-23 00:41:48 +0000216 */
drhff2d5ea2005-07-23 00:41:48 +0000217 for(i=0; i<=nCol; i++){
drh4c583122008-01-04 22:01:03 +0000218 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
drhff2d5ea2005-07-23 00:41:48 +0000219 }
drhff2d5ea2005-07-23 00:41:48 +0000220 for(i=0; i<nCol; i++){
drh4c583122008-01-04 22:01:03 +0000221 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000222 }
223
dane275dc32009-08-18 16:24:58 +0000224 /* Start the analysis loop. This loop runs through all the entries in
dan02fa4692009-08-17 17:06:58 +0000225 ** the index b-tree. */
drhff2d5ea2005-07-23 00:41:48 +0000226 endOfLoop = sqlite3VdbeMakeLabel(v);
drh66a51672008-01-03 00:01:23 +0000227 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
drhe6e04962005-07-23 02:17:03 +0000228 topOfLoop = sqlite3VdbeCurrentAddr(v);
drh8558cde2008-01-05 05:20:10 +0000229 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
dan02fa4692009-08-17 17:06:58 +0000230
drhff2d5ea2005-07-23 00:41:48 +0000231 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000232 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
dan69188d92009-08-19 08:18:32 +0000233#ifdef SQLITE_ENABLE_STAT2
dan02fa4692009-08-17 17:06:58 +0000234 if( i==0 ){
dane275dc32009-08-18 16:24:58 +0000235 /* Check if the record that cursor iIdxCur points to contains a
236 ** value that should be stored in the sqlite_stat2 table. If so,
237 ** store it. */
238 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
239 assert( regTabname+1==regIdxname
240 && regTabname+2==regSampleno
241 && regTabname+3==regCol
242 );
dan68c4dbb2009-08-20 09:11:06 +0000243 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
dane275dc32009-08-18 16:24:58 +0000244 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
245 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
246 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
247
248 /* Calculate new values for regSamplerecno and regSampleno.
249 **
250 ** sampleno = sampleno + 1
251 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
252 */
253 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
dan68c4dbb2009-08-20 09:11:06 +0000254 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
dane275dc32009-08-18 16:24:58 +0000255 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
256 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
257 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
258 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
259 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
260
261 sqlite3VdbeJumpHere(v, ne);
262 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
dan02fa4692009-08-17 17:06:58 +0000263 }
dan69188d92009-08-19 08:18:32 +0000264#endif
265
drh2d401ab2008-01-10 23:50:11 +0000266 sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
267 /**** TODO: add collating sequence *****/
drh35573352008-01-08 23:54:25 +0000268 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
drhff2d5ea2005-07-23 00:41:48 +0000269 }
dan0f9a34e2009-08-19 16:34:31 +0000270 if( db->mallocFailed ){
271 /* If a malloc failure has occurred, then the result of the expression
272 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
273 ** below may be negative. Which causes an assert() to fail (or an
274 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
275 return;
276 }
drh66a51672008-01-03 00:01:23 +0000277 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
drhff2d5ea2005-07-23 00:41:48 +0000278 for(i=0; i<nCol; i++){
dan85c165c2009-08-19 14:34:54 +0000279 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-(nCol*2));
drh2d401ab2008-01-10 23:50:11 +0000280 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
drhb1fdb2a2008-01-05 04:06:03 +0000281 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000282 }
dan02fa4692009-08-17 17:06:58 +0000283
284 /* End of the analysis loop. */
drhff2d5ea2005-07-23 00:41:48 +0000285 sqlite3VdbeResolveLabel(v, endOfLoop);
drh66a51672008-01-03 00:01:23 +0000286 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
287 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
drhff2d5ea2005-07-23 00:41:48 +0000288
dan02fa4692009-08-17 17:06:58 +0000289 /* Store the results in sqlite_stat1.
drhff2d5ea2005-07-23 00:41:48 +0000290 **
drh4c103832007-06-20 13:37:31 +0000291 ** The result is a single row of the sqlite_stat1 table. The first
drhff2d5ea2005-07-23 00:41:48 +0000292 ** two columns are the names of the table and index. The third column
293 ** is a string composed of a list of integer statistics about the
danielk19772f886d12009-02-28 10:47:41 +0000294 ** index. The first integer in the list is the total number of entries
drh17a18f22005-07-23 14:52:12 +0000295 ** in the index. There is one additional integer in the list for each
296 ** column of the table. This additional integer is a guess of how many
297 ** rows of the table the index will select. If D is the count of distinct
298 ** values and K is the total number of rows, then the integer is computed
299 ** as:
drhff2d5ea2005-07-23 00:41:48 +0000300 **
301 ** I = (K+D-1)/D
302 **
303 ** If K==0 then no entry is made into the sqlite_stat1 table.
304 ** If K>0 then it is always the case the D>0 so division by zero
305 ** is never possible.
306 */
drh2d401ab2008-01-10 23:50:11 +0000307 addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
dane275dc32009-08-18 16:24:58 +0000308 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
drhff2d5ea2005-07-23 00:41:48 +0000309 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000310 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
dane275dc32009-08-18 16:24:58 +0000311 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drh2d401ab2008-01-10 23:50:11 +0000312 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
313 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
314 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
315 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
dane275dc32009-08-18 16:24:58 +0000316 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drhff2d5ea2005-07-23 00:41:48 +0000317 }
dane275dc32009-08-18 16:24:58 +0000318 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
drh2d401ab2008-01-10 23:50:11 +0000319 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
320 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
321 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drhd654be82005-09-20 17:42:23 +0000322 sqlite3VdbeJumpHere(v, addr);
drhff2d5ea2005-07-23 00:41:48 +0000323 }
324}
325
326/*
drh497e4462005-07-23 03:18:40 +0000327** Generate code that will cause the most recent index analysis to
328** be laoded into internal hash tables where is can be used.
329*/
330static void loadAnalysis(Parse *pParse, int iDb){
331 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +0000332 if( v ){
drh66a51672008-01-03 00:01:23 +0000333 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
drhcf1be452007-05-12 12:08:51 +0000334 }
drh497e4462005-07-23 03:18:40 +0000335}
336
337/*
drhff2d5ea2005-07-23 00:41:48 +0000338** Generate code that will do an analysis of an entire database
339*/
340static void analyzeDatabase(Parse *pParse, int iDb){
341 sqlite3 *db = pParse->db;
danielk1977e501b892006-01-09 06:29:47 +0000342 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
drhff2d5ea2005-07-23 00:41:48 +0000343 HashElem *k;
344 int iStatCur;
345 int iMem;
346
347 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000348 iStatCur = pParse->nTab;
349 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000350 openStatTable(pParse, iDb, iStatCur, 0);
drh0a07c102008-01-03 18:03:08 +0000351 iMem = pParse->nMem+1;
danielk1977da184232006-01-05 11:34:32 +0000352 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
drhff2d5ea2005-07-23 00:41:48 +0000353 Table *pTab = (Table*)sqliteHashData(k);
354 analyzeOneTable(pParse, pTab, iStatCur, iMem);
355 }
drh497e4462005-07-23 03:18:40 +0000356 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000357}
358
359/*
360** Generate code that will do an analysis of a single table in
361** a database.
362*/
363static void analyzeTable(Parse *pParse, Table *pTab){
364 int iDb;
365 int iStatCur;
366
367 assert( pTab!=0 );
drh1fee73e2007-08-29 04:00:57 +0000368 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000369 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhff2d5ea2005-07-23 00:41:48 +0000370 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000371 iStatCur = pParse->nTab;
372 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000373 openStatTable(pParse, iDb, iStatCur, pTab->zName);
drh0a07c102008-01-03 18:03:08 +0000374 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
drh497e4462005-07-23 03:18:40 +0000375 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000376}
377
378/*
379** Generate code for the ANALYZE command. The parser calls this routine
380** when it recognizes an ANALYZE command.
drh9f18e8a2005-07-08 12:13:04 +0000381**
382** ANALYZE -- 1
drhff2d5ea2005-07-23 00:41:48 +0000383** ANALYZE <database> -- 2
drh9f18e8a2005-07-08 12:13:04 +0000384** ANALYZE ?<database>.?<tablename> -- 3
385**
386** Form 1 causes all indices in all attached databases to be analyzed.
387** Form 2 analyzes all indices the single database named.
388** Form 3 analyzes all indices associated with the named table.
389*/
390void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
drhff2d5ea2005-07-23 00:41:48 +0000391 sqlite3 *db = pParse->db;
392 int iDb;
393 int i;
394 char *z, *zDb;
395 Table *pTab;
396 Token *pTableName;
397
398 /* Read the database schema. If an error occurs, leave an error message
399 ** and code in pParse and return NULL. */
drh1fee73e2007-08-29 04:00:57 +0000400 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhff2d5ea2005-07-23 00:41:48 +0000401 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
402 return;
403 }
404
drh05800a12009-04-16 17:45:47 +0000405 assert( pName2!=0 || pName1==0 );
drhff2d5ea2005-07-23 00:41:48 +0000406 if( pName1==0 ){
407 /* Form 1: Analyze everything */
408 for(i=0; i<db->nDb; i++){
409 if( i==1 ) continue; /* Do not analyze the TEMP database */
410 analyzeDatabase(pParse, i);
411 }
drh05800a12009-04-16 17:45:47 +0000412 }else if( pName2->n==0 ){
drhff2d5ea2005-07-23 00:41:48 +0000413 /* Form 2: Analyze the database or table named */
414 iDb = sqlite3FindDb(db, pName1);
415 if( iDb>=0 ){
416 analyzeDatabase(pParse, iDb);
drhe6e04962005-07-23 02:17:03 +0000417 }else{
drh17435752007-08-16 04:30:38 +0000418 z = sqlite3NameFromToken(db, pName1);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000419 if( z ){
drhca424112008-01-25 15:04:48 +0000420 pTab = sqlite3LocateTable(pParse, 0, z, 0);
drh633e6d52008-07-28 19:34:53 +0000421 sqlite3DbFree(db, z);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000422 if( pTab ){
423 analyzeTable(pParse, pTab);
424 }
drhe6e04962005-07-23 02:17:03 +0000425 }
drhff2d5ea2005-07-23 00:41:48 +0000426 }
drhff2d5ea2005-07-23 00:41:48 +0000427 }else{
428 /* Form 3: Analyze the fully qualified table name */
429 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
430 if( iDb>=0 ){
431 zDb = db->aDb[iDb].zName;
drh17435752007-08-16 04:30:38 +0000432 z = sqlite3NameFromToken(db, pTableName);
drhcf1be452007-05-12 12:08:51 +0000433 if( z ){
drhca424112008-01-25 15:04:48 +0000434 pTab = sqlite3LocateTable(pParse, 0, z, zDb);
drh633e6d52008-07-28 19:34:53 +0000435 sqlite3DbFree(db, z);
drhcf1be452007-05-12 12:08:51 +0000436 if( pTab ){
437 analyzeTable(pParse, pTab);
438 }
drhff2d5ea2005-07-23 00:41:48 +0000439 }
440 }
441 }
drh9f18e8a2005-07-08 12:13:04 +0000442}
443
drh497e4462005-07-23 03:18:40 +0000444/*
445** Used to pass information from the analyzer reader through to the
446** callback routine.
447*/
448typedef struct analysisInfo analysisInfo;
449struct analysisInfo {
450 sqlite3 *db;
451 const char *zDatabase;
452};
453
454/*
455** This callback is invoked once for each index when reading the
456** sqlite_stat1 table.
457**
458** argv[0] = name of the index
459** argv[1] = results of analysis - on integer for each column
460*/
danielk197762c14b32008-11-19 09:05:26 +0000461static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
drh497e4462005-07-23 03:18:40 +0000462 analysisInfo *pInfo = (analysisInfo*)pData;
463 Index *pIndex;
464 int i, c;
465 unsigned int v;
466 const char *z;
467
468 assert( argc==2 );
danielk1977f3d3c272008-11-19 16:52:44 +0000469 UNUSED_PARAMETER2(NotUsed, argc);
470
drh1ec43c92005-09-06 10:26:47 +0000471 if( argv==0 || argv[0]==0 || argv[1]==0 ){
drh497e4462005-07-23 03:18:40 +0000472 return 0;
473 }
474 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
475 if( pIndex==0 ){
476 return 0;
477 }
478 z = argv[1];
drh17a18f22005-07-23 14:52:12 +0000479 for(i=0; *z && i<=pIndex->nColumn; i++){
drh497e4462005-07-23 03:18:40 +0000480 v = 0;
481 while( (c=z[0])>='0' && c<='9' ){
482 v = v*10 + c - '0';
483 z++;
484 }
485 pIndex->aiRowEst[i] = v;
486 if( *z==' ' ) z++;
487 }
488 return 0;
489}
490
491/*
dan85c165c2009-08-19 14:34:54 +0000492** If the Index.aSample variable is not NULL, delete the aSample[] array
493** and its contents.
494*/
495void sqlite3DeleteIndexSamples(Index *pIdx){
496#ifdef SQLITE_ENABLE_STAT2
497 if( pIdx->aSample ){
498 int j;
499 sqlite3 *dbMem = pIdx->pTable->dbMem;
500 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
501 IndexSample *p = &pIdx->aSample[j];
502 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
503 sqlite3DbFree(pIdx->pTable->dbMem, p->u.z);
504 }
505 }
506 sqlite3DbFree(dbMem, pIdx->aSample);
507 pIdx->aSample = 0;
508 }
509#endif
510}
511
512/*
513** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
514** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
515** arrays. The contents of sqlite_stat2 are used to populate the
516** Index.aSample[] arrays.
517**
518** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
519** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
520** during compilation and the sqlite_stat2 table is present, no data is
521** read from it.
522**
523** If SQLITE_ENABLE_STAT2 was defined during compilation and the
524** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
525** returned. However, in this case, data is read from the sqlite_stat1
526** table (if it is present) before returning.
527**
528** If an OOM error occurs, this function always sets db->mallocFailed.
529** This means if the caller does not care about other errors, the return
530** code may be ignored.
drh497e4462005-07-23 03:18:40 +0000531*/
drhcf1be452007-05-12 12:08:51 +0000532int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
drh497e4462005-07-23 03:18:40 +0000533 analysisInfo sInfo;
534 HashElem *i;
535 char *zSql;
drhcf1be452007-05-12 12:08:51 +0000536 int rc;
drh497e4462005-07-23 03:18:40 +0000537
drhff0587c2007-08-29 17:43:19 +0000538 assert( iDb>=0 && iDb<db->nDb );
539 assert( db->aDb[iDb].pBt!=0 );
540 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
drh1fee73e2007-08-29 04:00:57 +0000541
drh497e4462005-07-23 03:18:40 +0000542 /* Clear any prior statistics */
danielk1977da184232006-01-05 11:34:32 +0000543 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
drh497e4462005-07-23 03:18:40 +0000544 Index *pIdx = sqliteHashData(i);
drh51147ba2005-07-23 22:59:55 +0000545 sqlite3DefaultRowEst(pIdx);
dan85c165c2009-08-19 14:34:54 +0000546 sqlite3DeleteIndexSamples(pIdx);
drh497e4462005-07-23 03:18:40 +0000547 }
548
dan85c165c2009-08-19 14:34:54 +0000549 /* Check to make sure the sqlite_stat1 table exists */
drh497e4462005-07-23 03:18:40 +0000550 sInfo.db = db;
551 sInfo.zDatabase = db->aDb[iDb].zName;
552 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
dan85c165c2009-08-19 14:34:54 +0000553 return SQLITE_ERROR;
drh497e4462005-07-23 03:18:40 +0000554 }
555
drh497e4462005-07-23 03:18:40 +0000556 /* Load new statistics out of the sqlite_stat1 table */
dan85c165c2009-08-19 14:34:54 +0000557 zSql = sqlite3MPrintf(db,
558 "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
drhf16ce3b2009-02-13 16:59:53 +0000559 if( zSql==0 ){
560 rc = SQLITE_NOMEM;
561 }else{
562 (void)sqlite3SafetyOff(db);
563 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
564 (void)sqlite3SafetyOn(db);
565 sqlite3DbFree(db, zSql);
drhf16ce3b2009-02-13 16:59:53 +0000566 }
dan02fa4692009-08-17 17:06:58 +0000567
dan85c165c2009-08-19 14:34:54 +0000568
dane275dc32009-08-18 16:24:58 +0000569 /* Load the statistics from the sqlite_stat2 table. */
dan69188d92009-08-19 08:18:32 +0000570#ifdef SQLITE_ENABLE_STAT2
dan85c165c2009-08-19 14:34:54 +0000571 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
572 rc = SQLITE_ERROR;
573 }
dan02fa4692009-08-17 17:06:58 +0000574 if( rc==SQLITE_OK ){
dane275dc32009-08-18 16:24:58 +0000575 sqlite3_stmt *pStmt = 0;
576
dan02fa4692009-08-17 17:06:58 +0000577 zSql = sqlite3MPrintf(db,
dan85c165c2009-08-19 14:34:54 +0000578 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
dane275dc32009-08-18 16:24:58 +0000579 if( !zSql ){
dan85c165c2009-08-19 14:34:54 +0000580 rc = SQLITE_NOMEM;
581 }else{
582 (void)sqlite3SafetyOff(db);
583 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
584 (void)sqlite3SafetyOn(db);
585 sqlite3DbFree(db, zSql);
dan02fa4692009-08-17 17:06:58 +0000586 }
dane275dc32009-08-18 16:24:58 +0000587
dane275dc32009-08-18 16:24:58 +0000588 if( rc==SQLITE_OK ){
dan85c165c2009-08-19 14:34:54 +0000589 (void)sqlite3SafetyOff(db);
dane275dc32009-08-18 16:24:58 +0000590 while( sqlite3_step(pStmt)==SQLITE_ROW ){
591 char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
592 Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
593 if( pIdx ){
594 int iSample = sqlite3_column_int(pStmt, 1);
dana898aac2009-08-19 09:09:38 +0000595 sqlite3 *dbMem = pIdx->pTable->dbMem;
dan85c165c2009-08-19 14:34:54 +0000596 assert( dbMem==db || dbMem==0 );
dane275dc32009-08-18 16:24:58 +0000597 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
598 int eType = sqlite3_column_type(pStmt, 2);
599
600 if( pIdx->aSample==0 ){
dana898aac2009-08-19 09:09:38 +0000601 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
602 pIdx->aSample = (IndexSample *)sqlite3DbMallocZero(dbMem, sz);
dan69188d92009-08-19 08:18:32 +0000603 if( pIdx->aSample==0 ){
dan85c165c2009-08-19 14:34:54 +0000604 db->mallocFailed = 1;
dan69188d92009-08-19 08:18:32 +0000605 break;
606 }
dane275dc32009-08-18 16:24:58 +0000607 }
608
609 if( pIdx->aSample ){
610 IndexSample *pSample = &pIdx->aSample[iSample];
dan69188d92009-08-19 08:18:32 +0000611 pSample->eType = eType;
612 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
dane275dc32009-08-18 16:24:58 +0000613 pSample->u.r = sqlite3_column_double(pStmt, 2);
dan69188d92009-08-19 08:18:32 +0000614 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
dane275dc32009-08-18 16:24:58 +0000615 const char *z = (const char *)(
616 (eType==SQLITE_BLOB) ?
617 sqlite3_column_blob(pStmt, 2):
618 sqlite3_column_text(pStmt, 2)
619 );
620 int n = sqlite3_column_bytes(pStmt, 2);
621 if( n>24 ){
622 n = 24;
623 }
624 pSample->nByte = n;
dana898aac2009-08-19 09:09:38 +0000625 pSample->u.z = sqlite3DbMallocRaw(dbMem, n);
dane275dc32009-08-18 16:24:58 +0000626 if( pSample->u.z ){
627 memcpy(pSample->u.z, z, n);
628 }else{
dan85c165c2009-08-19 14:34:54 +0000629 db->mallocFailed = 1;
dan69188d92009-08-19 08:18:32 +0000630 break;
631 }
dane275dc32009-08-18 16:24:58 +0000632 }
633 }
634 }
635 }
636 }
637 rc = sqlite3_finalize(pStmt);
dan85c165c2009-08-19 14:34:54 +0000638 (void)sqlite3SafetyOn(db);
dane275dc32009-08-18 16:24:58 +0000639 }
dan02fa4692009-08-17 17:06:58 +0000640 }
dan69188d92009-08-19 08:18:32 +0000641#endif
dan02fa4692009-08-17 17:06:58 +0000642
dane275dc32009-08-18 16:24:58 +0000643 if( rc==SQLITE_NOMEM ){
644 db->mallocFailed = 1;
645 }
drhcf1be452007-05-12 12:08:51 +0000646 return rc;
drh497e4462005-07-23 03:18:40 +0000647}
drh9f18e8a2005-07-08 12:13:04 +0000648
drhff2d5ea2005-07-23 00:41:48 +0000649
drh9f18e8a2005-07-08 12:13:04 +0000650#endif /* SQLITE_OMIT_ANALYZE */