Sunday, 15 January 2012

Range queries on a large collection in MongoDB -



Range queries on a large collection in MongoDB -

i have document collection around 40 1000000 documents(~10gb). documents in collection small(~1000 bytes). main fields of involvement follows:

start_x integer end_x integer

i have query homecoming row given value of x. value of x there can ever 1 matching row in collection. using next selector purpose:

"start_x"=>{"$lte"=>1258}, "end_x"=>{"$gte"=> 1258}

i not getting expected performance query. started compound index (start_x =1 , end_x = 1). query plan showed around 400k nscanned

{ "cursor"=>"btreecursor start_x_1_end_x_1", "nscanned"=>417801, "nscannedobjects"=>1, "n"=>1, "millis"=>3548, "nyields"=>0, "nchunkskips"=>0, "ismultikey"=>false, "indexonly"=>false }

subsequently, added stand-alone index on start_x , end_x fields. query plan didn't show much improvement.

why indexonly not true though have compound index , fields used in query covered index?

is there way optimize query?

i ended using indexed lookup on end_x field address issue.

dropped indexes on collection added asc index on end_x field.

queried first matching row top bound equal or above given value

row = model.where(:end_x.gte => 1258).asc(:end_x).limit(1).first

checked ensure row returned indeed matching range

row = (row.present? , 1258.between?(row.start_x, row.end_x)) ? row : nil

mongodb

No comments:

Post a Comment