Ejemplo n.º 1
0
        public static RC AnalysisLoad(Context ctx, int db)
        {
            Debug.Assert(db >= 0 && db < ctx.DBs.length);
            Debug.Assert(ctx.DBs[db].Bt != null);

            // Clear any prior statistics
            Debug.Assert(Btree.SchemaMutexHeld(ctx, db, null));
            for (HashElem i = ctx.DBs[db].Schema.IndexHash.First; i != null; i = i.Next)
            {
                Index idx = (Index)i.Data;
                sqlite3DefaultRowEst(idx);
                sqlite3DeleteIndexSamples(ctx, idx);
                idx.Samples.data = null;
            }

            // Check to make sure the sqlite_stat1 table exists
            AnalysisInfo sInfo = new AnalysisInfo();

            sInfo.Ctx      = ctx;
            sInfo.Database = ctx.DBs[db].Name;
            if (sqlite3FindTable(ctx, "sqlite_stat1", sInfo.Database) == null)
            {
                return(RC.ERROR);
            }

            // Load new statistics out of the sqlite_stat1 table
            string sql = C._mtagprintf(ctx, "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.Database);

            if (sql == null)
            {
                rc = RC.NOMEM;
            }
            else
            {
                rc = Vdbe.Exec(ctx, sql, AnalysisLoader, sInfo, 0);
                C._tagfree(ctx, ref sql);
            }

            // Load the statistics from the sqlite_stat3 table.
#if ENABLE_STAT3
            if (rc == RC_OK)
            {
                bool lookasideEnabled = ctx.Lookaside.Enabled;
                ctx.Lookaside.Enabled = false;
                rc = LoadStat3(ctx, sInfo.Database);
                ctx.Lookaside.Enabled = lookasideEnabled;
            }
#endif

            if (rc == RC.NOMEM)
            {
                db.MallocFailed = true;
            }
            return(rc);
        }