时间:2021-02-12 08:51:28 | 栏目:mongodb | 点击:次
mongodb的数据插入速度是其一个亮点,同样的10000条数据,插入的速度要比Mysql和sqlserver都要快,当然这也是要看使用者怎么个使用法,你代码如果10000次写入使用10000次连接,那也是比不过其他数据库使用事务一次性提交的速度的。
同样,mongo也提供的一次性插入巨量数据的方法,因为mongodb没有事务这回事,所以在在C#驱动里,具体方法是InsertManyAsync()一次性插入多个文档。与之对应的是InsertOneAsync,这个是一次插入一个文档;
InsertManyAsync()这个方法带入的参数只要是实现了IEnumerable接口的类型就可以,所以可是list<>,这样的数据类型;
同样的10000次插入,两个方法时间差别很大。如图:
使用一次性插入多个文档方法,插入10000条耗时仅1.3秒,分成10000次插入,耗时19.9秒。区别大了个去。同样,前面我做过使用mysql插入10000条记录,要用4秒多,可见,这mongodb插入速度不是吹 的。
具体的代码如下,贴上:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; using System.Diagnostics; namespace sqltomongo { public class MongoHelp { private static IMongoClient client { get { if (null == _client) { _client = new MongoClient("mongodb://127.0.0.1:27017"); } return _client; } } public static IMongoDatabase database { get { _database = client.GetDatabase("HotelPersonInfo"); return _database; } set { _database = value; } } public static IMongoCollection<BsonDocument> collection { get { return _collection; } set { _collection = value; } } protected static IMongoClient _client; protected static IMongoDatabase _database; protected static IMongoCollection<BsonDocument> _collection; //测试效率,两个方法用时比较 public async static void TestMongo() { //自定义的对象 RoomInfo roomdata = new RoomInfo(); List<BsonDocument> docunemts = new List<BsonDocument>(); collection = database.GetCollection<BsonDocument>("HotelPersonInfo"); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 1; i < 10000; i++) { //mongo对用户自定义的对象扩展了tobasonDocument这个方法,可直接用 var roomdatadocument = new BsonDocument(roomdata.ToBsonDocument()); docunemts.Add(roomdatadocument); } //一次10000条 //这方法 查看api手册,只要实现了IEnumerable借口的类型就都行 await collection.InsertManyAsync(docunemts); sw.Stop(); TimeSpan ts2 =sw.Elapsed; Console.WriteLine("total is " + ts2.TotalMilliseconds); ///一次次插 10000次 Stopwatch sw2 = new Stopwatch(); sw2.Start(); for (int i = 1; i < 10000; i++) { var roomdatadocument = new BsonDocument(roomdata.ToBsonDocument()); await collection.InsertOneAsync(roomdatadocument); } sw2.Stop(); TimeSpan ts22 = sw2.Elapsed; Console.WriteLine("total is " + ts22.TotalMilliseconds); // await collection.InsertOneAsync(roomdatadocument); //collection = database.GetCollection<BsonDocument>("HotelPersonInfo"); // collection.InsertOneAsync(roomdatadocument); } } }
里面使用了一个自定义的对象:
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Bson; namespace sqltomongo { public class RoomInfo { public RoomInfo() { // id = "test"; Name = "nafd"; Moblie = "123456"; EMail = "dd@qq.com"; Tel = "010123"; Fax = "0755-001"; IdentityId = "616112323231"; RegisterType = "tid"; CardNo = "cardno"; Sex = "男"; Birthday = "1999"; Address = "china beijing"; ZipCode = "519000"; RegisterDate = "2015-03-03"; District2 = "District2"; District3 = "District3"; District4 = "District4"; } // public string id { get; set; } /// <summary> /// 名字 /// </summary> public string Name { get; set; } /// <summary> /// 手机号码 /// </summary> public string Moblie { get; set; } /// <summary> /// 邮箱 /// </summary> public string EMail {get;set;} /// <summary> /// 座机 /// </summary> public string Tel { get; set; } /// <summary> /// 传真 /// </summary> public string Fax { get; set; } /// <summary> /// 身份证 /// </summary> public string IdentityId { get; set; } /// <summary> /// 使用什么注册的 /// ID --身份证 (只需要id身份证的信息) /// </summary> public string RegisterType { get; set; } /// <summary> /// 会员卡号 /// </summary> public string CardNo { get; set; } /// <summary> /// 性别 /// </summary> public string Sex { get; set; } /// <summary> /// 生日 /// </summary> public string Birthday { get; set; } /// <summary> /// 地址 /// </summary> public string Address { get; set; } /// <summary> /// 邮编 /// </summary> public string ZipCode { get; set; } public string District2 { get; set; } public string District3 { get; set; } public string District4 { get; set; } /// <summary> /// 注册时间 /// </summary> public string RegisterDate { get; set; } } }
总结