Mongodb Mongo数据库的性能问题
今天,我测试了Mongo数据库,但遇到了性能问题。 在插入1.800.00之后,我尝试对所有值求和,但它太小了。 然后我在MSSQL中尝试了同样的方法,并使用了0s 你能告诉我我做错了什么吗? 这是芒果限制吗Mongodb Mongo数据库的性能问题,mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,今天,我测试了Mongo数据库,但遇到了性能问题。 在插入1.800.00之后,我尝试对所有值求和,但它太小了。 然后我在MSSQL中尝试了同样的方法,并使用了0s 你能告诉我我做错了什么吗? 这是芒果限制吗 static void Main(string[] args) { //Create a default mongo object. This handles our connections to the database. //By de
static void Main(string[] args)
{
//Create a default mongo object. This handles our connections to the database.
//By default, this will connect to localhost, port 27017 which we already have running from earlier.
var connStr = new MongoConnectionStringBuilder();
connStr.ConnectTimeout = new TimeSpan(1, 0, 0);
connStr.SocketTimeout = new TimeSpan(1, 0, 0);
connStr.Server = new MongoServerAddress("localhost");
var mongo = MongoServer.Create(connStr);
//Get the blog database. If it doesn't exist, that's ok because MongoDB will create it
//for us when we first use it. Awesome!!!
var db = mongo.GetDatabase("blog");
var sw = new Stopwatch();
sw.Start();
//Get the Post collection. By default, we'll use the name of the class as the collection name. Again,
//if it doesn't exist, MongoDB will create it when we first use it.
var collection = db.GetCollection<Post>("Post");
Console.WriteLine(collection.Count());
sw.Stop();
Console.WriteLine("Time: " + sw.Elapsed.TotalSeconds);
sw.Reset();
sw.Start();
var starting = collection.Count();
var batch = new List<Post>();
for (int i = starting; i < starting + 200000; i++)
{
var post = new Post
{
Body = i.ToString(),
Title = "title " + i.ToString(),
CharCount = i.ToString().Length,
CreatedBy = "user",
ModifiedBy = "user",
ModifiedOn = DateTime.Now,
CreatedOn = DateTime.Now
};
//collection.Insert<Post>(post);
batch.Add(post);
}
collection.InsertBatch(batch);
Console.WriteLine(collection.Count());
sw.Stop();
Console.WriteLine("Time to insert 100.000 records: " + sw.Elapsed.TotalSeconds);
//var q = collection.Find(Query.LT("Body", "30000")).ToList();
//Console.WriteLine(q.Count());
sw.Reset();
sw.Start();
var q2 = collection.AsQueryable<Post>();
var sum = q2.Sum(p => p.CharCount);
Console.WriteLine(sum);
sw.Stop();
Console.WriteLine("Time to sum '" + q2.Count() + "' Post records: " + sw.Elapsed.TotalSeconds); //PROBLEM: take 57 to SUM 1.000.000 records
static void Main(字符串[]args)
{
//创建一个默认的mongo对象。这将处理我们与数据库的连接。
//默认情况下,这将连接到localhost端口27017,我们之前已经从该端口运行了。
var connStr=新的MongoConnectionStringBuilder();
connStr.ConnectTimeout=新的时间跨度(1,0,0);
connStr.SocketTimeout=新的时间跨度(1,0,0);
connStr.Server=新的MongoServer地址(“localhost”);
var mongo=MongoServer.Create(connStr);
//获取博客数据库。如果它不存在,那没关系,因为MongoDB将创建它
//当我们第一次使用它时,它是为我们准备的。太棒了!!!
var db=mongo.GetDatabase(“blog”);
var sw=新秒表();
sw.Start();
//获取Post集合。默认情况下,我们将使用类的名称作为集合名称。再次,
//如果它不存在,MongoDB将在我们第一次使用它时创建它。
var collection=db.GetCollection(“Post”);
Console.WriteLine(collection.Count());
sw.Stop();
Console.WriteLine(“时间:+sw.passed.TotalSeconds”);
sw.Reset();
sw.Start();
var start=collection.Count();
var batch=新列表();
对于(int i=启动;i<启动+200000;i++)
{
var post=新职位
{
Body=i.ToString(),
Title=“Title”+i.ToString(),
CharCount=i.ToString().Length,
CreatedBy=“user”,
ModifiedBy=“user”,
ModifiedOn=DateTime。现在,
CreatedOn=DateTime.Now
};
//收款。插入(邮寄);
批量添加(post);
}
集合。插入批(批);
Console.WriteLine(collection.Count());
sw.Stop();
Console.WriteLine(“插入100.000条记录的时间:“+sw.已用时间.TotalSeconds”);
//var q=collection.Find(Query.LT(“Body”,“30000”)).ToList();
//Console.WriteLine(q.Count());
sw.Reset();
sw.Start();
var q2=collection.AsQueryable();
var sum=q2.sum(p=>p.CharCount);
控制台写入线(总和);
sw.Stop();
Console.WriteLine(“求和时间””+q2.Count()+“”Post记录:“+sw.eassed.TotalSeconds”);//问题:求和1.000.000条记录需要57秒
}
}下一行中的性能问题:
var q2 = collection.AsQueryable<Post>();
var q2=collection.AsQueryable();
在上面的行中,由于驱动程序不支持linq,您将所有POST从posts集合加载到内存中。在MSSQL中,由于linq和计算将通过数据库,所以只需不到一秒钟的时间。在这里,我想几乎所有的57秒都需要将数据加载到内存中
在mongodb中,为了获得最佳性能,您需要创建额外的字段(反规范化数据)并尽可能计算任何总和、计数器等。如果不可能,您需要使用或使用可用的函数,例如(非常适合您的求和计算示例)