2015年5月6日 星期三

SQLite 刪除資料後,檔案大小不會變小的解決方法 Vacuum 指令,也等於壓縮Sqlite資料

       當SQLite資料內容頻繁的做插入、刪除、此時SQLITE檔案並不會變小,這時就可以透過下面的C#語法將資料壓縮。
       筆者進行360W筆資料的移除後發現檔案大小依舊為300MB,當執行vacuum後壓縮資料庫為16MB。同時效能也可以提高。



//--插入進資料庫 這裡的資料一定為唯一的
            SQLiteConnection sqlite_conn;//----連結建立
            SQLiteCommand sqlite_cmd;//--------連結指令建立

            sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
            sqlite_conn.Open();//--------------打開資料庫
            sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
            sqlite_cmd.CommandText = "vacuum; ";
            sqlite_cmd.ExecuteNonQuery();//----執行

沒有留言:

張貼留言