在.NET CORE中使用Dapper极慢的SQLite查询速度(Extremely slow SQLite query speed using Dapper in .NET CORE)

编程入门 行业动态 更新时间:2024-10-27 02:21:36
在.NET CORE中使用Dapper极慢的SQLite查询速度(Extremely slow SQLite query speed using Dapper in .NET CORE)

我有一些代码从.NET Core 2.0中的本地SQLite数据库文件中提取HTML页面。

此代码在调试模式下工作良好,但在发布应用程序后运行速度非常慢。

我使用秒表来诊断哪一段代码导致了问题,并发现connection.QueryFirstOrDefault需要2ms才能在调试模式下查找单个行,但是同样的任务在发布应用程序后需要1.4秒。 这大约要慢700倍。

//initialize connection var connection = new SqliteConnection("Data Source=" + dbName); // Build SQL String string query = @"SELECT * FROM HtmlItems WHERE PostID = 1; // Start Timer var watch = System.Diagnostics.Stopwatch.StartNew(); Submit query HtmlItem = connection.QueryFirstOrDefault<HtmlItem>(query); // End Timer watch.Stop(); var result = watch.ElapsedMilliseconds();

查询映射到一个像这样的对象

public class HtmlItem { public int PostID { get; set; } public string PostTitle { get; set; } public string PostDescription { get; set; } public int PostDate { get; set; } // Unix Timestamp public int Hidden { get; set; } public string Url { get; set; } public string PostHTML { get; set; } }

在调试和生产中使用相同的数据库文件,该数据库文件只有三行。 我的应用程序应该是唯一试图访问该文件的东西。

索引SQLite数据库文件看起来似乎没有提高速度。

我想知道如何确定是什么原因导致我与数据库的连接速度降低了700倍。

I have a bit of code which pulls in a HTML page from a local SQLite database file in .NET Core 2.0.

This code works fine when exicuted in debug mode, but runs extremely slow in production after I have published the app.

I used a Stopwatch to diagnose which piece of code is causing the problem and found that connection.QueryFirstOrDefault takes 2ms to find a single row in debug mode, however the same task takes 1.4 seconds after I have published the app. That's about 700 times slower.

//initialize connection var connection = new SqliteConnection("Data Source=" + dbName); // Build SQL String string query = @"SELECT * FROM HtmlItems WHERE PostID = 1; // Start Timer var watch = System.Diagnostics.Stopwatch.StartNew(); Submit query HtmlItem = connection.QueryFirstOrDefault<HtmlItem>(query); // End Timer watch.Stop(); var result = watch.ElapsedMilliseconds();

The query maps to an object looking like this

public class HtmlItem { public int PostID { get; set; } public string PostTitle { get; set; } public string PostDescription { get; set; } public int PostDate { get; set; } // Unix Timestamp public int Hidden { get; set; } public string Url { get; set; } public string PostHTML { get; set; } }

The same database file is used in debug and production, this database file only has three rows. My app should be the only thing trying to access the file.

Indexing the SQLite database file dosen't seem to make any speed improvement.

I'm wondering how I can identify what's causing my connection to the database to be 700x slower in production.

最满意答案

在同步读取文件时,SQLite似乎并没有像.net核心那么快速。

如果你使用异步读取文件,那么它看起来要快得多。

HtmlItem = await connection.QueryFirstOrDefault<HtmlItem>(query);

SQLite dosen't seem to be very quick with dapper on .net core when reading the file synchronously.

If you read the file using asynchronous then it seems to be much faster.

HtmlItem = await connection.QueryFirstOrDefault<HtmlItem>(query);

更多推荐

本文发布于:2023-07-22 12:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   Dapper   SQLite   NET   CORE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!