ds@1952: /* ds@1952: ** 2005 July 8 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains code associated with the ANALYZE command. ds@1952: ** ds@1952: ** @(#) $Id$ ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_ANALYZE ds@1952: #include "sqliteInt.h" ds@1952: ds@1952: /* ds@1952: ** This routine generates code that opens the sqlite_stat1 table on cursor ds@1952: ** iStatCur. ds@1952: ** ds@1952: ** If the sqlite_stat1 tables does not previously exist, it is created. ds@1952: ** If it does previously exist, all entires associated with table zWhere ds@1952: ** are removed. If zWhere==0 then all entries are removed. ds@1952: */ ds@1952: static void openStatTable( ds@1952: Parse *pParse, /* Parsing context */ ds@1952: int iDb, /* The database we are looking in */ ds@1952: int iStatCur, /* Open the sqlite_stat1 table on this cursor */ ds@1952: const char *zWhere /* Delete entries associated with this table */ ds@1952: ){ ds@1952: sqlite3 *db = pParse->db; ds@1952: Db *pDb; ds@1952: int iRootPage; ds@1952: Table *pStat; ds@1952: Vdbe *v = sqlite3GetVdbe(pParse); ds@1952: ds@1952: if( v==0 ) return; ds@1952: assert( sqlite3BtreeHoldsAllMutexes(db) ); ds@1952: assert( sqlite3VdbeDb(v)==db ); ds@1952: pDb = &db->aDb[iDb]; ds@1952: if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){ ds@1952: /* The sqlite_stat1 tables does not exist. Create it. ds@1952: ** Note that a side-effect of the CREATE TABLE statement is to leave ds@1952: ** the rootpage of the new table on the top of the stack. This is ds@1952: ** important because the OpenWrite opcode below will be needing it. */ ds@1952: sqlite3NestedParse(pParse, ds@1952: "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)", ds@1952: pDb->zName ds@1952: ); ds@1952: iRootPage = 0; /* Cause rootpage to be taken from top of stack */ ds@1952: }else if( zWhere ){ ds@1952: /* The sqlite_stat1 table exists. Delete all entries associated with ds@1952: ** the table zWhere. */ ds@1952: sqlite3NestedParse(pParse, ds@1952: "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", ds@1952: pDb->zName, zWhere ds@1952: ); ds@1952: iRootPage = pStat->tnum; ds@1952: }else{ ds@1952: /* The sqlite_stat1 table already exists. Delete all rows. */ ds@1952: iRootPage = pStat->tnum; ds@1952: sqlite3VdbeAddOp(v, OP_Clear, pStat->tnum, iDb); ds@1952: } ds@1952: ds@1952: /* Open the sqlite_stat1 table for writing. Unless it was created ds@1952: ** by this vdbe program, lock it for writing at the shared-cache level. ds@1952: ** If this vdbe did create the sqlite_stat1 table, then it must have ds@1952: ** already obtained a schema-lock, making the write-lock redundant. ds@1952: */ ds@1952: if( iRootPage>0 ){ ds@1952: sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1"); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); ds@1952: sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage); ds@1952: sqlite3VdbeAddOp(v, OP_SetNumColumns, iStatCur, 3); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code to do an analysis of all indices associated with ds@1952: ** a single table. ds@1952: */ ds@1952: static void analyzeOneTable( ds@1952: Parse *pParse, /* Parser context */ ds@1952: Table *pTab, /* Table whose indices are to be analyzed */ ds@1952: int iStatCur, /* Cursor that writes to the sqlite_stat1 table */ ds@1952: int iMem /* Available memory locations begin here */ ds@1952: ){ ds@1952: Index *pIdx; /* An index to being analyzed */ ds@1952: int iIdxCur; /* Cursor number for index being analyzed */ ds@1952: int nCol; /* Number of columns in the index */ ds@1952: Vdbe *v; /* The virtual machine being built up */ ds@1952: int i; /* Loop counter */ ds@1952: int topOfLoop; /* The top of the loop */ ds@1952: int endOfLoop; /* The end of the loop */ ds@1952: int addr; /* The address of an instruction */ ds@1952: int iDb; /* Index of database containing pTab */ ds@1952: ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: if( v==0 || pTab==0 || pTab->pIndex==0 ){ ds@1952: /* Do no analysis for tables that have no indices */ ds@1952: return; ds@1952: } ds@1952: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); ds@1952: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); ds@1952: assert( iDb>=0 ); ds@1952: #ifndef SQLITE_OMIT_AUTHORIZATION ds@1952: if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, ds@1952: pParse->db->aDb[iDb].zName ) ){ ds@1952: return; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* Establish a read-lock on the table at the shared-cache level. */ ds@1952: sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); ds@1952: ds@1952: iIdxCur = pParse->nTab; ds@1952: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ ds@1952: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); ds@1952: ds@1952: /* Open a cursor to the index to be analyzed ds@1952: */ ds@1952: assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) ); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); ds@1952: VdbeComment((v, "# %s", pIdx->zName)); ds@1952: sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, ds@1952: (char *)pKey, P3_KEYINFO_HANDOFF); ds@1952: nCol = pIdx->nColumn; ds@1952: if( iMem+nCol*2>=pParse->nMem ){ ds@1952: pParse->nMem = iMem+nCol*2+1; ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, nCol+1); ds@1952: ds@1952: /* Memory cells are used as follows: ds@1952: ** ds@1952: ** mem[iMem]: The total number of rows in the table. ds@1952: ** mem[iMem+1]: Number of distinct values in column 1 ds@1952: ** ... ds@1952: ** mem[iMem+nCol]: Number of distinct values in column N ds@1952: ** mem[iMem+nCol+1] Last observed value of column 1 ds@1952: ** ... ds@1952: ** mem[iMem+nCol+nCol]: Last observed value of column N ds@1952: ** ds@1952: ** Cells iMem through iMem+nCol are initialized to 0. The others ds@1952: ** are initialized to NULL. ds@1952: */ ds@1952: for(i=0; i<=nCol; i++){ ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i); ds@1952: } ds@1952: for(i=0; i0 then it is always the case the D>0 so division by zero ds@1952: ** is never possible. ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); ds@1952: addr = sqlite3VdbeAddOp(v, OP_IfNot, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_NewRowid, iStatCur, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); ds@1952: sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0); ds@1952: for(i=0; idb; ds@1952: Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ ds@1952: HashElem *k; ds@1952: int iStatCur; ds@1952: int iMem; ds@1952: ds@1952: sqlite3BeginWriteOperation(pParse, 0, iDb); ds@1952: iStatCur = pParse->nTab++; ds@1952: openStatTable(pParse, iDb, iStatCur, 0); ds@1952: iMem = pParse->nMem; ds@1952: for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ ds@1952: Table *pTab = (Table*)sqliteHashData(k); ds@1952: analyzeOneTable(pParse, pTab, iStatCur, iMem); ds@1952: } ds@1952: loadAnalysis(pParse, iDb); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code that will do an analysis of a single table in ds@1952: ** a database. ds@1952: */ ds@1952: static void analyzeTable(Parse *pParse, Table *pTab){ ds@1952: int iDb; ds@1952: int iStatCur; ds@1952: ds@1952: assert( pTab!=0 ); ds@1952: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); ds@1952: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); ds@1952: sqlite3BeginWriteOperation(pParse, 0, iDb); ds@1952: iStatCur = pParse->nTab++; ds@1952: openStatTable(pParse, iDb, iStatCur, pTab->zName); ds@1952: analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem); ds@1952: loadAnalysis(pParse, iDb); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code for the ANALYZE command. The parser calls this routine ds@1952: ** when it recognizes an ANALYZE command. ds@1952: ** ds@1952: ** ANALYZE -- 1 ds@1952: ** ANALYZE -- 2 ds@1952: ** ANALYZE ?.? -- 3 ds@1952: ** ds@1952: ** Form 1 causes all indices in all attached databases to be analyzed. ds@1952: ** Form 2 analyzes all indices the single database named. ds@1952: ** Form 3 analyzes all indices associated with the named table. ds@1952: */ ds@1952: void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ ds@1952: sqlite3 *db = pParse->db; ds@1952: int iDb; ds@1952: int i; ds@1952: char *z, *zDb; ds@1952: Table *pTab; ds@1952: Token *pTableName; ds@1952: ds@1952: /* Read the database schema. If an error occurs, leave an error message ds@1952: ** and code in pParse and return NULL. */ ds@1952: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); ds@1952: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ ds@1952: return; ds@1952: } ds@1952: ds@1952: if( pName1==0 ){ ds@1952: /* Form 1: Analyze everything */ ds@1952: for(i=0; inDb; i++){ ds@1952: if( i==1 ) continue; /* Do not analyze the TEMP database */ ds@1952: analyzeDatabase(pParse, i); ds@1952: } ds@1952: }else if( pName2==0 || pName2->n==0 ){ ds@1952: /* Form 2: Analyze the database or table named */ ds@1952: iDb = sqlite3FindDb(db, pName1); ds@1952: if( iDb>=0 ){ ds@1952: analyzeDatabase(pParse, iDb); ds@1952: }else{ ds@1952: z = sqlite3NameFromToken(db, pName1); ds@1952: pTab = sqlite3LocateTable(pParse, z, 0); ds@1952: sqlite3_free(z); ds@1952: if( pTab ){ ds@1952: analyzeTable(pParse, pTab); ds@1952: } ds@1952: } ds@1952: }else{ ds@1952: /* Form 3: Analyze the fully qualified table name */ ds@1952: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); ds@1952: if( iDb>=0 ){ ds@1952: zDb = db->aDb[iDb].zName; ds@1952: z = sqlite3NameFromToken(db, pTableName); ds@1952: if( z ){ ds@1952: pTab = sqlite3LocateTable(pParse, z, zDb); ds@1952: sqlite3_free(z); ds@1952: if( pTab ){ ds@1952: analyzeTable(pParse, pTab); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Used to pass information from the analyzer reader through to the ds@1952: ** callback routine. ds@1952: */ ds@1952: typedef struct analysisInfo analysisInfo; ds@1952: struct analysisInfo { ds@1952: sqlite3 *db; ds@1952: const char *zDatabase; ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** This callback is invoked once for each index when reading the ds@1952: ** sqlite_stat1 table. ds@1952: ** ds@1952: ** argv[0] = name of the index ds@1952: ** argv[1] = results of analysis - on integer for each column ds@1952: */ ds@1952: static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){ ds@1952: analysisInfo *pInfo = (analysisInfo*)pData; ds@1952: Index *pIndex; ds@1952: int i, c; ds@1952: unsigned int v; ds@1952: const char *z; ds@1952: ds@1952: assert( argc==2 ); ds@1952: if( argv==0 || argv[0]==0 || argv[1]==0 ){ ds@1952: return 0; ds@1952: } ds@1952: pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase); ds@1952: if( pIndex==0 ){ ds@1952: return 0; ds@1952: } ds@1952: z = argv[1]; ds@1952: for(i=0; *z && i<=pIndex->nColumn; i++){ ds@1952: v = 0; ds@1952: while( (c=z[0])>='0' && c<='9' ){ ds@1952: v = v*10 + c - '0'; ds@1952: z++; ds@1952: } ds@1952: pIndex->aiRowEst[i] = v; ds@1952: if( *z==' ' ) z++; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Load the content of the sqlite_stat1 table into the index hash tables. ds@1952: */ ds@1952: int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ ds@1952: analysisInfo sInfo; ds@1952: HashElem *i; ds@1952: char *zSql; ds@1952: int rc; ds@1952: ds@1952: assert( iDb>=0 && iDbnDb ); ds@1952: assert( db->aDb[iDb].pBt!=0 ); ds@1952: assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); ds@1952: ds@1952: /* Clear any prior statistics */ ds@1952: for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ ds@1952: Index *pIdx = sqliteHashData(i); ds@1952: sqlite3DefaultRowEst(pIdx); ds@1952: } ds@1952: ds@1952: /* Check to make sure the sqlite_stat1 table existss */ ds@1952: sInfo.db = db; ds@1952: sInfo.zDatabase = db->aDb[iDb].zName; ds@1952: if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: ds@1952: /* Load new statistics out of the sqlite_stat1 table */ ds@1952: zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1", ds@1952: sInfo.zDatabase); ds@1952: sqlite3SafetyOff(db); ds@1952: rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); ds@1952: sqlite3SafetyOn(db); ds@1952: sqlite3_free(zSql); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: ds@1952: #endif /* SQLITE_OMIT_ANALYZE */