gpt4 book ai didi

c# - 使用 System.Data.SQLite 在 C# 应用程序中缓慢打开 SQLite 连接

转载 作者:太空狗 更新时间:2023-10-29 21:17:33 26 4
gpt4 key购买 nike

编辑 3:

我想我的问题暂时已经解决了...我将我的服务和测试应用都更改为以 SYSTEM 帐户而不是 NetworkService 帐户运行。更改用户帐户的好处是否会持续存在,或者是否只是暂时的,还有待观察。

原始问题:

我注意到我的 224kB 小型 SQLite 数据库在我的 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() 停止时它还没有到达那个点。

编辑2:

好的,我已经做了一些测试。在本地运行测试,它在大约 5 秒内完成了 1000 次迭代循环,每次调用 cnn.Open() 大约需要 5 毫秒。从我在本地 PC 上执行的同一 Windows 安装程序运行测试,它在大约 25 分钟内完成,每次调用 cnn.Open() 平均耗时 1468 毫秒。

我制作了一个小测试程序,它只调用服务程序中的 TestOpenConn() 函数(与 Windows 服务中运行的代码完全相同),针对位于的文件副本运行一个测试目录。在服务器或我的本地 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 帐户运行。更改用户帐户的好处是否会持续存在,或者是否只是暂时的,还有待观察。

关于c# - 使用 System.Data.SQLite 在 C# 应用程序中缓慢打开 SQLite 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22697362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com