analyze.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  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. **
  14. ** @(#) $Id: analyze.c,v 1.24 2007/11/15 13:10:23 danielk1977 Exp $
  15. */
  16. #ifndef SQLITE_OMIT_ANALYZE
  17. #include "sqliteInt.h"
  18. /*
  19. ** This routine generates code that opens the sqlite_stat1 table on cursor
  20. ** iStatCur.
  21. **
  22. ** If the sqlite_stat1 tables does not previously exist, it is created.
  23. ** If it does previously exist, all entires associated with table zWhere
  24. ** are removed. If zWhere==0 then all entries are removed.
  25. */
  26. static void openStatTable(
  27. Parse *pParse, /* Parsing context */
  28. int iDb, /* The database we are looking in */
  29. int iStatCur, /* Open the sqlite_stat1 table on this cursor */
  30. const char *zWhere /* Delete entries associated with this table */
  31. ){
  32. sqlite3 *db = pParse->db;
  33. Db *pDb;
  34. int iRootPage;
  35. Table *pStat;
  36. Vdbe *v = sqlite3GetVdbe(pParse);
  37. if( v==0 ) return;
  38. assert( sqlite3BtreeHoldsAllMutexes(db) );
  39. assert( sqlite3VdbeDb(v)==db );
  40. pDb = &db->aDb[iDb];
  41. if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
  42. /* The sqlite_stat1 tables does not exist. Create it.
  43. ** Note that a side-effect of the CREATE TABLE statement is to leave
  44. ** the rootpage of the new table on the top of the stack. This is
  45. ** important because the OpenWrite opcode below will be needing it. */
  46. sqlite3NestedParse(pParse,
  47. "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)",
  48. pDb->zName
  49. );
  50. iRootPage = 0; /* Cause rootpage to be taken from top of stack */
  51. }else if( zWhere ){
  52. /* The sqlite_stat1 table exists. Delete all entries associated with
  53. ** the table zWhere. */
  54. sqlite3NestedParse(pParse,
  55. "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q",
  56. pDb->zName, zWhere
  57. );
  58. iRootPage = pStat->tnum;
  59. }else{
  60. /* The sqlite_stat1 table already exists. Delete all rows. */
  61. iRootPage = pStat->tnum;
  62. sqlite3VdbeAddOp(v, OP_Clear, pStat->tnum, iDb);
  63. }
  64. /* Open the sqlite_stat1 table for writing. Unless it was created
  65. ** by this vdbe program, lock it for writing at the shared-cache level.
  66. ** If this vdbe did create the sqlite_stat1 table, then it must have
  67. ** already obtained a schema-lock, making the write-lock redundant.
  68. */
  69. if( iRootPage>0 ){
  70. sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
  71. }
  72. sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
  73. sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage);
  74. sqlite3VdbeAddOp(v, OP_SetNumColumns, iStatCur, 3);
  75. }
  76. /*
  77. ** Generate code to do an analysis of all indices associated with
  78. ** a single table.
  79. */
  80. static void analyzeOneTable(
  81. Parse *pParse, /* Parser context */
  82. Table *pTab, /* Table whose indices are to be analyzed */
  83. int iStatCur, /* Cursor that writes to the sqlite_stat1 table */
  84. int iMem /* Available memory locations begin here */
  85. ){
  86. Index *pIdx; /* An index to being analyzed */
  87. int iIdxCur; /* Cursor number for index being analyzed */
  88. int nCol; /* Number of columns in the index */
  89. Vdbe *v; /* The virtual machine being built up */
  90. int i; /* Loop counter */
  91. int topOfLoop; /* The top of the loop */
  92. int endOfLoop; /* The end of the loop */
  93. int addr; /* The address of an instruction */
  94. int iDb; /* Index of database containing pTab */
  95. v = sqlite3GetVdbe(pParse);
  96. if( v==0 || pTab==0 || pTab->pIndex==0 ){
  97. /* Do no analysis for tables that have no indices */
  98. return;
  99. }
  100. assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  101. iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  102. assert( iDb>=0 );
  103. #ifndef SQLITE_OMIT_AUTHORIZATION
  104. if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
  105. pParse->db->aDb[iDb].zName ) ){
  106. return;
  107. }
  108. #endif
  109. /* Establish a read-lock on the table at the shared-cache level. */
  110. sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  111. iIdxCur = pParse->nTab;
  112. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  113. KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  114. /* Open a cursor to the index to be analyzed
  115. */
  116. assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
  117. sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
  118. VdbeComment((v, "# %s", pIdx->zName));
  119. sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum,
  120. (char *)pKey, P3_KEYINFO_HANDOFF);
  121. nCol = pIdx->nColumn;
  122. if( iMem+nCol*2>=pParse->nMem ){
  123. pParse->nMem = iMem+nCol*2+1;
  124. }
  125. sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, nCol+1);
  126. /* Memory cells are used as follows:
  127. **
  128. ** mem[iMem]: The total number of rows in the table.
  129. ** mem[iMem+1]: Number of distinct values in column 1
  130. ** ...
  131. ** mem[iMem+nCol]: Number of distinct values in column N
  132. ** mem[iMem+nCol+1] Last observed value of column 1
  133. ** ...
  134. ** mem[iMem+nCol+nCol]: Last observed value of column N
  135. **
  136. ** Cells iMem through iMem+nCol are initialized to 0. The others
  137. ** are initialized to NULL.
  138. */
  139. for(i=0; i<=nCol; i++){
  140. sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i);
  141. }
  142. for(i=0; i<nCol; i++){
  143. sqlite3VdbeAddOp(v, OP_MemNull, iMem+nCol+i+1, 0);
  144. }
  145. /* Do the analysis.
  146. */
  147. endOfLoop = sqlite3VdbeMakeLabel(v);
  148. sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, endOfLoop);
  149. topOfLoop = sqlite3VdbeCurrentAddr(v);
  150. sqlite3VdbeAddOp(v, OP_MemIncr, 1, iMem);
  151. for(i=0; i<nCol; i++){
  152. sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i);
  153. sqlite3VdbeAddOp(v, OP_MemLoad, iMem+nCol+i+1, 0);
  154. sqlite3VdbeAddOp(v, OP_Ne, 0x100, 0);
  155. }
  156. sqlite3VdbeAddOp(v, OP_Goto, 0, endOfLoop);
  157. for(i=0; i<nCol; i++){
  158. addr = sqlite3VdbeAddOp(v, OP_MemIncr, 1, iMem+i+1);
  159. sqlite3VdbeChangeP2(v, topOfLoop + 3*i + 3, addr);
  160. sqlite3VdbeAddOp(v, OP_Column, iIdxCur, i);
  161. sqlite3VdbeAddOp(v, OP_MemStore, iMem+nCol+i+1, 1);
  162. }
  163. sqlite3VdbeResolveLabel(v, endOfLoop);
  164. sqlite3VdbeAddOp(v, OP_Next, iIdxCur, topOfLoop);
  165. sqlite3VdbeAddOp(v, OP_Close, iIdxCur, 0);
  166. /* Store the results.
  167. **
  168. ** The result is a single row of the sqlite_stat1 table. The first
  169. ** two columns are the names of the table and index. The third column
  170. ** is a string composed of a list of integer statistics about the
  171. ** index. The first integer in the list is the total number of entires
  172. ** in the index. There is one additional integer in the list for each
  173. ** column of the table. This additional integer is a guess of how many
  174. ** rows of the table the index will select. If D is the count of distinct
  175. ** values and K is the total number of rows, then the integer is computed
  176. ** as:
  177. **
  178. ** I = (K+D-1)/D
  179. **
  180. ** If K==0 then no entry is made into the sqlite_stat1 table.
  181. ** If K>0 then it is always the case the D>0 so division by zero
  182. ** is never possible.
  183. */
  184. sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
  185. addr = sqlite3VdbeAddOp(v, OP_IfNot, 0, 0);
  186. sqlite3VdbeAddOp(v, OP_NewRowid, iStatCur, 0);
  187. sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
  188. sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
  189. sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
  190. sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0);
  191. for(i=0; i<nCol; i++){
  192. sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
  193. sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0);
  194. sqlite3VdbeAddOp(v, OP_Add, 0, 0);
  195. sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
  196. sqlite3VdbeAddOp(v, OP_MemLoad, iMem+i+1, 0);
  197. sqlite3VdbeAddOp(v, OP_Divide, 0, 0);
  198. sqlite3VdbeAddOp(v, OP_ToInt, 0, 0);
  199. if( i==nCol-1 ){
  200. sqlite3VdbeAddOp(v, OP_Concat, nCol*2-1, 0);
  201. }else{
  202. sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
  203. }
  204. }
  205. sqlite3VdbeOp3(v, OP_MakeRecord, 3, 0, "aaa", 0);
  206. sqlite3VdbeAddOp(v, OP_Insert, iStatCur, OPFLAG_APPEND);
  207. sqlite3VdbeJumpHere(v, addr);
  208. }
  209. }
  210. /*
  211. ** Generate code that will cause the most recent index analysis to
  212. ** be laoded into internal hash tables where is can be used.
  213. */
  214. static void loadAnalysis(Parse *pParse, int iDb){
  215. Vdbe *v = sqlite3GetVdbe(pParse);
  216. if( v ){
  217. sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0);
  218. }
  219. }
  220. /*
  221. ** Generate code that will do an analysis of an entire database
  222. */
  223. static void analyzeDatabase(Parse *pParse, int iDb){
  224. sqlite3 *db = pParse->db;
  225. Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
  226. HashElem *k;
  227. int iStatCur;
  228. int iMem;
  229. sqlite3BeginWriteOperation(pParse, 0, iDb);
  230. iStatCur = pParse->nTab++;
  231. openStatTable(pParse, iDb, iStatCur, 0);
  232. iMem = pParse->nMem;
  233. for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
  234. Table *pTab = (Table*)sqliteHashData(k);
  235. analyzeOneTable(pParse, pTab, iStatCur, iMem);
  236. }
  237. loadAnalysis(pParse, iDb);
  238. }
  239. /*
  240. ** Generate code that will do an analysis of a single table in
  241. ** a database.
  242. */
  243. static void analyzeTable(Parse *pParse, Table *pTab){
  244. int iDb;
  245. int iStatCur;
  246. assert( pTab!=0 );
  247. assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  248. iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  249. sqlite3BeginWriteOperation(pParse, 0, iDb);
  250. iStatCur = pParse->nTab++;
  251. openStatTable(pParse, iDb, iStatCur, pTab->zName);
  252. analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem);
  253. loadAnalysis(pParse, iDb);
  254. }
  255. /*
  256. ** Generate code for the ANALYZE command. The parser calls this routine
  257. ** when it recognizes an ANALYZE command.
  258. **
  259. ** ANALYZE -- 1
  260. ** ANALYZE <database> -- 2
  261. ** ANALYZE ?<database>.?<tablename> -- 3
  262. **
  263. ** Form 1 causes all indices in all attached databases to be analyzed.
  264. ** Form 2 analyzes all indices the single database named.
  265. ** Form 3 analyzes all indices associated with the named table.
  266. */
  267. void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
  268. sqlite3 *db = pParse->db;
  269. int iDb;
  270. int i;
  271. char *z, *zDb;
  272. Table *pTab;
  273. Token *pTableName;
  274. /* Read the database schema. If an error occurs, leave an error message
  275. ** and code in pParse and return NULL. */
  276. assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
  277. if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  278. return;
  279. }
  280. if( pName1==0 ){
  281. /* Form 1: Analyze everything */
  282. for(i=0; i<db->nDb; i++){
  283. if( i==1 ) continue; /* Do not analyze the TEMP database */
  284. analyzeDatabase(pParse, i);
  285. }
  286. }else if( pName2==0 || pName2->n==0 ){
  287. /* Form 2: Analyze the database or table named */
  288. iDb = sqlite3FindDb(db, pName1);
  289. if( iDb>=0 ){
  290. analyzeDatabase(pParse, iDb);
  291. }else{
  292. z = sqlite3NameFromToken(db, pName1);
  293. if( z ){
  294. pTab = sqlite3LocateTable(pParse, z, 0);
  295. sqlite3_free(z);
  296. if( pTab ){
  297. analyzeTable(pParse, pTab);
  298. }
  299. }
  300. }
  301. }else{
  302. /* Form 3: Analyze the fully qualified table name */
  303. iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
  304. if( iDb>=0 ){
  305. zDb = db->aDb[iDb].zName;
  306. z = sqlite3NameFromToken(db, pTableName);
  307. if( z ){
  308. pTab = sqlite3LocateTable(pParse, z, zDb);
  309. sqlite3_free(z);
  310. if( pTab ){
  311. analyzeTable(pParse, pTab);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. /*
  318. ** Used to pass information from the analyzer reader through to the
  319. ** callback routine.
  320. */
  321. typedef struct analysisInfo analysisInfo;
  322. struct analysisInfo {
  323. sqlite3 *db;
  324. const char *zDatabase;
  325. };
  326. /*
  327. ** This callback is invoked once for each index when reading the
  328. ** sqlite_stat1 table.
  329. **
  330. ** argv[0] = name of the index
  331. ** argv[1] = results of analysis - on integer for each column
  332. */
  333. static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
  334. analysisInfo *pInfo = (analysisInfo*)pData;
  335. Index *pIndex;
  336. int i, c;
  337. unsigned int v;
  338. const char *z;
  339. assert( argc==2 );
  340. if( argv==0 || argv[0]==0 || argv[1]==0 ){
  341. return 0;
  342. }
  343. pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
  344. if( pIndex==0 ){
  345. return 0;
  346. }
  347. z = argv[1];
  348. for(i=0; *z && i<=pIndex->nColumn; i++){
  349. v = 0;
  350. while( (c=z[0])>='0' && c<='9' ){
  351. v = v*10 + c - '0';
  352. z++;
  353. }
  354. pIndex->aiRowEst[i] = v;
  355. if( *z==' ' ) z++;
  356. }
  357. return 0;
  358. }
  359. /*
  360. ** Load the content of the sqlite_stat1 table into the index hash tables.
  361. */
  362. int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
  363. analysisInfo sInfo;
  364. HashElem *i;
  365. char *zSql;
  366. int rc;
  367. assert( iDb>=0 && iDb<db->nDb );
  368. assert( db->aDb[iDb].pBt!=0 );
  369. assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
  370. /* Clear any prior statistics */
  371. for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
  372. Index *pIdx = sqliteHashData(i);
  373. sqlite3DefaultRowEst(pIdx);
  374. }
  375. /* Check to make sure the sqlite_stat1 table existss */
  376. sInfo.db = db;
  377. sInfo.zDatabase = db->aDb[iDb].zName;
  378. if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
  379. return SQLITE_ERROR;
  380. }
  381. /* Load new statistics out of the sqlite_stat1 table */
  382. zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
  383. sInfo.zDatabase);
  384. sqlite3SafetyOff(db);
  385. rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
  386. sqlite3SafetyOn(db);
  387. sqlite3_free(zSql);
  388. return rc;
  389. }
  390. #endif /* SQLITE_OMIT_ANALYZE */