Index: src/analyze.c ================================================================== --- src/analyze.c +++ src/analyze.c @@ -32,11 +32,12 @@ */ static void openStatTable( Parse *pParse, /* Parsing context */ int iDb, /* The database we are looking in */ int iStatCur, /* Open the sqlite_stat1 table on this cursor */ - const char *zWhere /* Delete entries associated with this table */ + const char *zWhere, /* Delete entries for this table or index */ + const char *zWhereType /* Either "tbl" or "idx" */ ){ static const struct { const char *zName; const char *zCols; } aTable[] = { @@ -77,11 +78,11 @@ ** entire contents of the table. */ aRoot[i] = pStat->tnum; sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); if( zWhere ){ sqlite3NestedParse(pParse, - "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere + "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere ); }else{ /* The sqlite_stat[12] table already exists. Delete all rows. */ sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); } @@ -101,10 +102,11 @@ ** a single table. */ static void analyzeOneTable( Parse *pParse, /* Parser context */ Table *pTab, /* Table whose indices are to be analyzed */ + Index *pOnlyIdx, /* If not NULL, only analyze this one index */ int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ int iMem /* Available memory locations begin here */ ){ sqlite3 *db = pParse->db; /* Database handle */ Index *pIdx; /* An index to being analyzed */ @@ -158,13 +160,16 @@ sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); iIdxCur = pParse->nTab++; sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - int nCol = pIdx->nColumn; - KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + int nCol; + KeyInfo *pKey; + if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; + nCol = pIdx->nColumn; + pKey = sqlite3IndexKeyinfo(pParse, pIdx); if( iMem+1+(nCol*2)>pParse->nMem ){ pParse->nMem = iMem+1+(nCol*2); } /* Open a cursor to the index to be analyzed. */ @@ -381,35 +386,40 @@ int iMem; sqlite3BeginWriteOperation(pParse, 0, iDb); iStatCur = pParse->nTab; pParse->nTab += 2; - openStatTable(pParse, iDb, iStatCur, 0); + openStatTable(pParse, iDb, iStatCur, 0, 0); iMem = pParse->nMem+1; for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ Table *pTab = (Table*)sqliteHashData(k); - analyzeOneTable(pParse, pTab, iStatCur, iMem); + analyzeOneTable(pParse, pTab, 0, iStatCur, iMem); } loadAnalysis(pParse, iDb); } /* ** Generate code that will do an analysis of a single table in -** a database. +** a database. If pOnlyIdx is not NULL then it is a single index +** in pTab that should be analyzed. */ -static void analyzeTable(Parse *pParse, Table *pTab){ +static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){ int iDb; int iStatCur; assert( pTab!=0 ); assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); sqlite3BeginWriteOperation(pParse, 0, iDb); iStatCur = pParse->nTab; pParse->nTab += 2; - openStatTable(pParse, iDb, iStatCur, pTab->zName); - analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1); + if( pOnlyIdx ){ + openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); + }else{ + openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); + } + analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1); loadAnalysis(pParse, iDb); } /* ** Generate code for the ANALYZE command. The parser calls this routine @@ -427,10 +437,11 @@ sqlite3 *db = pParse->db; int iDb; int i; char *z, *zDb; Table *pTab; + Index *pIdx; Token *pTableName; /* Read the database schema. If an error occurs, leave an error message ** and code in pParse and return NULL. */ assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); @@ -451,29 +462,31 @@ if( iDb>=0 ){ analyzeDatabase(pParse, iDb); }else{ z = sqlite3NameFromToken(db, pName1); if( z ){ - pTab = sqlite3LocateTable(pParse, 0, z, 0); - sqlite3DbFree(db, z); - if( pTab ){ - analyzeTable(pParse, pTab); + if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ + analyzeTable(pParse, pIdx->pTable, pIdx); + }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ + analyzeTable(pParse, pTab, 0); } + sqlite3DbFree(db, z); } } }else{ /* Form 3: Analyze the fully qualified table name */ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); if( iDb>=0 ){ zDb = db->aDb[iDb].zName; z = sqlite3NameFromToken(db, pTableName); if( z ){ - pTab = sqlite3LocateTable(pParse, 0, z, zDb); - sqlite3DbFree(db, z); - if( pTab ){ - analyzeTable(pParse, pTab); + if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ + analyzeTable(pParse, pIdx->pTable, pIdx); + }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){ + analyzeTable(pParse, pTab, 0); } + sqlite3DbFree(db, z); } } } } ADDED test/analyze7.test Index: test/analyze7.test ================================================================== --- /dev/null +++ test/analyze7.test @@ -0,0 +1,106 @@ +# 2011 April 1 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# This file implements tests for the ANALYZE command when an idnex +# name is given as the argument. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# There is nothing to test if ANALYZE is disable for this build. +# +ifcapable {!analyze} { + finish_test + return +} + +# Generate some test data +# +do_test analyze7-1.0 { + execsql { + CREATE TABLE sequence(x INTEGER PRIMARY KEY); + INSERT INTO sequence VALUES(1); + INSERT INTO sequence VALUES(2); + INSERT INTO sequence SELECT x+2 FROM sequence; + INSERT INTO sequence SELECT x+4 FROM sequence; + INSERT INTO sequence SELECT x+8 FROM sequence; + INSERT INTO sequence SELECT x+16 FROM sequence; + INSERT INTO sequence SELECT x+32 FROM sequence; + INSERT INTO sequence SELECT x+64 FROM sequence; + INSERT INTO sequence SELECT x+128 FROM sequence; + INSERT INTO sequence SELECT x+256 FROM sequence; + CREATE TABLE t1(a,b,c,d); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1cd ON t1(c,d); + INSERT INTO t1 SELECT x, x, x/100, x FROM sequence; + EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123; + } +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~10 rows)}} +do_test analyze7-1.1 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}} +do_test analyze7-1.2 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~10 rows)}} + +# Run an analyze on one of the three indices. Verify that this +# effects the row-count estimate on the one query that uses that +# one index. +# +do_test analyze7-2.0 { + execsql {ANALYZE t1a;} + db cache flush + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} +do_test analyze7-2.1 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}} +do_test analyze7-2.2 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~10 rows)}} + +# Verify that since the query planner now things that t1a is more +# selective than t1b, it prefers to use t1a. +# +do_test analyze7-2.3 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} + +# Run an analysis on another of the three indices. Verify that this +# new analysis works and does not disrupt the previous analysis. +# +do_test analyze7-3.0 { + execsql {ANALYZE t1cd;} + db cache flush; + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} +do_test analyze7-3.1 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}} +do_test analyze7-3.2 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~102 rows)}} +do_test analyze7-3.3 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} +do_test analyze7-3.4 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}} +do_test analyze7-3.5 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} +do_test analyze7-3.6 { + execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123} +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?) (~1 rows)}} + +finish_test