2011-01-22 46 views
2
public IQueryable<T> GetRecords<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression, int from, int first) where T : class, new() 
    { 
     first = first == 0 ? 30 : first; 
     return _db.GetCollection<T>(collectionName).Linq().Where(expression).Skip(from).Take(first); 
    } 
    var x = GetRecords<Event>(p => true, 0, 12222); 
    string eventJson = new JavaScriptSerializer().Serialize(x); 

这个函数从mongoDB获取数据。mongoDB像SQL一样运行查询!

SqlDataReader dr = SqlHelper.ExecuteReader("Select Top(12222)* From NewsFeed"); 
    string eventJson = new JavaScriptSerializer().Serialize(dr); 

和这个来自SQL Server。

我试着测量每个人的执行时间,结果如下:
Mongo:172ms
SQL:185ms。
但据我所知,mongoDB应该比SQL快得多,对!!!

+5

为什么你觉得它应该更快? – 2011-01-22 19:33:00

+0

为什么我认为mongo应该比SQL更快!?! – Rawhi 2011-01-22 19:46:03

回答

7

MongoDB并不一定就是一个简单的查询那样的性能不佳的SQL服务器;当你有辅助数据时,速度优势就出现了。我不知道你的域名是什么样的,但一个常见的例子是博客。在SQL数据库中,您将拥有一个帖子表,一个评论表,一个作者表等。您的查询检索单个博客帖子显示所需的所有数据将包含一些连接或多个查询(两种方式都会影响性能有点)。在NoSQL数据库中,可能有一个表,Posts。一个没有连接的查询将撤回所有需要的数据。

Here's a nice blog post talking about the different approaches between a NoSQL type store and a relational store.