MongoDB C# 低性能问题
我正在 win64 机器上测试 MongoDB 1.6.5 速度和 C#。我使用 Yahoo.geoplanet 作为加载州、县、城镇的来源,但我性能不太好。我目前有更多 5 秒的时间从这些源加载美国各州,将列表传递到本地主机中的网页。 仅使用 id 作为索引。有人可以建议执行方式吗?谢谢
class BsonPlaces
{
[BsonId]
public String Id { get; set; }
public String Iso { get; set; }
public String Name { get; set; }
public String Language { get; set; }
public String Place_Type { get; set; }
public String Parent_Id { get; set; }
}
public List<BsonPlaces> Get_States(string UseCountry)
{
using (var helper = BsonHelper.Create())
{
var query = Query.EQ("Place_Type", "State");
if (!String.IsNullOrEmpty(UseCountry))
query = Query.And(query, Query.EQ("Iso", UseCountry));
var cursor = helper.GeoPlanet.PlacesRepository.Db.Places
.FindAs<BsonPlaces>(query);
if (!String.IsNullOrEmpty(UseCountry))
cursor.SetSortOrder(SortBy.Ascending("Name"));
return cursor.ToList();
}
}
I'm testing MongoDB 1.6.5 speed and C# in win64 machine. I use Yahoo.geoplanet as source to load states, county, towns but i'm not very performant. I have currently more 5 sec to load the US states from these source passing a List to a web page in localhost.
Use only id as index. Can someone suggest way to perform. Thanks
class BsonPlaces
{
[BsonId]
public String Id { get; set; }
public String Iso { get; set; }
public String Name { get; set; }
public String Language { get; set; }
public String Place_Type { get; set; }
public String Parent_Id { get; set; }
}
public List<BsonPlaces> Get_States(string UseCountry)
{
using (var helper = BsonHelper.Create())
{
var query = Query.EQ("Place_Type", "State");
if (!String.IsNullOrEmpty(UseCountry))
query = Query.And(query, Query.EQ("Iso", UseCountry));
var cursor = helper.GeoPlanet.PlacesRepository.Db.Places
.FindAs<BsonPlaces>(query);
if (!String.IsNullOrEmpty(UseCountry))
cursor.SetSortOrder(SortBy.Ascending("Name"));
return cursor.ToList();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想问题不在 mongodb 中,加载可能很慢有两个原因:
为了提高速度,您可以:
1. 对查询返回的项目设置限制:
2. 为“Name”、“Iso”、“Place_Type”创建索引:
I suppose problem not in mongodb, loading can be slow for two reasons:
For speed improvements you can:
1.Set limit to items that will be returned by query:
2.Create indexes for 'Name', 'Iso', 'Place_Type':
C# 驱动程序可能存在很大的性能问题。在 shell 上进行 100k 次简单查询需要 3 秒,相同的查询(用官方 c# 驱动程序 1.5 的 c# linq 编写)需要 30 秒。 Profiler 告诉 C# 客户端的每个查询花费的时间不到 1 毫秒。所以我假设 c# 驱动程序做了很多不必要的事情,导致查询如此缓慢。
mongodb 2.0.7下,操作系统:windows 7,内存:16G。
c# driver probably have big performance problem. A simple query for 100k times on shell takes 3 seconds, same query (written in c# linq of official c# driver 1.5) takes 30 seconds. Profiler tells each query from c# client takes less than 1 ms. So I assume c# driver is doing a lot of unnecessary stuffs that makes the query so slow.
Under mongodb 2.0.7, OS: windows 7, Ram: 16G.
我从Yahoo下载了GeoPlanet Data,发现geoplanet_places_7.6.0.tsv文件有5,653,969行数据。
这意味着在没有索引的情况下,您将对超过 500 万个条目进行“全表扫描”以检索美国 50 个州。
当查询一个国家内的州时,以下索引可能是最有帮助的:
...EnsureIndex("Iso", "Place_Type");
不确定为什么您想要搜索所有“州”而不指定国家/地区,但您需要另一个索引。
有时,当涉及排序时,索引与排序顺序相匹配可能是有利的。例如,由于您要对“Name”进行排序,因此索引可以是:
...EnsureIndex("Iso", "Place_Type", "Name");
然而,由于只有 50 个状态,无论如何排序可能会非常快。
我怀疑您的性能缓慢是由 C# 驱动程序造成的,但如果添加索引后您仍然遇到性能问题,请告诉我们。
I downloaded the GeoPlanet Data from Yahoo and found that the geoplanet_places_7.6.0.tsv file has 5,653,969 lines of data.
That means that in the absence of an index you are doing a "full table scan" of over 5 million entries to retrieve the 50 US states.
When querying for states within a country, the following index would probably be the most helpful:
...EnsureIndex("Iso", "Place_Type");
Not sure why you would want to search for all "States" without specifying a country, but you would need another index for that.
Sometimes when a sort is involved it can be advantageous for the index to match the sort order. For example, since you are sorting on "Name" the index could be:
...EnsureIndex("Iso", "Place_Type", "Name");
However, with only 50 states the sort will probably be very fast anyway.
I doubt any of your slow performance is attributable to the C# driver, but if after adding the indexes you still have performance problems let us know.