Windows Phone 8上的SQLite性能非常糟糕

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

我的数据库非常简单,只有一个表:位置:整数主键“FieldIndex”,varchar“Field1”,varchar“Field2”,varchar“Field3”,varchar“Field4”,varchar“Field5”,varchar“Field6”,int “类别ID”

我还有“FieldIndex”和“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秒)?

可怕性能的主要缺点是微软的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-net ORM。 https://github.com/peterhuene/sqlite-net-wp8我已将它用于我的项目,性能非常好。