我试图理解为什么Sqlite插入在我们的系统中非常慢。所以我写了一个小测试项目。它所做的就是在表中写入1行。这个小插件需要150毫秒!
我知道下面会打开一个交易。这仍然太长了。
我尝试过添加PRAGMA synchronous = OFF和PRAGMA locking_mode = exclusive。没变。
这是测试代码:
class Program
{
static SQLiteConnection m_dbConnection;
static void Main(string[] args)
{
initDB();
writeToDb();
m_dbConnection.Close();
Console.ReadKey();
}
private static void writeToDb()
{
Stopwatch sw = new Stopwatch();
string sql = "insert into highscores (name, score) values ('Me', 9001)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
sw.Restart();
command.ExecuteNonQuery();
Console.WriteLine("Writing to db took {0}ms", sw.ElapsedMilliseconds);
}
private static void initDB()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
string sql = "CREATE TABLE highscores (name VARCHAR(20), score INT)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}