Windows Phone 8上的SQLite性能非常差

本文关键字:性能 非常 SQLite 上的 Phone Windows | 更新日期: 2023-09-27 18:20:46

我有一个使用SQLite数据库的Windows Phone 8应用程序。为了访问数据库,我使用Andy Wigley(来源)的WinRT Wrapper

我的数据库非常简单,只有一个表:位置:integer主键"FieldIndex"、varchar"Field1"、varchar"Field2"、varcharField3、varchar Field4、varchar菲尔德5、varchar菲尔德6、int"CategoryID"

我还有一个关于"FieldIndex"answers"CategoryID"的索引。

表中总共有4000个条目,数据库大小为900 kB。我还压缩了数据库(真空)。数据库作为应用程序内容部署到手机中(=在安装文件夹中=只读)。我只将数据库用于查询。我的数据访问代码如下:

using (var db = new SQLiteWinRTPhone.Database(Package.Current.InstalledLocation, @"Model'db.sqlite"))
{
    await db.OpenAsync(SQLiteWinRTPhone.SqliteOpenMode.OpenRead);
    await db.ExecuteStatementAsync("PRAGMA journal_mode = MEMORY");
    await db.ExecuteStatementAsync("PRAGMA temp_store =  2;");
    using (var stmt = await db.PrepareStatementAsync("SELECT * FROM Locations;"))
    {
        int i = 0;
        while (await stmt.StepAsync())
        {
            // There is nothing happening here
            // Just for testing. In my real code, I iterate on all rows and store them in a object. I wanted isolate the issue here.
            i++;
        }
    }
    MessageBox.Show("We read " + i.toString() + " records.");
}

目前,上述陈述需要20秒才能完成。从我的角度来看,这是不可接受的。我试图评测我的应用程序,热路径在本机代码库中(主要与ConcRT相关)。WinRT包装器被编译为"发布",我不明白为什么性能如此糟糕。

目标:我想读取所有行,并将它们存储在一个对象中,以便绑定到我的视图、搜索等。

有什么想法可以让我的数据库查询在某种程度上被接受(<5秒)吗?

Windows Phone 8上的SQLite性能非常差

性能糟糕的主要原因是微软的WinRT包装器,默认情况下性能不好。始终使用以下方式选择和插入循环:

await stmt.StepAsync().AsTask().ConfigureAwait(false))

更多详细信息:http://blogs.msdn.com/b/andy_wigley/archive/2013/11/21/how-to-massively-improve-sqlite-performance-using-sqlwinrt.aspx

我不知道你的代码出了什么问题。您还可以尝试使用另一个包装器,例如c#-sqlite+sqlite-netORM。https://github.com/peterhuene/sqlite-net-wp8我把它用于我的项目,表现非常好。