blob: 31acbc381547242051a76de3a2c914f65f6ac57f [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){
dan69188d92009-08-19 08:18:32 +0000111 Index *pIdx; /* An index to being analyzed */
112 int iIdxCur; /* Cursor open on index being analyzed */
113 Vdbe *v; /* The virtual machine being built up */
114 int i; /* Loop counter */
115 int topOfLoop; /* The top of the loop */
116 int endOfLoop; /* The end of the loop */
117 int addr; /* The address of an instruction */
118 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 */
dan69188d92009-08-19 08:18:32 +0000126#ifdef SQLITE_ENABLE_STAT2
127 int regTemp2 = iMem++; /* Temporary use register */
128 int regSamplerecno = iMem++; /* Next sample index record number */
129 int regRecno = iMem++; /* Register next index record number */
dane275dc32009-08-18 16:24:58 +0000130 int regCount = iMem++; /* Total number of records in table */
dan69188d92009-08-19 08:18:32 +0000131#endif
dane275dc32009-08-18 16:24:58 +0000132
drhff2d5ea2005-07-23 00:41:48 +0000133 v = sqlite3GetVdbe(pParse);
drh05800a12009-04-16 17:45:47 +0000134 if( v==0 || NEVER(pTab==0) || pTab->pIndex==0 ){
drh0c356672005-09-10 22:40:53 +0000135 /* Do no analysis for tables that have no indices */
drhff2d5ea2005-07-23 00:41:48 +0000136 return;
137 }
drh1fee73e2007-08-29 04:00:57 +0000138 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000139 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
140 assert( iDb>=0 );
drhe6e04962005-07-23 02:17:03 +0000141#ifndef SQLITE_OMIT_AUTHORIZATION
142 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
danielk1977da184232006-01-05 11:34:32 +0000143 pParse->db->aDb[iDb].zName ) ){
drhe6e04962005-07-23 02:17:03 +0000144 return;
145 }
146#endif
147
danielk1977c00da102006-01-07 13:21:04 +0000148 /* Establish a read-lock on the table at the shared-cache level. */
149 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
150
danielk19776ab3a2e2009-02-19 14:39:25 +0000151 iIdxCur = pParse->nTab++;
drhff2d5ea2005-07-23 00:41:48 +0000152 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
dane275dc32009-08-18 16:24:58 +0000153 int nCol = pIdx->nColumn;
danielk1977b3bf5562006-01-10 17:58:23 +0000154 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
155
dane275dc32009-08-18 16:24:58 +0000156 if( iMem+1+(nCol*2)>pParse->nMem ){
157 pParse->nMem = iMem+1+(nCol*2);
158 }
159
160 /* Open a cursor to the index to be analyzed. */
danielk1977da184232006-01-05 11:34:32 +0000161 assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
danielk1977207872a2008-01-03 07:54:23 +0000162 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000163 (char *)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +0000164 VdbeComment((v, "%s", pIdx->zName));
drhff2d5ea2005-07-23 00:41:48 +0000165
dan69188d92009-08-19 08:18:32 +0000166 /* Populate the registers containing the table and index names. */
167 if( pTab->pIndex==pIdx ){
168 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
169 }
170 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
171
172#ifdef SQLITE_ENABLE_STAT2
dane275dc32009-08-18 16:24:58 +0000173 /* If this iteration of the loop is generating code to analyze the
174 ** first index in the pTab->pIndex list, then register regCount has
175 ** not been populated. In this case populate it now. */
dan02fa4692009-08-17 17:06:58 +0000176 if( pTab->pIndex==pIdx ){
dane275dc32009-08-18 16:24:58 +0000177 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
dan02fa4692009-08-17 17:06:58 +0000178 }
dane275dc32009-08-18 16:24:58 +0000179
180 /* Zero the regSampleno and regRecno registers. */
181 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
182 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
183
184 /* If there are less than INDEX_SAMPLES records in the index, then
185 ** set the contents of regSampleRecno to integer value INDEX_SAMPLES.
186 ** Otherwise, set it to zero. This is to ensure that if there are
187 ** less than the said number of entries in the index, no samples at
188 ** all are collected. */
189 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
190 sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, sqlite3VdbeCurrentAddr(v)+2,
191 regCount);
192 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSamplerecno);
dan69188d92009-08-19 08:18:32 +0000193#endif
dan02fa4692009-08-17 17:06:58 +0000194
dan85c165c2009-08-19 14:34:54 +0000195 /* The block of memory cells initialized here is used as follows.
drhff2d5ea2005-07-23 00:41:48 +0000196 **
dan85c165c2009-08-19 14:34:54 +0000197 ** iMem:
198 ** The total number of rows in the table.
dan02fa4692009-08-17 17:06:58 +0000199 **
dan85c165c2009-08-19 14:34:54 +0000200 ** iMem+1 .. iMem+nCol:
201 ** Number of distinct entries in index considering the
202 ** left-most N columns only, where N is between 1 and nCol,
203 ** inclusive.
dan02fa4692009-08-17 17:06:58 +0000204 **
dan85c165c2009-08-19 14:34:54 +0000205 ** iMem+nCol+1 .. Mem+2*nCol:
206 ** Previous value of indexed columns, from left to right.
dan02fa4692009-08-17 17:06:58 +0000207 **
dan85c165c2009-08-19 14:34:54 +0000208 ** Cells iMem through iMem+nCol are initialized to 0. The others are
209 ** initialized to contain an SQL NULL.
drhff2d5ea2005-07-23 00:41:48 +0000210 */
drhff2d5ea2005-07-23 00:41:48 +0000211 for(i=0; i<=nCol; i++){
drh4c583122008-01-04 22:01:03 +0000212 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
drhff2d5ea2005-07-23 00:41:48 +0000213 }
drhff2d5ea2005-07-23 00:41:48 +0000214 for(i=0; i<nCol; i++){
drh4c583122008-01-04 22:01:03 +0000215 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000216 }
217
dane275dc32009-08-18 16:24:58 +0000218 /* Start the analysis loop. This loop runs through all the entries in
dan02fa4692009-08-17 17:06:58 +0000219 ** the index b-tree. */
drhff2d5ea2005-07-23 00:41:48 +0000220 endOfLoop = sqlite3VdbeMakeLabel(v);
drh66a51672008-01-03 00:01:23 +0000221 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
drhe6e04962005-07-23 02:17:03 +0000222 topOfLoop = sqlite3VdbeCurrentAddr(v);
drh8558cde2008-01-05 05:20:10 +0000223 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
dan02fa4692009-08-17 17:06:58 +0000224
drhff2d5ea2005-07-23 00:41:48 +0000225 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000226 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
dan69188d92009-08-19 08:18:32 +0000227#ifdef SQLITE_ENABLE_STAT2
dan02fa4692009-08-17 17:06:58 +0000228 if( i==0 ){
dane275dc32009-08-18 16:24:58 +0000229 /* Check if the record that cursor iIdxCur points to contains a
230 ** value that should be stored in the sqlite_stat2 table. If so,
231 ** store it. */
232 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
233 assert( regTabname+1==regIdxname
234 && regTabname+2==regSampleno
235 && regTabname+3==regCol
236 );
237 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
238 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
239 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
240
241 /* Calculate new values for regSamplerecno and regSampleno.
242 **
243 ** sampleno = sampleno + 1
244 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
245 */
246 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
247 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regCount, regTemp);
248 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
249 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
250 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
251 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
252 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
253
254 sqlite3VdbeJumpHere(v, ne);
255 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
dan02fa4692009-08-17 17:06:58 +0000256 }
dan69188d92009-08-19 08:18:32 +0000257#endif
258
drh2d401ab2008-01-10 23:50:11 +0000259 sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
260 /**** TODO: add collating sequence *****/
drh35573352008-01-08 23:54:25 +0000261 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
drhff2d5ea2005-07-23 00:41:48 +0000262 }
drh66a51672008-01-03 00:01:23 +0000263 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
drhff2d5ea2005-07-23 00:41:48 +0000264 for(i=0; i<nCol; i++){
dan85c165c2009-08-19 14:34:54 +0000265 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-(nCol*2));
drh2d401ab2008-01-10 23:50:11 +0000266 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
drhb1fdb2a2008-01-05 04:06:03 +0000267 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
drhff2d5ea2005-07-23 00:41:48 +0000268 }
dan02fa4692009-08-17 17:06:58 +0000269
270 /* End of the analysis loop. */
drhff2d5ea2005-07-23 00:41:48 +0000271 sqlite3VdbeResolveLabel(v, endOfLoop);
drh66a51672008-01-03 00:01:23 +0000272 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
273 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
drhff2d5ea2005-07-23 00:41:48 +0000274
dan02fa4692009-08-17 17:06:58 +0000275 /* Store the results in sqlite_stat1.
drhff2d5ea2005-07-23 00:41:48 +0000276 **
drh4c103832007-06-20 13:37:31 +0000277 ** The result is a single row of the sqlite_stat1 table. The first
drhff2d5ea2005-07-23 00:41:48 +0000278 ** two columns are the names of the table and index. The third column
279 ** is a string composed of a list of integer statistics about the
danielk19772f886d12009-02-28 10:47:41 +0000280 ** index. The first integer in the list is the total number of entries
drh17a18f22005-07-23 14:52:12 +0000281 ** in the index. There is one additional integer in the list for each
282 ** column of the table. This additional integer is a guess of how many
283 ** rows of the table the index will select. If D is the count of distinct
284 ** values and K is the total number of rows, then the integer is computed
285 ** as:
drhff2d5ea2005-07-23 00:41:48 +0000286 **
287 ** I = (K+D-1)/D
288 **
289 ** If K==0 then no entry is made into the sqlite_stat1 table.
290 ** If K>0 then it is always the case the D>0 so division by zero
291 ** is never possible.
292 */
drh2d401ab2008-01-10 23:50:11 +0000293 addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
dane275dc32009-08-18 16:24:58 +0000294 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
drhff2d5ea2005-07-23 00:41:48 +0000295 for(i=0; i<nCol; i++){
drh2d401ab2008-01-10 23:50:11 +0000296 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
dane275dc32009-08-18 16:24:58 +0000297 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drh2d401ab2008-01-10 23:50:11 +0000298 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
299 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
300 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
301 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
dane275dc32009-08-18 16:24:58 +0000302 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
drhff2d5ea2005-07-23 00:41:48 +0000303 }
dane275dc32009-08-18 16:24:58 +0000304 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
drh2d401ab2008-01-10 23:50:11 +0000305 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
306 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
307 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drhd654be82005-09-20 17:42:23 +0000308 sqlite3VdbeJumpHere(v, addr);
drhff2d5ea2005-07-23 00:41:48 +0000309 }
310}
311
312/*
drh497e4462005-07-23 03:18:40 +0000313** Generate code that will cause the most recent index analysis to
314** be laoded into internal hash tables where is can be used.
315*/
316static void loadAnalysis(Parse *pParse, int iDb){
317 Vdbe *v = sqlite3GetVdbe(pParse);
drhcf1be452007-05-12 12:08:51 +0000318 if( v ){
drh66a51672008-01-03 00:01:23 +0000319 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
drhcf1be452007-05-12 12:08:51 +0000320 }
drh497e4462005-07-23 03:18:40 +0000321}
322
323/*
drhff2d5ea2005-07-23 00:41:48 +0000324** Generate code that will do an analysis of an entire database
325*/
326static void analyzeDatabase(Parse *pParse, int iDb){
327 sqlite3 *db = pParse->db;
danielk1977e501b892006-01-09 06:29:47 +0000328 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
drhff2d5ea2005-07-23 00:41:48 +0000329 HashElem *k;
330 int iStatCur;
331 int iMem;
332
333 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000334 iStatCur = pParse->nTab;
335 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000336 openStatTable(pParse, iDb, iStatCur, 0);
drh0a07c102008-01-03 18:03:08 +0000337 iMem = pParse->nMem+1;
danielk1977da184232006-01-05 11:34:32 +0000338 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
drhff2d5ea2005-07-23 00:41:48 +0000339 Table *pTab = (Table*)sqliteHashData(k);
340 analyzeOneTable(pParse, pTab, iStatCur, iMem);
341 }
drh497e4462005-07-23 03:18:40 +0000342 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000343}
344
345/*
346** Generate code that will do an analysis of a single table in
347** a database.
348*/
349static void analyzeTable(Parse *pParse, Table *pTab){
350 int iDb;
351 int iStatCur;
352
353 assert( pTab!=0 );
drh1fee73e2007-08-29 04:00:57 +0000354 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000355 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhff2d5ea2005-07-23 00:41:48 +0000356 sqlite3BeginWriteOperation(pParse, 0, iDb);
dan02fa4692009-08-17 17:06:58 +0000357 iStatCur = pParse->nTab;
358 pParse->nTab += 2;
drhff2d5ea2005-07-23 00:41:48 +0000359 openStatTable(pParse, iDb, iStatCur, pTab->zName);
drh0a07c102008-01-03 18:03:08 +0000360 analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
drh497e4462005-07-23 03:18:40 +0000361 loadAnalysis(pParse, iDb);
drhff2d5ea2005-07-23 00:41:48 +0000362}
363
364/*
365** Generate code for the ANALYZE command. The parser calls this routine
366** when it recognizes an ANALYZE command.
drh9f18e8a2005-07-08 12:13:04 +0000367**
368** ANALYZE -- 1
drhff2d5ea2005-07-23 00:41:48 +0000369** ANALYZE <database> -- 2
drh9f18e8a2005-07-08 12:13:04 +0000370** ANALYZE ?<database>.?<tablename> -- 3
371**
372** Form 1 causes all indices in all attached databases to be analyzed.
373** Form 2 analyzes all indices the single database named.
374** Form 3 analyzes all indices associated with the named table.
375*/
376void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
drhff2d5ea2005-07-23 00:41:48 +0000377 sqlite3 *db = pParse->db;
378 int iDb;
379 int i;
380 char *z, *zDb;
381 Table *pTab;
382 Token *pTableName;
383
384 /* Read the database schema. If an error occurs, leave an error message
385 ** and code in pParse and return NULL. */
drh1fee73e2007-08-29 04:00:57 +0000386 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhff2d5ea2005-07-23 00:41:48 +0000387 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
388 return;
389 }
390
drh05800a12009-04-16 17:45:47 +0000391 assert( pName2!=0 || pName1==0 );
drhff2d5ea2005-07-23 00:41:48 +0000392 if( pName1==0 ){
393 /* Form 1: Analyze everything */
394 for(i=0; i<db->nDb; i++){
395 if( i==1 ) continue; /* Do not analyze the TEMP database */
396 analyzeDatabase(pParse, i);
397 }
drh05800a12009-04-16 17:45:47 +0000398 }else if( pName2->n==0 ){
drhff2d5ea2005-07-23 00:41:48 +0000399 /* Form 2: Analyze the database or table named */
400 iDb = sqlite3FindDb(db, pName1);
401 if( iDb>=0 ){
402 analyzeDatabase(pParse, iDb);
drhe6e04962005-07-23 02:17:03 +0000403 }else{
drh17435752007-08-16 04:30:38 +0000404 z = sqlite3NameFromToken(db, pName1);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000405 if( z ){
drhca424112008-01-25 15:04:48 +0000406 pTab = sqlite3LocateTable(pParse, 0, z, 0);
drh633e6d52008-07-28 19:34:53 +0000407 sqlite3DbFree(db, z);
danielk1977b8b4bfa2007-11-15 13:10:22 +0000408 if( pTab ){
409 analyzeTable(pParse, pTab);
410 }
drhe6e04962005-07-23 02:17:03 +0000411 }
drhff2d5ea2005-07-23 00:41:48 +0000412 }
drhff2d5ea2005-07-23 00:41:48 +0000413 }else{
414 /* Form 3: Analyze the fully qualified table name */
415 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
416 if( iDb>=0 ){
417 zDb = db->aDb[iDb].zName;
drh17435752007-08-16 04:30:38 +0000418 z = sqlite3NameFromToken(db, pTableName);
drhcf1be452007-05-12 12:08:51 +0000419 if( z ){
drhca424112008-01-25 15:04:48 +0000420 pTab = sqlite3LocateTable(pParse, 0, z, zDb);
drh633e6d52008-07-28 19:34:53 +0000421 sqlite3DbFree(db, z);
drhcf1be452007-05-12 12:08:51 +0000422 if( pTab ){
423 analyzeTable(pParse, pTab);
424 }
drhff2d5ea2005-07-23 00:41:48 +0000425 }
426 }
427 }
drh9f18e8a2005-07-08 12:13:04 +0000428}
429
drh497e4462005-07-23 03:18:40 +0000430/*
431** Used to pass information from the analyzer reader through to the
432** callback routine.
433*/
434typedef struct analysisInfo analysisInfo;
435struct analysisInfo {
436 sqlite3 *db;
437 const char *zDatabase;
438};
439
440/*
441** This callback is invoked once for each index when reading the
442** sqlite_stat1 table.
443**
444** argv[0] = name of the index
445** argv[1] = results of analysis - on integer for each column
446*/
danielk197762c14b32008-11-19 09:05:26 +0000447static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
drh497e4462005-07-23 03:18:40 +0000448 analysisInfo *pInfo = (analysisInfo*)pData;
449 Index *pIndex;
450 int i, c;
451 unsigned int v;
452 const char *z;
453
454 assert( argc==2 );
danielk1977f3d3c272008-11-19 16:52:44 +0000455 UNUSED_PARAMETER2(NotUsed, argc);
456
drh1ec43c92005-09-06 10:26:47 +0000457 if( argv==0 || argv[0]==0 || argv[1]==0 ){
drh497e4462005-07-23 03:18:40 +0000458 return 0;
459 }
460 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
461 if( pIndex==0 ){
462 return 0;
463 }
464 z = argv[1];
drh17a18f22005-07-23 14:52:12 +0000465 for(i=0; *z && i<=pIndex->nColumn; i++){
drh497e4462005-07-23 03:18:40 +0000466 v = 0;
467 while( (c=z[0])>='0' && c<='9' ){
468 v = v*10 + c - '0';
469 z++;
470 }
471 pIndex->aiRowEst[i] = v;
472 if( *z==' ' ) z++;
473 }
474 return 0;
475}
476
477/*
dan85c165c2009-08-19 14:34:54 +0000478** If the Index.aSample variable is not NULL, delete the aSample[] array
479** and its contents.
480*/
481void sqlite3DeleteIndexSamples(Index *pIdx){
482#ifdef SQLITE_ENABLE_STAT2
483 if( pIdx->aSample ){
484 int j;
485 sqlite3 *dbMem = pIdx->pTable->dbMem;
486 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
487 IndexSample *p = &pIdx->aSample[j];
488 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
489 sqlite3DbFree(pIdx->pTable->dbMem, p->u.z);
490 }
491 }
492 sqlite3DbFree(dbMem, pIdx->aSample);
493 pIdx->aSample = 0;
494 }
495#endif
496}
497
498/*
499** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
500** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
501** arrays. The contents of sqlite_stat2 are used to populate the
502** Index.aSample[] arrays.
503**
504** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
505** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
506** during compilation and the sqlite_stat2 table is present, no data is
507** read from it.
508**
509** If SQLITE_ENABLE_STAT2 was defined during compilation and the
510** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
511** returned. However, in this case, data is read from the sqlite_stat1
512** table (if it is present) before returning.
513**
514** If an OOM error occurs, this function always sets db->mallocFailed.
515** This means if the caller does not care about other errors, the return
516** code may be ignored.
drh497e4462005-07-23 03:18:40 +0000517*/
drhcf1be452007-05-12 12:08:51 +0000518int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
drh497e4462005-07-23 03:18:40 +0000519 analysisInfo sInfo;
520 HashElem *i;
521 char *zSql;
drhcf1be452007-05-12 12:08:51 +0000522 int rc;
drh497e4462005-07-23 03:18:40 +0000523
drhff0587c2007-08-29 17:43:19 +0000524 assert( iDb>=0 && iDb<db->nDb );
525 assert( db->aDb[iDb].pBt!=0 );
526 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
drh1fee73e2007-08-29 04:00:57 +0000527
drh497e4462005-07-23 03:18:40 +0000528 /* Clear any prior statistics */
danielk1977da184232006-01-05 11:34:32 +0000529 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
drh497e4462005-07-23 03:18:40 +0000530 Index *pIdx = sqliteHashData(i);
drh51147ba2005-07-23 22:59:55 +0000531 sqlite3DefaultRowEst(pIdx);
dan85c165c2009-08-19 14:34:54 +0000532 sqlite3DeleteIndexSamples(pIdx);
drh497e4462005-07-23 03:18:40 +0000533 }
534
dan85c165c2009-08-19 14:34:54 +0000535 /* Check to make sure the sqlite_stat1 table exists */
drh497e4462005-07-23 03:18:40 +0000536 sInfo.db = db;
537 sInfo.zDatabase = db->aDb[iDb].zName;
538 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
dan85c165c2009-08-19 14:34:54 +0000539 return SQLITE_ERROR;
drh497e4462005-07-23 03:18:40 +0000540 }
541
drh497e4462005-07-23 03:18:40 +0000542 /* Load new statistics out of the sqlite_stat1 table */
dan85c165c2009-08-19 14:34:54 +0000543 zSql = sqlite3MPrintf(db,
544 "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
drhf16ce3b2009-02-13 16:59:53 +0000545 if( zSql==0 ){
546 rc = SQLITE_NOMEM;
547 }else{
548 (void)sqlite3SafetyOff(db);
549 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
550 (void)sqlite3SafetyOn(db);
551 sqlite3DbFree(db, zSql);
drhf16ce3b2009-02-13 16:59:53 +0000552 }
dan02fa4692009-08-17 17:06:58 +0000553
dan85c165c2009-08-19 14:34:54 +0000554
dane275dc32009-08-18 16:24:58 +0000555 /* Load the statistics from the sqlite_stat2 table. */
dan69188d92009-08-19 08:18:32 +0000556#ifdef SQLITE_ENABLE_STAT2
dan85c165c2009-08-19 14:34:54 +0000557 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
558 rc = SQLITE_ERROR;
559 }
dan02fa4692009-08-17 17:06:58 +0000560 if( rc==SQLITE_OK ){
dane275dc32009-08-18 16:24:58 +0000561 sqlite3_stmt *pStmt = 0;
562
dan02fa4692009-08-17 17:06:58 +0000563 zSql = sqlite3MPrintf(db,
dan85c165c2009-08-19 14:34:54 +0000564 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
dane275dc32009-08-18 16:24:58 +0000565 if( !zSql ){
dan85c165c2009-08-19 14:34:54 +0000566 rc = SQLITE_NOMEM;
567 }else{
568 (void)sqlite3SafetyOff(db);
569 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
570 (void)sqlite3SafetyOn(db);
571 sqlite3DbFree(db, zSql);
dan02fa4692009-08-17 17:06:58 +0000572 }
dane275dc32009-08-18 16:24:58 +0000573
dane275dc32009-08-18 16:24:58 +0000574 if( rc==SQLITE_OK ){
dan85c165c2009-08-19 14:34:54 +0000575 (void)sqlite3SafetyOff(db);
dane275dc32009-08-18 16:24:58 +0000576 while( sqlite3_step(pStmt)==SQLITE_ROW ){
577 char *zIndex = (char *)sqlite3_column_text(pStmt, 0);
578 Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase);
579 if( pIdx ){
580 int iSample = sqlite3_column_int(pStmt, 1);
dana898aac2009-08-19 09:09:38 +0000581 sqlite3 *dbMem = pIdx->pTable->dbMem;
dan85c165c2009-08-19 14:34:54 +0000582 assert( dbMem==db || dbMem==0 );
dane275dc32009-08-18 16:24:58 +0000583 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
584 int eType = sqlite3_column_type(pStmt, 2);
585
586 if( pIdx->aSample==0 ){
dana898aac2009-08-19 09:09:38 +0000587 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
588 pIdx->aSample = (IndexSample *)sqlite3DbMallocZero(dbMem, sz);
dan69188d92009-08-19 08:18:32 +0000589 if( pIdx->aSample==0 ){
dan85c165c2009-08-19 14:34:54 +0000590 db->mallocFailed = 1;
dan69188d92009-08-19 08:18:32 +0000591 break;
592 }
dane275dc32009-08-18 16:24:58 +0000593 }
594
595 if( pIdx->aSample ){
596 IndexSample *pSample = &pIdx->aSample[iSample];
dan69188d92009-08-19 08:18:32 +0000597 pSample->eType = eType;
598 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
dane275dc32009-08-18 16:24:58 +0000599 pSample->u.r = sqlite3_column_double(pStmt, 2);
dan69188d92009-08-19 08:18:32 +0000600 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
dane275dc32009-08-18 16:24:58 +0000601 const char *z = (const char *)(
602 (eType==SQLITE_BLOB) ?
603 sqlite3_column_blob(pStmt, 2):
604 sqlite3_column_text(pStmt, 2)
605 );
606 int n = sqlite3_column_bytes(pStmt, 2);
607 if( n>24 ){
608 n = 24;
609 }
610 pSample->nByte = n;
dana898aac2009-08-19 09:09:38 +0000611 pSample->u.z = sqlite3DbMallocRaw(dbMem, n);
dane275dc32009-08-18 16:24:58 +0000612 if( pSample->u.z ){
613 memcpy(pSample->u.z, z, n);
614 }else{
dan85c165c2009-08-19 14:34:54 +0000615 db->mallocFailed = 1;
dan69188d92009-08-19 08:18:32 +0000616 break;
617 }
dane275dc32009-08-18 16:24:58 +0000618 }
619 }
620 }
621 }
622 }
623 rc = sqlite3_finalize(pStmt);
dan85c165c2009-08-19 14:34:54 +0000624 (void)sqlite3SafetyOn(db);
dane275dc32009-08-18 16:24:58 +0000625 }
dan02fa4692009-08-17 17:06:58 +0000626 }
dan69188d92009-08-19 08:18:32 +0000627#endif
dan02fa4692009-08-17 17:06:58 +0000628
dane275dc32009-08-18 16:24:58 +0000629 if( rc==SQLITE_NOMEM ){
630 db->mallocFailed = 1;
631 }
drhcf1be452007-05-12 12:08:51 +0000632 return rc;
drh497e4462005-07-23 03:18:40 +0000633}
drh9f18e8a2005-07-08 12:13:04 +0000634
drhff2d5ea2005-07-23 00:41:48 +0000635
drh9f18e8a2005-07-08 12:13:04 +0000636#endif /* SQLITE_OMIT_ANALYZE */