1

我在我的 Windows 手机 8.1 上使用 SQLite-Net 扩展,插入 2000 行需要 20 多分钟,因为我认为我的 SD 购物车的速度。但是从数据库中选择似乎也很慢。通过 ID(int) 选择我的所有 (2000) 行大约需要 120 秒。我试图在我的数据库打开后立即执行 SQLITE PRAGMAS。它对选择的速度没有影响。

  dbConnection.ExecuteScalarAsync<int>("PRAGMA main.locking_mode = EXCLUSIVE").Wait();
        dbConnection.ExecuteScalarAsync<int>("PRAGMA main.synchronous = NORMAL").Wait();
        dbConnection.ExecuteScalarAsync<int>("PRAGMA main.journal_mode = WAL").Wait();
        dbConnection.ExecuteScalarAsync<int>("PRAGMA main.cache_size = 10000").Wait();
       dbConnection.ExecuteScalarAsync<int>("PRAGMA main.temp_store = MEMORY").Wait();

我正在使用 SQLite Net 扩展的异步版本:SQLite.Net.Async.SQLiteAsyncConnection

你能给我一些建议如何正确快速地使用带有 SQLite-Net 扩展的 sqlite 吗?

这是我从子文件夹中选择文件的递归方法的示例:

      private async Task<List<SongModel>> GetAllSongsFromFolderHelp(int folderID,bool fromSubFolders,bool appendChilderen ,List<SongModel> filesAcc)
    {
        List<DbFFModel> files = await dbConnection.QueryAsync<DbFFModel>("Select * from DbFFModel where IDParent = ?", folderID); //select
        foreach (DbFFModel ff in files)
        {
            if (ff.IsFile)
            {
                SongModel tmp = await this.GetSongByFileIDAsync(ff.ID, appendChilderen);
                if(tmp != null)
                    filesAcc.Add(tmp);
            }
            else if (fromSubFolders)
                await GetAllSongsFromFolderHelp(ff.ID, fromSubFolders,appendChilderen, filesAcc);
        }

        return filesAcc;
    }

这是插入记录的示例:

            IReadOnlyList<StorageFile> files = await storageFolder.GetFilesAsync();
        foreach (StorageFile storageFile in files)
        {
            bool isSupportedMusic = Classes.SupportedFiles.IsSupportedMusic(storageFile.ContentType, storageFile.FileType);
            if (isSupportedMusic)
            {
                BasicProperties fileProperties = await storageFile.GetBasicPropertiesAsync();
                int fileID = await dbConnection.ExecuteScalarAsync<int>("select ID from DbFFModel where Path = ?", storageFile.Path);
                DbFFModel dbFile = await dbConnection.FindAsync<DbFFModel>(fileID);

                bool updateFile = true;
                if (dbFile == null || (!DateTimeComparer.IsEqualRounded(dbFile.DateModified, Convert.ToDateTime(fileProperties.DateModified.ToString()))))
                {
                    if (dbFile == null)
                    {
                        updateFile = false;
                        dbFile = new DbFFModel();
                    }

                    dbFile.Mark = true;
                    dbFile.IDParent = dbFolder.ID;
                    dbFile.Path = storageFile.Path;
                    dbFile.IsFile = true;
                    dbFile.DisplayName = Path.GetFileName(storageFile.Path);
                    dbFile.DateCreated = storageFile.DateCreated.DateTime;
                    dbFile.DateModified = fileProperties.DateModified.DateTime;

                    if (updateFile) 
                        await dbConnection.UpdateAsync(dbFile);//update row
                    else
                        await dbConnection.InsertAsync(dbFile);//insert row

                    await MakeSongDb(dbFile.ID, storageFile);
                }
                else
                {
                    await UpdateMark(dbFile);
                }
            }
        }

谢谢

4

0 回答 0