[C#] 一個輕量化的Database - LiteDB 製作索引(index) ,百萬資料測試查詢篇

2024-02-01


今天迎來 LiteDB 最後大的測試,就是建立索引,為了測試資料,我寫入了一百萬筆,所以花了一點時間

為了要比較前後,所以花了一點時間做範例+結果


1.  因為為了讓測試有感覺,我寫了一百萬筆資料進去

using (var db = new LiteDatabase("filename=" + AppDomain.CurrentDomain.BaseDirectory + "sample.db; journal=false;connection=shared")) { var userColleciton = db.GetCollection<Friend>("FRIENDS"); for (var i = 1; i <= 1_000_000; i++) { { var u = new Friend { _id = "friend" + i, CreateDate = DateTime.Now, IsActive = true, Name = "FRIEND" + i, Salary = i }; u.Phones.Add(new Friend.Phone { Num = "09" + i, Type = "HOME" }); userColleciton.Upsert(u); if (i % 10_000 == 0) { Console.WriteLine("Already upsert:" + i); } } } }


2.然後直接查詢,看看效能如何,為了避免開檔或是其他影響因素,我用迴圈跑了十次,這邊案例是我查詢 Name 這屬性 包含有 99999 的字樣

for (var i = 1; i <= 10; i++) { using (var db = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + "sample.db")) { var userColleciton = db.GetCollection<Friend>("FRIENDS"); sw.Restart(); var data1 = db.Execute("select $ from FRIENDS where Name LIKE '%99999%'").ToList(); if (data1 != null) { //foreach (var d in data1) //{ // Console.WriteLine(JsonConvert.SerializeObject(BsonMapper.Global.Deserialize<Friend>(d))); //} sw.Stop(); Console.WriteLine("Cost time:" + sw.Elapsed); Console.WriteLine("Count:" + data1.Count()); } } } //Result /* Hello, World! Cost time:00:00:05.9118816 Count:19 Cost time:00:00:04.4788509 Count:19 Cost time:00:00:04.2698380 Count:19 Cost time:00:00:04.5681260 Count:19 Cost time:00:00:04.2358789 Count:19 Cost time:00:00:04.2418008 Count:19 Cost time:00:00:04.4060890 Count:19 Cost time:00:00:04.3499493 Count:19 Cost time:00:00:04.2901083 Count:19 Cost time:00:00:04.2594764 Count:19


看一下結果大概 4-6  秒左右


3.建立索引

using (var db = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + "sample.db")) { var userColleciton = db.GetCollection<Friend>("FRIENDS"); var result = userColleciton.EnsureIndex(x => x.Phones); Console.WriteLine("Make Index Result:" + result); } //Result /* Make Index Result:True Cost time:00:00:13.3313368 */


4.同樣是跟第二點一樣的 code 就不贅述了直接給跑完後的結果 大概 兩秒鐘就可以查到,速度算是提升很多

Cost time:00:00:02.5046661 Count:19 Cost time:00:00:01.3138527 Count:19 Cost time:00:00:01.2688938 Count:19 Cost time:00:00:01.3026238 Count:19 Cost time:00:00:01.3637566 Count:19 Cost time:00:00:01.2479089 Count:19 Cost time:00:00:01.2522639 Count:19 Cost time:00:00:01.2349144 Count:19 Cost time:00:00:01.3638718 Count:19 Cost time:00:00:01.2699475 Count:19


今天測試大概到這邊,畢竟就是分享如何建立索引,跟自己測試一下速度



當麻許的碎念筆記 2014 | Donma Hsu Design.