blob: 0a8339bafbad32bc2932abcc73022e6a3fb2e71a [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.
drh9f18e8a2005-07-08 12:13:04 +000013*/
14#ifndef SQLITE_OMIT_ANALYZE
15#include "sqliteInt.h"
16
17/*
dan02fa4692009-08-17 17:06:58 +000018** This routine generates code that opens the sqlite_stat1 table for
dan69188d92009-08-19 08:18:32 +000019** writing with cursor iStatCur. If the library was built with the
20** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
21** opened for writing using cursor (iStatCur+1)
drhff2d5ea2005-07-23 00:41:48 +000022**
23** If the sqlite_stat1 tables does not previously exist, it is created.
dan69188d92009-08-19 08:18:32 +000024** Similarly, if the sqlite_stat2 table does not exist and the library
25** is compiled with SQLITE_ENABLE_STAT2 defined, it is created.
26**
27** Argument zWhere may be a pointer to a buffer containing a table name,
28** or it may be a NULL pointer. If it is not NULL, then all entries in
29** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
30** with the named table are deleted. If zWhere==0, then code is generated
31** to delete all stat table entries.
drhff2d5ea2005-07-23 00:41:48 +000032*/
33static void openStatTable(
34 Parse *pParse, /* Parsing context */
35 int iDb, /* The database we are looking in */
36 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
37 const char *zWhere /* Delete entries associated with this table */
38){
dan558814f2010-06-02 05:53:53 +000039 static const struct {
dan69188d92009-08-19 08:18:32 +000040 const char *zName;
41 const char *zCols;
42 } aTable[] = {
43 { "sqlite_stat1", "tbl,idx,stat" },
44#ifdef SQLITE_ENABLE_STAT2
45 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
46#endif
47 };
48
dan02fa4692009-08-17 17:06:58 +000049 int aRoot[] = {0, 0};
shanecea72b22009-09-07 04:38:36 +000050 u8 aCreateTbl[] = {0, 0};
dan02fa4692009-08-17 17:06:58 +000051
52 int i;
drhff2d5ea2005-07-23 00:41:48 +000053 sqlite3 *db = pParse->db;
54 Db *pDb;
drhff2d5ea2005-07-23 00:41:48 +000055 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +000056 if( v==0 ) return;
drh1fee73e2007-08-29 04:00:57 +000057 assert( sqlite3BtreeHoldsAllMutexes(db) );
58 assert( sqlite3VdbeDb(v)==db );
drhff2d5ea2005-07-23 00:41:48 +000059 pDb = &db->aDb[iDb];
dan02fa4692009-08-17 17:06:58 +000060
dan69188d92009-08-19 08:18:32 +000061 for(i=0; i<ArraySize(aTable); i++){
62 const char *zTab = aTable[i].zName;
dan02fa4692009-08-17 17:06:58 +000063 Table *pStat;
dan69188d92009-08-19 08:18:32 +000064 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
dan02fa4692009-08-17 17:06:58 +000065 /* The sqlite_stat[12] table does not exist. Create it. Note that a
66 ** side-effect of the CREATE TABLE statement is to leave the rootpage
67 ** of the new table in register pParse->regRoot. This is important
68 ** because the OpenWrite opcode below will be needing it. */
69 sqlite3NestedParse(pParse,
dan69188d92009-08-19 08:18:32 +000070 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
dan02fa4692009-08-17 17:06:58 +000071 );
72 aRoot[i] = pParse->regRoot;
73 aCreateTbl[i] = 1;
74 }else{
75 /* The table already exists. If zWhere is not NULL, delete all entries
76 ** associated with the table zWhere. If zWhere is NULL, delete the
77 ** entire contents of the table. */
78 aRoot[i] = pStat->tnum;
dan69188d92009-08-19 08:18:32 +000079 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
dan02fa4692009-08-17 17:06:58 +000080 if( zWhere ){
81 sqlite3NestedParse(pParse,
dan69188d92009-08-19 08:18:32 +000082 "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
dan02fa4692009-08-17 17:06:58 +000083 );
84 }else{
85 /* The sqlite_stat[12] table already exists. Delete all rows. */
86 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
87 }
88 }
drhff2d5ea2005-07-23 00:41:48 +000089 }
90
dan02fa4692009-08-17 17:06:58 +000091 /* Open the sqlite_stat[12] tables for writing. */
dan69188d92009-08-19 08:18:32 +000092 for(i=0; i<ArraySize(aTable); i++){
dan02fa4692009-08-17 17:06:58 +000093 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
94 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
95 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
danielk1977c00da102006-01-07 13:21:04 +000096 }
drhff2d5ea2005-07-23 00:41:48 +000097}
98
99/*
100** Generate code to do an analysis of all indices associated with
101** a single table.
102*/
103static void analyzeOneTable(
104 Parse *pParse, /* Parser context */
105 Table *pTab, /* Table whose indices are to be analyzed */
drhdfe88ec2008-11-03 20:55:06 +0000106 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
drhff2d5ea2005-07-23 00:41:48 +0000107 int iMem /* Available memory locations begin here */
108){
dan0f9a34e2009-08-19 16:34:31 +0000109 sqlite3 *db = pParse->db; /* Database handle */
dan69188d92009-08-19 08:18:32 +0000110 Index *pIdx; /* An index to being analyzed */
111 int iIdxCur; /* Cursor open on index being analyzed */
112 Vdbe *v; /* The virtual machine being built up */
113 int i; /* Loop counter */
114 int topOfLoop; /* The top of the loop */
115 int endOfLoop; /* The end of the loop */
drh6ac78a02010-09-28 14:26:36 +0000116 int addr = 0; /* The address of an instruction */
drh15564052010-09-25 22:32:56 +0000117 int jZeroRows = 0; /* Jump from here if number of rows is zero */
dan69188d92009-08-19 08:18:32 +0000118 int iDb; /* Index of database containing pTab */
dane275dc32009-08-18 16:24:58 +0000119 int regTabname = iMem++; /* Register containing table name */
120 int regIdxname = iMem++; /* Register containing index name */
121 int regSampleno = iMem++; /* Register containing next sample number */
122 int regCol = iMem++; /* Content of a column analyzed table */
dane275dc32009-08-18 16:24:58 +0000123 int regRec = iMem++; /* Register holding completed record */
124 int regTemp = iMem++; /* Temporary use register */
dane275dc32009-08-18 16:24:58 +0000125 int regRowid = iMem++; /* Rowid for the inserted record */
dan68c4dbb2009-08-20 09:11:06 +0000126
dan69188d92009-08-19 08:18:32 +0000127#ifdef SQLITE_ENABLE_STAT2
128 int regTemp2 = iMem++; /* Temporary use register */
dan68c4dbb2009-08-20 09:11:06 +0000129 int regSamplerecno = iMem++; /* Index of next sample to record */
130 int regRecno = iMem++; /* Current sample index */
131 int regLast = iMem++; /* Index of last sample to record */
132 int regFirst = iMem++; /* Index of first sample to record */
dan69188d92009-08-19 08:18:32 +0000133#endif
dane275dc32009-08-18 16:24:58 +0000134
drhff2d5ea2005-07-23 00:41:48 +0000135 v = sqlite3GetVdbe(pParse);
drh15564052010-09-25 22:32:56 +0000136 if( v==0 || NEVER(pTab==0) ){
137 return;
138 }
drhf39d29c2010-09-28 17:34:46 +0000139 if( pTab->tnum==0 ){
140 /* Do not gather statistics on views or virtual tables */
drh15564052010-09-25 22:32:56 +0000141 return;
142 }
143 if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
144 /* Do not gather statistics on system tables */
drhff2d5ea2005-07-23 00:41:48 +0000145 return;
146 }
dan0f9a34e2009-08-19 16:34:31 +0000147 assert( sqlite3BtreeHoldsAllMutexes(db) );
148 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977da184232006-01-05 11:34:32 +0000149 assert( iDb>=0 );
drhe6e04962005-07-23 02:17:03 +0000150#ifndef SQLITE_OMIT_AUTHORIZATION
151 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
dan0f9a34e2009-08-19 16:34:31 +0000152 db->aDb[iDb].zName ) ){
drhe6e04962005-07-23 02:17:03 +0000153 return;
154 }
155#endif
156
danielk1977c00da102006-01-07 13:21:04 +0000157 /* Establish a read-lock on the table at the shared-cache level. */
158 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
159
danielk19776ab3a2e2009-02-19 14:39:25 +0000160 iIdxCur = pParse->nTab++;
drh15564052010-09-25 22:32:56 +0000161 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
drhff2d5ea2005-07-23 00:41:48 +0000162 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
dane275dc32009-08-18 16:24:58 +0000163 int nCol = pIdx->nColumn;
danielk1977b3bf5562006-01-10 17:58:23 +0000164 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
165
dane275dc32009-08-18 16:24:58 +0000166 if( iMem+1+(nCol*2)>pParse->nMem ){
167 pParse->nMem = iMem+1+(nCol*2);
168 }
169
170 /* Open a cursor to the index to be analyzed. */
dan0f9a34e2009-08-19 16:34:31 +0000171 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
danielk1977207872a2008-01-03 07:54:23 +0000172 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000173 (char *)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +0000174 VdbeComment((v, "%s", pIdx->zName));
drhff2d5ea2005-07-23 00:41:48 +0000175
drh15564052010-09-25 22:32:56 +0000176 /* Populate the register containing the index name. */
dan69188d92009-08-19 08:18:32 +0000177 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
178
179#ifdef SQLITE_ENABLE_STAT2
dan68c4dbb2009-08-20 09:11:06 +0000180
dane275dc32009-08-18 16:24:58 +0000181 /* If this iteration of the loop is generating code to analyze the
dan68c4dbb2009-08-20 09:11:06 +0000182 ** first index in the pTab->pIndex list, then register regLast has
dane275dc32009-08-18 16:24:58 +0000183 ** not been populated. In this case populate it now. */
dan02fa4692009-08-17 17:06:58 +0000184 if( pTab->pIndex==pIdx ){
dan68c4dbb2009-08-20 09:11:06 +0000185 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
186 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
187 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
188
189 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
190 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
191 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
192 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
193 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
194 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
195 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
196 sqlite3VdbeJumpHere(v, addr);
dan02fa4692009-08-17 17:06:58 +0000197 }
dane275dc32009-08-18 16:24:58 +0000198
199 /* Zero the regSampleno and regRecno registers. */
200 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
201 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
dan68c4dbb2009-08-20 09:11:06 +0000202 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
dan69188d92009-08-19 08:18:32 +0000203#endif
dan02fa4692009-08-17 17:06:58 +0000204
dan85c165c2009-08-19 14:34:54 +0000205 /* The block of memory cells initialized here is used as follows.
drhff2d5ea2005-07-23 00:41:48 +0000206 **
dan85c165c2009-08-19 14:34:54 +0000207 ** iMem:
208 ** The total number of rows in the table.
dan02fa4692009-08-17 17:06:58 +0000209 **
dan85c165c2009-08-19 14:34:54 +0000210 ** iMem+1 .. iMem+nCol:
211 ** Number of distinct entries in index considering the
212 ** left-most N columns only, where N is between 1 and nCol,
213 ** inclusive.
dan02fa4692009-08-17 17:06:58 +0000214 **
dan85c165c2009-08-19 14:34:54 +0000215 ** iMem+nCol+1 .. Mem+2*nCol:
216 ** Previous value of indexed columns, from left to right.
dan02fa4692009-08-17 17:06:58 +0000217 **
dan85c165c2009-08-19 14:34:54 +0000218 ** Cells iMem through iMem+nCol are initialized to 0. The others are
219 ** initialized to contain an SQL NULL.
drhff2d5ea2005-07-23 00:41:48 +0000220 */
drhff2d5ea2005-07-23 00:41:48 +0000221 for(i=0; i<=nCol; i++){
drh4c583122008-01-04 22:01:03 +0000222 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
drhff2d5ea2005-07-23 00:41:48 +0000223 }
drhff2d5ea2005-07-23 00:41:48 +0000224 for(i=0; i<nCol; i++){
drh4c583122008-01-04 22:01:03 +0000225 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000226 }
227
dane275dc32009-08-18 16:24:58 +0000228 /* Start the analysis loop. This loop runs through all the entries in
dan02fa4692009-08-17 17:06:58 +0000229 ** the index b-tree. */
drhff2d5ea2005-07-23 00:41:48 +0000230 endOfLoop = sqlite3VdbeMakeLabel(v);
drh66a51672008-01-03 00:01:23 +0000231 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
drhe6e04962005-07-23 02:17:03 +0000232 topOfLoop = sqlite3VdbeCurrentAddr(v);
drh8558cde2008-01-05 05:20:10 +0000233 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
dan02fa4692009-08-17 17:06:58 +0000234
drhff2d5ea2005-07-23 00:41:48 +0000235 for(i=0; i<nCol; i++){
drh48566982011-01-04 19:01:26 +0000236 CollSeq *pColl;
drh2d401ab2008-01-10 23:50:11 +0000237 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
dan02fa4692009-08-17 17:06:58 +0000238 if( i==0 ){
drh8e93b102011-01-04 17:57:53 +0000239#ifdef SQLITE_ENABLE_STAT2
dane275dc32009-08-18 16:24:58 +0000240 /* Check if the record that cursor iIdxCur points to contains a
241 ** value that should be stored in the sqlite_stat2 table. If so,
242 ** store it. */
243 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
244 assert( regTabname+1==regIdxname
245 && regTabname+2==regSampleno
246 && regTabname+3==regCol
247 );
dan68c4dbb2009-08-20 09:11:06 +0000248 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
dane275dc32009-08-18 16:24:58 +0000249 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
250 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
251 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
252
253 /* Calculate new values for regSamplerecno and regSampleno.
254 **
255 ** sampleno = sampleno + 1
256 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
257 */
258 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
dan68c4dbb2009-08-20 09:11:06 +0000259 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
dane275dc32009-08-18 16:24:58 +0000260 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
261 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
262 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
263 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
264 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
265
266 sqlite3VdbeJumpHere(v, ne);
267 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
dan69188d92009-08-19 08:18:32 +0000268#endif
269
drh8e93b102011-01-04 17:57:53 +0000270 /* Always record the very first row */
271 sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
272 }
drh48566982011-01-04 19:01:26 +0000273 assert( pIdx->azColl!=0 );
274 assert( pIdx->azColl[i]!=0 );
275 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
276 sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
277 (char*)pColl, P4_COLLSEQ);
drh8e93b102011-01-04 17:57:53 +0000278 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
drhff2d5ea2005-07-23 00:41:48 +0000279 }
dan0f9a34e2009-08-19 16:34:31 +0000280 if( db->mallocFailed ){
281 /* If a malloc failure has occurred, then the result of the expression
282 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
283 ** below may be negative. Which causes an assert() to fail (or an
284 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
285 return;
286 }
drh66a51672008-01-03 00:01:23 +0000287 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
drhff2d5ea2005-07-23 00:41:48 +0000288 for(i=0; i<nCol; i++){
drh906b6622011-01-24 19:14:06 +0000289 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
drh8e93b102011-01-04 17:57:53 +0000290 if( i==0 ){
drh906b6622011-01-24 19:14:06 +0000291 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
drh8e93b102011-01-04 17:57:53 +0000292 }
drh906b6622011-01-24 19:14:06 +0000293 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
drh2d401ab2008-01-10 23:50:11 +0000294 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
drhb1fdb2a2008-01-05 04:06:03 +0000295 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000296 }
dan02fa4692009-08-17 17:06:58 +0000297
298 /* End of the analysis loop. */
drhff2d5ea2005-07-23 00:41:48 +0000299 sqlite3VdbeResolveLabel(v, endOfLoop);
drh66a51672008-01-03 00:01:23 +0000300 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
301 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
drhff2d5ea2005-07-23 00:41:48 +0000302
dan02fa4692009-08-17 17:06:58 +0000303 /* Store the results in sqlite_stat1.
drhff2d5ea2005-07-23 00:41:48 +0000304 **
drh4c103832007-06-20 13:37:31 +0000305 ** The result is a single row of the sqlite_stat1 table. The first
drhff2d5ea2005-07-23 00:41:48 +0000306 ** two columns are the names of the table and index. The third column
307 ** is a string composed of a list of integer statistics about the
danielk19772f886d12009-02-28 10:47:41 +0000308 ** index. The first integer in the list is the total number of entries
drh17a18f22005-07-23 14:52:12 +0000309 ** in the index. There is one additional integer in the list for each
310 ** column of the table. This additional integer is a guess of how many
311 ** rows of the table the index will select. If D is the count of distinct
312 ** values and K is the total number of rows, then the integer is computed
313 ** as:
drhff2d5ea2005-07-23 00:41:48 +0000314 **
315 ** I = (K+D-1)/D
316 **
317 ** If K==0 then no entry is made into the sqlite_stat1 table.
318 ** If K>0 then it is always the case the D>0 so division by zero
319 ** is never possible.
320 */
dane275dc32009-08-18 16:24:58 +0000321 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
drh15564052010-09-25 22:32:56 +0000322 if( jZeroRows==0 ){
323 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
324 }
drhff2d5ea2005-07-23 00:41:48 +0000325 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000326 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
dane275dc32009-08-18 16:24:58 +0000327 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drh2d401ab2008-01-10 23:50:11 +0000328 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
329 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
330 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
331 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
dane275dc32009-08-18 16:24:58 +0000332 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drhff2d5ea2005-07-23 00:41:48 +0000333 }
dane275dc32009-08-18 16:24:58 +0000334 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
drh2d401ab2008-01-10 23:50:11 +0000335 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
336 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
337 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh15564052010-09-25 22:32:56 +0000338 }
339
340 /* If the table has no indices, create a single sqlite_stat1 entry
341 ** containing NULL as the index name and the row count as the content.
342 */
343 if( pTab->pIndex==0 ){
344 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
345 VdbeComment((v, "%s", pTab->zName));
346 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
347 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
348 }else{
349 assert( jZeroRows>0 );
350 addr = sqlite3VdbeAddOp0(v, OP_Goto);
351 sqlite3VdbeJumpHere(v, jZeroRows);
352 }
353 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
354 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
355 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
356 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
357 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
358 if( pParse->nMem<regRec ) pParse->nMem = regRec;
359 if( jZeroRows ){
drhd654be82005-09-20 17:42:23 +0000360 sqlite3VdbeJumpHere(v, addr);
drhff2d5ea2005-07-23 00:41:48 +0000361 }
362}
363
364/*
drh497e4462005-07-23 03:18:40 +0000365** Generate code that will cause the most recent index analysis to
drh15564052010-09-25 22:32:56 +0000366** be loaded into internal hash tables where is can be used.
drh497e4462005-07-23 03:18:40 +0000367*/
368static void loadAnalysis(Parse *pParse, int iDb){
369 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +0000370 if( v ){
drh66a51672008-01-03 00:01:23 +0000371 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
drhcf1be452007-05-12 12:08:51 +0000372 }
drh497e4462005-07-23 03:18:40 +0000373}
374
375/*
drhff2d5ea2005-07-23 00:41:48 +0000376** Generate code that will do an analysis of an entire database
377*/
378static void analyzeDatabase(Parse *pParse, int iDb){
379 sqlite3 *db = pParse->db;
danielk1977e501b892006-01-09 06:29:47 +0000380 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
drhff2d5ea2005-07-23 00:41:48 +0000381 HashElem *k;
382 int iStatCur;
383 int iMem;
384
385 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000386 iStatCur = pParse->nTab;
387 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000388 openStatTable(pParse, iDb, iStatCur, 0);
drh0a07c102008-01-03 18:03:08 +0000389 iMem = pParse->nMem+1;
danielk1977da184232006-01-05 11:34:32 +0000390 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
drhff2d5ea2005-07-23 00:41:48 +0000391 Table *pTab = (Table*)sqliteHashData(k);
392 analyzeOneTable(pParse, pTab, iStatCur, iMem);
393 }
drh497e4462005-07-23 03:18:40 +0000394 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000395}
396
397/*
398** Generate code that will do an analysis of a single table in
399** a database.
400*/
401static void analyzeTable(Parse *pParse, Table *pTab){
402 int iDb;
403 int iStatCur;
404
405 assert( pTab!=0 );
drh1fee73e2007-08-29 04:00:57 +0000406 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000407 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhff2d5ea2005-07-23 00:41:48 +0000408 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000409 iStatCur = pParse->nTab;
410 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000411 openStatTable(pParse, iDb, iStatCur, pTab->zName);
drh0a07c102008-01-03 18:03:08 +0000412 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
drh497e4462005-07-23 03:18:40 +0000413 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000414}
415
416/*
417** Generate code for the ANALYZE command. The parser calls this routine
418** when it recognizes an ANALYZE command.
drh9f18e8a2005-07-08 12:13:04 +0000419**
420** ANALYZE -- 1
drhff2d5ea2005-07-23 00:41:48 +0000421** ANALYZE <database> -- 2
drh9f18e8a2005-07-08 12:13:04 +0000422** ANALYZE ?<database>.?<tablename> -- 3
423**
424** Form 1 causes all indices in all attached databases to be analyzed.
425** Form 2 analyzes all indices the single database named.
426** Form 3 analyzes all indices associated with the named table.
427*/
428void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
drhff2d5ea2005-07-23 00:41:48 +0000429 sqlite3 *db = pParse->db;
430 int iDb;
431 int i;
432 char *z, *zDb;
433 Table *pTab;
434 Token *pTableName;
435
436 /* Read the database schema. If an error occurs, leave an error message
437 ** and code in pParse and return NULL. */
drh1fee73e2007-08-29 04:00:57 +0000438 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhff2d5ea2005-07-23 00:41:48 +0000439 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
440 return;
441 }
442
drh05800a12009-04-16 17:45:47 +0000443 assert( pName2!=0 || pName1==0 );
drhff2d5ea2005-07-23 00:41:48 +0000444 if( pName1==0 ){
445 /* Form 1: Analyze everything */
446 for(i=0; i<db->nDb; i++){
447 if( i==1 ) continue; /* Do not analyze the TEMP database */
448 analyzeDatabase(pParse, i);
449 }
drh05800a12009-04-16 17:45:47 +0000450 }else if( pName2->n==0 ){
drhff2d5ea2005-07-23 00:41:48 +0000451 /* Form 2: Analyze the database or table named */
452 iDb = sqlite3FindDb(db, pName1);
453 if( iDb>=0 ){
454 analyzeDatabase(pParse, iDb);
drhe6e04962005-07-23 02:17:03 +0000455 }else{
drh17435752007-08-16 04:30:38 +0000456 z = sqlite3NameFromToken(db, pName1);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000457 if( z ){
drhca424112008-01-25 15:04:48 +0000458 pTab = sqlite3LocateTable(pParse, 0, z, 0);
drh633e6d52008-07-28 19:34:53 +0000459 sqlite3DbFree(db, z);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000460 if( pTab ){
461 analyzeTable(pParse, pTab);
462 }
drhe6e04962005-07-23 02:17:03 +0000463 }
drhff2d5ea2005-07-23 00:41:48 +0000464 }
drhff2d5ea2005-07-23 00:41:48 +0000465 }else{
466 /* Form 3: Analyze the fully qualified table name */
467 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
468 if( iDb>=0 ){
469 zDb = db->aDb[iDb].zName;
drh17435752007-08-16 04:30:38 +0000470 z = sqlite3NameFromToken(db, pTableName);
drhcf1be452007-05-12 12:08:51 +0000471 if( z ){
drhca424112008-01-25 15:04:48 +0000472 pTab = sqlite3LocateTable(pParse, 0, z, zDb);
drh633e6d52008-07-28 19:34:53 +0000473 sqlite3DbFree(db, z);
drhcf1be452007-05-12 12:08:51 +0000474 if( pTab ){
475 analyzeTable(pParse, pTab);
476 }
drhff2d5ea2005-07-23 00:41:48 +0000477 }
478 }
479 }
drh9f18e8a2005-07-08 12:13:04 +0000480}
481
drh497e4462005-07-23 03:18:40 +0000482/*
483** Used to pass information from the analyzer reader through to the
484** callback routine.
485*/
486typedef struct analysisInfo analysisInfo;
487struct analysisInfo {
488 sqlite3 *db;
489 const char *zDatabase;
490};
491
492/*
493** This callback is invoked once for each index when reading the
494** sqlite_stat1 table.
495**
drh15564052010-09-25 22:32:56 +0000496** argv[0] = name of the table
497** argv[1] = name of the index (might be NULL)
498** argv[2] = results of analysis - on integer for each column
499**
500** Entries for which argv[1]==NULL simply record the number of rows in
501** the table.
drh497e4462005-07-23 03:18:40 +0000502*/
danielk197762c14b32008-11-19 09:05:26 +0000503static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
drh497e4462005-07-23 03:18:40 +0000504 analysisInfo *pInfo = (analysisInfo*)pData;
505 Index *pIndex;
drh15564052010-09-25 22:32:56 +0000506 Table *pTable;
507 int i, c, n;
drh497e4462005-07-23 03:18:40 +0000508 unsigned int v;
509 const char *z;
510
drh15564052010-09-25 22:32:56 +0000511 assert( argc==3 );
danielk1977f3d3c272008-11-19 16:52:44 +0000512 UNUSED_PARAMETER2(NotUsed, argc);
513
drh15564052010-09-25 22:32:56 +0000514 if( argv==0 || argv[0]==0 || argv[2]==0 ){
drh497e4462005-07-23 03:18:40 +0000515 return 0;
516 }
drh15564052010-09-25 22:32:56 +0000517 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
518 if( pTable==0 ){
drh497e4462005-07-23 03:18:40 +0000519 return 0;
520 }
drh15564052010-09-25 22:32:56 +0000521 if( argv[1] ){
522 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
523 }else{
524 pIndex = 0;
525 }
526 n = pIndex ? pIndex->nColumn : 0;
527 z = argv[2];
528 for(i=0; *z && i<=n; i++){
drh497e4462005-07-23 03:18:40 +0000529 v = 0;
530 while( (c=z[0])>='0' && c<='9' ){
531 v = v*10 + c - '0';
532 z++;
533 }
drh15564052010-09-25 22:32:56 +0000534 if( i==0 ) pTable->nRowEst = v;
535 if( pIndex==0 ) break;
drh497e4462005-07-23 03:18:40 +0000536 pIndex->aiRowEst[i] = v;
537 if( *z==' ' ) z++;
538 }
539 return 0;
540}
541
542/*
dan85c165c2009-08-19 14:34:54 +0000543** If the Index.aSample variable is not NULL, delete the aSample[] array
544** and its contents.
545*/
dand46def72010-07-24 11:28:28 +0000546void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
dan85c165c2009-08-19 14:34:54 +0000547#ifdef SQLITE_ENABLE_STAT2
548 if( pIdx->aSample ){
549 int j;
dan85c165c2009-08-19 14:34:54 +0000550 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
551 IndexSample *p = &pIdx->aSample[j];
552 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
dand46def72010-07-24 11:28:28 +0000553 sqlite3DbFree(db, p->u.z);
dan85c165c2009-08-19 14:34:54 +0000554 }
555 }
dand46def72010-07-24 11:28:28 +0000556 sqlite3DbFree(db, pIdx->aSample);
dan85c165c2009-08-19 14:34:54 +0000557 }
shanecea72b22009-09-07 04:38:36 +0000558#else
drh43b18e12010-08-17 19:40:08 +0000559 UNUSED_PARAMETER(db);
shanecea72b22009-09-07 04:38:36 +0000560 UNUSED_PARAMETER(pIdx);
dan85c165c2009-08-19 14:34:54 +0000561#endif
562}
563
564/*
565** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
566** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
567** arrays. The contents of sqlite_stat2 are used to populate the
568** Index.aSample[] arrays.
569**
570** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
571** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
572** during compilation and the sqlite_stat2 table is present, no data is
573** read from it.
574**
575** If SQLITE_ENABLE_STAT2 was defined during compilation and the
576** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
577** returned. However, in this case, data is read from the sqlite_stat1
578** table (if it is present) before returning.
579**
580** If an OOM error occurs, this function always sets db->mallocFailed.
581** This means if the caller does not care about other errors, the return
582** code may be ignored.
drh497e4462005-07-23 03:18:40 +0000583*/
drhcf1be452007-05-12 12:08:51 +0000584int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
drh497e4462005-07-23 03:18:40 +0000585 analysisInfo sInfo;
586 HashElem *i;
587 char *zSql;
drhcf1be452007-05-12 12:08:51 +0000588 int rc;
drh497e4462005-07-23 03:18:40 +0000589
drhff0587c2007-08-29 17:43:19 +0000590 assert( iDb>=0 && iDb<db->nDb );
591 assert( db->aDb[iDb].pBt!=0 );
592 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
drh1fee73e2007-08-29 04:00:57 +0000593
drh497e4462005-07-23 03:18:40 +0000594 /* Clear any prior statistics */
danielk1977da184232006-01-05 11:34:32 +0000595 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
drh497e4462005-07-23 03:18:40 +0000596 Index *pIdx = sqliteHashData(i);
drh51147ba2005-07-23 22:59:55 +0000597 sqlite3DefaultRowEst(pIdx);
dand46def72010-07-24 11:28:28 +0000598 sqlite3DeleteIndexSamples(db, pIdx);
599 pIdx->aSample = 0;
drh497e4462005-07-23 03:18:40 +0000600 }
601
dan85c165c2009-08-19 14:34:54 +0000602 /* Check to make sure the sqlite_stat1 table exists */
drh497e4462005-07-23 03:18:40 +0000603 sInfo.db = db;
604 sInfo.zDatabase = db->aDb[iDb].zName;
605 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
dan85c165c2009-08-19 14:34:54 +0000606 return SQLITE_ERROR;
drh497e4462005-07-23 03:18:40 +0000607 }
608
drh497e4462005-07-23 03:18:40 +0000609 /* Load new statistics out of the sqlite_stat1 table */
dan85c165c2009-08-19 14:34:54 +0000610 zSql = sqlite3MPrintf(db,
drh15564052010-09-25 22:32:56 +0000611 "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
drhf16ce3b2009-02-13 16:59:53 +0000612 if( zSql==0 ){
613 rc = SQLITE_NOMEM;
614 }else{
drhf16ce3b2009-02-13 16:59:53 +0000615 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
drhf16ce3b2009-02-13 16:59:53 +0000616 sqlite3DbFree(db, zSql);
drhf16ce3b2009-02-13 16:59:53 +0000617 }
dan02fa4692009-08-17 17:06:58 +0000618
dan85c165c2009-08-19 14:34:54 +0000619
dane275dc32009-08-18 16:24:58 +0000620 /* Load the statistics from the sqlite_stat2 table. */
dan69188d92009-08-19 08:18:32 +0000621#ifdef SQLITE_ENABLE_STAT2
dan85c165c2009-08-19 14:34:54 +0000622 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
623 rc = SQLITE_ERROR;
624 }
dan02fa4692009-08-17 17:06:58 +0000625 if( rc==SQLITE_OK ){
dane275dc32009-08-18 16:24:58 +0000626 sqlite3_stmt *pStmt = 0;
627
dan02fa4692009-08-17 17:06:58 +0000628 zSql = sqlite3MPrintf(db,
dan85c165c2009-08-19 14:34:54 +0000629 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
dane275dc32009-08-18 16:24:58 +0000630 if( !zSql ){
dan85c165c2009-08-19 14:34:54 +0000631 rc = SQLITE_NOMEM;
632 }else{
dan85c165c2009-08-19 14:34:54 +0000633 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
dan85c165c2009-08-19 14:34:54 +0000634 sqlite3DbFree(db, zSql);
dan02fa4692009-08-17 17:06:58 +0000635 }
dane275dc32009-08-18 16:24:58 +0000636
dane275dc32009-08-18 16:24:58 +0000637 if( rc==SQLITE_OK ){
638 while( sqlite3_step(pStmt)==SQLITE_ROW ){
drh65a0ce12011-01-04 20:06:33 +0000639 char *zIndex; /* Index name */
640 Index *pIdx; /* Pointer to the index object */
641
642 zIndex = (char *)sqlite3_column_text(pStmt, 0);
643 pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
dane275dc32009-08-18 16:24:58 +0000644 if( pIdx ){
645 int iSample = sqlite3_column_int(pStmt, 1);
646 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
647 int eType = sqlite3_column_type(pStmt, 2);
648
649 if( pIdx->aSample==0 ){
dana898aac2009-08-19 09:09:38 +0000650 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
drhb9755982010-07-24 16:34:37 +0000651 pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
dan69188d92009-08-19 08:18:32 +0000652 if( pIdx->aSample==0 ){
dan85c165c2009-08-19 14:34:54 +0000653 db->mallocFailed = 1;
dan69188d92009-08-19 08:18:32 +0000654 break;
655 }
dan1feeaed2010-07-23 15:41:47 +0000656 memset(pIdx->aSample, 0, sz);
dane275dc32009-08-18 16:24:58 +0000657 }
658
drh9e1fade2009-08-20 23:05:31 +0000659 assert( pIdx->aSample );
660 {
dane275dc32009-08-18 16:24:58 +0000661 IndexSample *pSample = &pIdx->aSample[iSample];
shanecea72b22009-09-07 04:38:36 +0000662 pSample->eType = (u8)eType;
dan69188d92009-08-19 08:18:32 +0000663 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
dane275dc32009-08-18 16:24:58 +0000664 pSample->u.r = sqlite3_column_double(pStmt, 2);
dan69188d92009-08-19 08:18:32 +0000665 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
dane275dc32009-08-18 16:24:58 +0000666 const char *z = (const char *)(
667 (eType==SQLITE_BLOB) ?
668 sqlite3_column_blob(pStmt, 2):
669 sqlite3_column_text(pStmt, 2)
670 );
671 int n = sqlite3_column_bytes(pStmt, 2);
672 if( n>24 ){
673 n = 24;
674 }
shanecea72b22009-09-07 04:38:36 +0000675 pSample->nByte = (u8)n;
shaneh1141ae22010-03-26 01:54:33 +0000676 if( n < 1){
677 pSample->u.z = 0;
dane275dc32009-08-18 16:24:58 +0000678 }else{
drh174b9a12010-07-26 11:07:20 +0000679 pSample->u.z = sqlite3DbStrNDup(0, z, n);
680 if( pSample->u.z==0 ){
shaneh1141ae22010-03-26 01:54:33 +0000681 db->mallocFailed = 1;
682 break;
683 }
dan69188d92009-08-19 08:18:32 +0000684 }
dane275dc32009-08-18 16:24:58 +0000685 }
686 }
687 }
688 }
689 }
690 rc = sqlite3_finalize(pStmt);
691 }
dan02fa4692009-08-17 17:06:58 +0000692 }
dan69188d92009-08-19 08:18:32 +0000693#endif
dan02fa4692009-08-17 17:06:58 +0000694
dane275dc32009-08-18 16:24:58 +0000695 if( rc==SQLITE_NOMEM ){
696 db->mallocFailed = 1;
697 }
drhcf1be452007-05-12 12:08:51 +0000698 return rc;
drh497e4462005-07-23 03:18:40 +0000699}
drh9f18e8a2005-07-08 12:13:04 +0000700
drhff2d5ea2005-07-23 00:41:48 +0000701
drh9f18e8a2005-07-08 12:13:04 +0000702#endif /* SQLITE_OMIT_ANALYZE */