/ Check-in [6609c25f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Cleanup and refactor parts of the optimizer. (CVS 3301)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6609c25fbfa5ad7f55c356936abb1721686c47ca
User & Date: drh 2006-06-27 02:33:40
Context
2006-06-27
02:36
Make sure that MATCH terms that a virtual table says should be omitted really are omitted. (CVS 3302) check-in: 3e1f5567 user: drh tags: trunk
02:33
Cleanup and refactor parts of the optimizer. (CVS 3301) check-in: 6609c25f user: drh tags: trunk
01:54
Cache and reuse virtual table index information in the optimizer. Improved diagnostics for virtual table index selection. (CVS 3300) check-in: 28413cf2 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is reponsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.224 2006/06/27 01:54:26 drh Exp $
           19  +** $Id: where.c,v 1.225 2006/06/27 02:33:40 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
    25     25   */
    26     26   #define BMS  (sizeof(Bitmask)*8)
................................................................................
  1852   1852         m = getMask(&maskSet, pTabItem->iCursor);
  1853   1853         if( (m & notReady)==0 ){
  1854   1854           if( j==iFrom ) iFrom++;
  1855   1855           continue;
  1856   1856         }
  1857   1857         assert( pTabItem->pTab );
  1858   1858   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1859         -      pIndex = 0;
  1860   1859         if( IsVirtual(pTabItem->pTab) ){
  1861   1860           sqlite3_index_info **ppIdxInfo = &pWInfo->a[j].pIdxInfo;
  1862   1861           cost = bestVirtualIndex(pParse, &wc, pTabItem, notReady,
  1863   1862                                   ppOrderBy ? *ppOrderBy : 0, i==0,
  1864   1863                                   ppIdxInfo);
  1865   1864           flags = WHERE_VIRTUALTABLE;
  1866   1865           pIndex = *ppIdxInfo;
................................................................................
  1871   1870           nEq = 0;
  1872   1871         }else 
  1873   1872   #endif
  1874   1873         {
  1875   1874           cost = bestIndex(pParse, &wc, pTabItem, notReady,
  1876   1875                            (i==0 && ppOrderBy) ? *ppOrderBy : 0,
  1877   1876                            &pIdx, &flags, &nEq);
         1877  +        pIndex = 0;
  1878   1878         }
  1879   1879         if( cost<lowestCost ){
  1880   1880           once = 1;
  1881   1881           lowestCost = cost;
  1882   1882           pBest = pIdx;
  1883   1883           bestFlags = flags;
  1884   1884           bestNEq = nEq;
  1885   1885           bestJ = j;
  1886         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1887   1886           pLevel->pBestIdx = pIndex;
  1888         -#endif
  1889   1887         }
  1890   1888         if( doNotReorder ) break;
  1891   1889       }
  1892   1890       TRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
  1893   1891              pLevel-pWInfo->a));
  1894   1892       if( (bestFlags & WHERE_ORDERBY)!=0 ){
  1895   1893         *ppOrderBy = 0;
................................................................................
  1917   1915       *ppOrderBy = 0;
  1918   1916     }
  1919   1917   
  1920   1918     /* Open all tables in the pTabList and any indices selected for
  1921   1919     ** searching those tables.
  1922   1920     */
  1923   1921     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  1924         -  pLevel = pWInfo->a;
  1925   1922     for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
  1926   1923       Table *pTab;     /* Table to open */
  1927   1924       Index *pIx;      /* Index used to access pTab (if any) */
  1928   1925       int iDb;         /* Index of database containing table/index */
  1929   1926       int iIdxCur = pLevel->iIdxCur;
  1930   1927   
  1931   1928   #ifndef SQLITE_OMIT_EXPLAIN
................................................................................
  2032   2029       }
  2033   2030   
  2034   2031   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2035   2032       if( pLevel->pBestIdx ){
  2036   2033         /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
  2037   2034         **          to access the data.
  2038   2035         */
  2039         -      int ii;
         2036  +      int j;
  2040   2037         sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
  2041   2038         int nConstraint = pBestIdx->nConstraint;
  2042   2039         struct sqlite3_index_constraint_usage *aUsage =
  2043   2040                                                     pBestIdx->aConstraintUsage;
  2044   2041         const struct sqlite3_index_constraint *aConstraint =
  2045   2042                                                     pBestIdx->aConstraint;
  2046   2043   
  2047         -      for(ii=1; ii<=nConstraint; ii++){
  2048         -        int j;
  2049         -        for(j=0; j<nConstraint; j++){
  2050         -          if( aUsage[j].argvIndex==ii ){
  2051         -            int k = aConstraint[j].iTermOffset;
  2052         -            sqlite3ExprCode(pParse, wc.a[k].pExpr->pRight);
         2044  +      for(j=1; j<=nConstraint; j++){
         2045  +        int k;
         2046  +        for(k=0; k<nConstraint; k++){
         2047  +          if( aUsage[k].argvIndex==j ){
         2048  +            int iTerm = aConstraint[j-1].iTermOffset;
         2049  +            sqlite3ExprCode(pParse, wc.a[iTerm].pExpr->pRight);
  2053   2050               break;
  2054   2051             }
  2055   2052           }
  2056         -        if( j==nConstraint ) break;
         2053  +        if( k==nConstraint ) break;
  2057   2054         }
  2058         -      sqlite3VdbeAddOp(v, OP_Integer, ii-1, 0);
         2055  +      sqlite3VdbeAddOp(v, OP_Integer, j-1, 0);
  2059   2056         sqlite3VdbeAddOp(v, OP_Integer, pBestIdx->idxNum, 0);
  2060   2057         sqlite3VdbeOp3(v, OP_VFilter, iCur, brk, pBestIdx->idxStr,
  2061   2058                         pBestIdx->needToFreeIdxStr ? P3_MPRINTF : P3_STATIC);
  2062   2059         pBestIdx->needToFreeIdxStr = 0;
  2063         -      for(ii=0; ii<pBestIdx->nConstraint; ii++){
  2064         -        if( pBestIdx->aConstraintUsage[ii].omit ){
  2065         -          disableTerm(pLevel, &wc.a[ii]);
         2060  +      for(j=0; j<pBestIdx->nConstraint; j++){
         2061  +        if( aUsage[j].omit ){
         2062  +          int iTerm = aConstraint[j].iTermOffset;
         2063  +          disableTerm(pLevel, &wc.a[iTerm]);
  2066   2064           }
  2067   2065         }
  2068   2066         pLevel->op = OP_VNext;
  2069   2067         pLevel->p1 = iCur;
  2070   2068         pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  2071   2069       }else
  2072   2070   #endif /* SQLITE_OMIT_VIRTUALTABLE */