[英]Creating database using DataContext and System.Data.SQLite in C#
[英]Slow opening SQLite connection in C# app using System.Data.SQLite
我想我的问题暂时已经解决了……我将我的服务和测试应用程序都更改为以SYSTEM
帐户而不是NetworkService
帐户运行。 更改用户帐户的好处是否会持续存在,或者是否只是暂时的,还有待观察。
我注意到我的小型 224kB SQLite DB 在我的 C# 应用程序中打开非常慢,从少量毫秒到 1.5 秒或更多。 下面是我的代码,以及我今天下午添加的所有额外调试语句。 我把它缩小到调用cnn.Open();
如此处的日志所示:
2014-03-27 15:05:39,864 DEBUG - Creating SQLiteConnection...
2014-03-27 15:05:39,927 DEBUG - SQLiteConnection Created!
2014-03-27 15:05:39,927 DEBUG - SQLiteConnection Opening...
2014-03-27 15:05:41,627 DEBUG - SQLiteConnection Opened!
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand Creating...
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand Created!
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand executing reader...
2014-03-27 15:05:41,658 DEBUG - SQLiteCommand executed reader!
2014-03-27 15:05:41,658 DEBUG - DataTable Loading...
2014-03-27 15:05:41,767 DEBUG - DataTable Loaded!
如您所见,在本例中,打开连接需要 1.7 秒。 我试过重复这个,但无法预测后续连接是否会立即打开,或者像这样延迟。
我已经考虑使用某种形式的连接池,但是对于单实例单线程应用程序是否值得追求? 现在,我正在创建我的 SQLiteDatabase 类的一个实例,并为我的每个查询调用以下函数。
public DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
try
{
Logging.LogDebug("Creating SQLiteConnection...");
using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
{
Logging.LogDebug("SQLiteConnection Created!");
Logging.LogDebug("SQLiteConnection Opening...");
cnn.Open();
Logging.LogDebug("SQLiteConnection Opened!");
Logging.LogDebug("SQLiteCommand Creating...");
using (SQLiteCommand mycommand = new SQLiteCommand(cnn))
{
Logging.LogDebug("SQLiteCommand Created!");
mycommand.CommandText = sql;
Logging.LogDebug("SQLiteCommand executing reader...");
using (SQLiteDataReader reader = mycommand.ExecuteReader())
{
Logging.LogDebug("SQLiteCommand executed reader!");
Logging.LogDebug("DataTable Loading...");
dt.Load(reader);
Logging.LogDebug("DataTable Loaded!");
reader.Close();
}
}
cnn.Close();
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}
当然, dbConnection
是连接字符串,由以下函数设置。 inputFile
只是要打开的文件名的字符串路径。
public SqLiteDatabase(String inputFile)
{
dbConnection = String.Format("Data Source={0}", inputFile);
}
在这一点上,我认为sql
无关紧要,因为当 cnn.Open() 停止时,它并没有达到那个点。
好的,我已经做了一些更多的测试。 在本地运行测试,它在大约 5 秒内完成 1000 次迭代循环,每次调用cnn.Open()
大约需要 5 毫秒。 从我在本地 PC 上执行的相同 Windows 安装程序中运行测试,它在大约 25 分钟内完成,每次调用cnn.Open()
平均需要 1468 cnn.Open()
。
我制作了一个小型测试程序,它只从服务程序(与 Windows 服务中运行的代码完全相同TestOpenConn()
调用TestOpenConn()
函数,针对位于测试目录中的文件副本运行。 在服务器或我的本地 PC 上运行它会产生可接受的性能(服务器上每次调用 1.95 毫秒,本地 PC 上每次调用 4 毫秒):
namespace EGC_Timing_Test
{
class Program
{
static void Main(string[] args)
{
Logging.Init("log4net.xml", "test.log");
var db = new SqLiteDatabase("config.sqlite");
db.TestOpenConn();
}
}
}
下面是测试函数:
public void TestOpenConn()
{
// TODO: Remove this after testing loop of opening / closing SQLite DB repeatedly:
const int iterations = 1000;
Logging.LogDebug(String.Format("Running TestOpenConn for {0} opens...", iterations));
var startTime = DateTime.Now;
for (var i = 0; i < iterations; i++)
{
using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
{
Logging.LogDebug(String.Format("SQLiteConnection Opening, iteration {0} of {1}...", i, iterations));
var startTimeInner = DateTime.Now;
cnn.Open();
var endTimeInner = DateTime.Now;
var diffTimeInner = endTimeInner - startTimeInner;
Logging.LogDebug(String.Format("SQLiteConnection Opened in {0}ms!", diffTimeInner.TotalMilliseconds));
cnn.Close();
}
}
var endTime = DateTime.Now;
var diffTime = endTime - startTime;
Logging.LogDebug(String.Format("Done running TestOpenConn for {0} opens!", iterations));
Logging.LogInfo(String.Format("{0} iterations total:\t{1}", iterations, diffTime));
Logging.LogInfo(String.Format("{0} iterations average:\t{1}ms", iterations, diffTime.TotalMilliseconds/iterations));
}
我想我的问题暂时已经解决了……我将我的服务和测试应用程序都更改为以SYSTEM
帐户而不是NetworkService
帐户运行。 更改用户帐户的好处是否会持续存在,或者是否只是暂时的,还有待观察。
我假设您使用的是开源System.Data.SQLite
库。
如果是这种情况,很容易通过 Visual Studio Performance Profiler 看到SQLiteConnection
类的Open
方法存在一些严重的性能问题。 另外,在此处查看此类的源代码: https : //system.data.sqlite.org/index.html/artifact/97648754af51ffd6
有大量的磁盘访问用于读取 XML 配置和 Windows 环境变量。
我的建议是尝试尽可能少地调用Open()
,并尝试在内存中保留对这个打开的SQLiteConnection
对象的引用。 SQLite Forum
引发了一张性能票
遇到了同样的问题,我正在调查这个问题,它似乎与文件或其父文件夹的权限、创建者和/或创建方式有关。 就我而言,SQLite 数据库文件是由以普通用户身份运行的脚本创建的,然后 IIS 托管的服务将在不同的域服务帐户下访问该文件。
每次服务打开一个连接,都需要 1.5 秒以上,但其他方面都正常运行(它最终可以访问文件)。 以普通用户身份运行的独立程序可以在几毫秒内在同一位置打开同一个文件的连接。
对 procmon 跟踪的分析表明,在服务的情况下,我们在大约 1.5 秒的过程中在文件上获得了几个 ACCESS DENIED 日志,这些日志在以普通用户身份运行时没有出现在跟踪中。
不知道那里发生了什么。 该服务运行良好,最终能够查询文件中的数据,尽管速度很慢。
当我们将服务帐户设为文件父文件夹的所有者并授予他写权限时,ACCESS DENIED 日志消失,服务全速运行。
您可以将适当用户的“修改”权限添加到您的数据库文件夹中。 右键单击文件夹> 属性> 安全性> 编辑> 添加(我添加了IIS_Users)> 选择“修改”复选框> 确定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.