MongoDB C# low performance issue

Question!

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();
   }
}


Answers

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.



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.

By : liang


I suppose problem not in mongodb, loading can be slow for two reasons:

  1. You trying to load big count of 'BsonPlaces'(20000 for example or even more).
  2. Some another code on page take much time.

For speed improvements you can:

1.Set limit to items that will be returned by query:

 cursor.SetLimit(100); 

2.Create indexes for 'Name', 'Iso', 'Place_Type':

helper.GeoPlanet.PlacesRepository.Db.Places.EnsureIndex("Name");


This video can help you solving your question :)
By: admin