SQLite速度評(píng)測(cè)代碼
更新時(shí)間:2008年09月14日 08:33:40 作者:
SQLite 作為一個(gè)輕量級(jí)嵌入式數(shù)據(jù)庫(kù),還是非常好用的。雨痕極力推薦~~~~~~
今天有個(gè)朋友測(cè)試 SQLite,然后得出的結(jié)論是:SQLite 效率太低,批量插入1000條記錄,居然耗時(shí) 2 分鐘!
下面是他發(fā)給我的測(cè)試代碼。我暈~~~~~~
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
// 創(chuàng)建數(shù)據(jù)庫(kù)文件
File.Delete("test1.db3");
SQLiteConnection.CreateFile("test1.db3");
DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 連接數(shù)據(jù)庫(kù)
conn.ConnectionString = "Data Source=test1.db3";
conn.Open();
// 創(chuàng)建數(shù)據(jù)表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
// 添加參數(shù)
cmd.Parameters.Add(cmd.CreateParameter());
// 開始計(jì)時(shí)
Stopwatch watch = new Stopwatch();
watch.Start();
// 連續(xù)插入1000條記錄
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into [test1] ([s]) values (?)";
cmd.Parameters[0].Value = i.ToString();
cmd.ExecuteNonQuery();
}
// 停止計(jì)時(shí)
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
哎~~~~ 一個(gè)常識(shí)性的錯(cuò)誤,我加幾行代碼 (新增代碼標(biāo)記 "http:// <-------------------")。
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
// 創(chuàng)建數(shù)據(jù)庫(kù)文件
File.Delete("test1.db3");
SQLiteConnection.CreateFile("test1.db3");
DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 連接數(shù)據(jù)庫(kù)
conn.ConnectionString = "Data Source=test1.db3";
conn.Open();
// 創(chuàng)建數(shù)據(jù)表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
// 添加參數(shù)
cmd.Parameters.Add(cmd.CreateParameter());
// 開始計(jì)時(shí)
Stopwatch watch = new Stopwatch();
watch.Start();
DbTransaction trans = conn.BeginTransaction(); // <-------------------
try
{
// 連續(xù)插入1000條記錄
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into [test1] ([s]) values (?)";
cmd.Parameters[0].Value = i.ToString();
cmd.ExecuteNonQuery();
}
trans.Commit(); // <-------------------
}
catch
{
trans.Rollback(); // <-------------------
throw; // <-------------------
}
// 停止計(jì)時(shí)
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
執(zhí)行一下,耗時(shí) 0.2 秒。這差距是不是太大了點(diǎn)?
為什么只是簡(jiǎn)單啟用了一個(gè)事務(wù)會(huì)有這么大的差距呢?很簡(jiǎn)單,SQLite 缺省為每個(gè)操作啟動(dòng)一個(gè)事務(wù),那么原代碼 1000 次插入起碼開啟了 1000 個(gè)事務(wù),"事務(wù)開啟 + SQL 執(zhí)行 + 事務(wù)關(guān)閉" 自然耗費(fèi)了大量的時(shí)間,這也是后面顯示啟動(dòng)事務(wù)后為什么如此快的原因。其實(shí)這是數(shù)據(jù)庫(kù)操作的基本常識(shí),大家要緊記,不好的代碼效率差的不是一點(diǎn)半點(diǎn)。
相關(guān)文章
python之sqlalchemy創(chuàng)建表的實(shí)例詳解
這篇文章主要介紹了數(shù)據(jù)庫(kù)之sqlalchemy創(chuàng)建表的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10
Sqlite數(shù)據(jù)庫(kù)里插入數(shù)據(jù)的條數(shù)上限是500
sqlite每次只能插入的數(shù)據(jù)不能超過500條數(shù)據(jù),大家在使用的時(shí)候需要注意一下。2015-04-04
sqlite時(shí)間戳轉(zhuǎn)時(shí)間語(yǔ)句(時(shí)間轉(zhuǎn)時(shí)間戳)
這篇文章主要介紹了sqlite時(shí)間戳轉(zhuǎn)時(shí)間、時(shí)間轉(zhuǎn)時(shí)間戳的方法,需要的朋友可以參考下2014-06-06
VScode第三方插件打開sqlite數(shù)據(jù)庫(kù)圖文教程
在實(shí)際做一個(gè)項(xiàng)目的時(shí)候,為了提高效率我們會(huì)首選不重復(fù)造輪子,所以可能會(huì)用到第三方庫(kù),下面這篇文章主要給大家介紹了關(guān)于VScode第三方插件打開sqlite數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
SQLite高手晉級(jí)教程:調(diào)試與性能優(yōu)化以及常見問題
SQLite 是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),廣泛用于各種應(yīng)用中,包括移動(dòng)應(yīng)用和嵌入式系統(tǒng),盡管它非常靈活和強(qiáng)大,但在處理大規(guī)模數(shù)據(jù)或高并發(fā)請(qǐng)求時(shí),性能優(yōu)化變得非常重要,本篇文章將重點(diǎn)講解 SQLite 的調(diào)試工具和性能優(yōu)化技巧,以幫助您解決常見問題并進(jìn)一步提升數(shù)據(jù)庫(kù)性能2025-03-03
SQLite學(xué)習(xí)手冊(cè)(SQLite在線備份)
在SQLite中提供了一組用于在線數(shù)據(jù)庫(kù)備份的APIs函數(shù)(C接口),可以很好的解決上述方法存在的不足。通過該組函數(shù),可以將源數(shù)據(jù)庫(kù)中的內(nèi)容拷貝到另一個(gè)數(shù)據(jù)庫(kù),同時(shí)覆蓋目標(biāo)數(shù)據(jù)庫(kù)中的數(shù)據(jù)2013-12-12
保護(hù)你的Sqlite數(shù)據(jù)庫(kù)(SQLite數(shù)據(jù)庫(kù)安全秘籍)
相信使用PHP開發(fā)的人員一定不會(huì)對(duì)SQLite感到陌生了,PHP5已經(jīng)集成了這個(gè)輕量型的數(shù)據(jù)庫(kù)。并且很多虛擬主機(jī)無(wú)論是win還是*nux都支持它。2009-08-08

