php - mongoDB index strategy -
i have collection called post. have mapping system ensures each document has these fields:
- id (int)
- target (string)
- type (string)
- user_id
- client_id
- updated (string, 11 int timestamp)
- created (string, 11 int timestamp)
- enabled (bool)
this collection accessed output in api schema.
so typical requests might be:
/post?type=image&user_id=2 /post?updated=35234423&order_by=client_id /post?enabled=true&order_by=id
there no 100% guarantee fields make find or sort field.
recently when table reached 8gb of data, started getting error:
"localhost:27017: data sort() no index. add index or specify smaller limit"
i have looked @ documentation mongo index , found difficult understand whether works in same way mysql index.
some threads found on indexing: mongodb - data sort() no index error seem suggest using specific sort fields ensure index hit. cannot when alot of filtering , sorting optional.
could suggest firm solution in terms of whether should index fields on table?
thanks feedback guys, i've started building auto index function:
public function get() { $indices['post'] = array( 'fields' => array( 'id' => array('unique' => true, 'dropdups' => true, 'background' => true), 'client_id' => array('dropdups' => true, 'background' => true), 'image_id' => array('dropdups' => true, 'background' => true), 'user_id' => array('dropdups' => true, 'background' => true), 'publish_target' => array('dropdups' => true, 'background' => true), 'type' => array('dropdups' => true, 'background' => true), 'status' => array('dropdups' => true, 'background' => true), 'text' => array('background' => true) ) ); foreach ($indices $key => $index) { /* set collection */ $collection = $this->mongodb->{$key}; /* delete indexes */ $collection->deleteindexes(); /* loop fields , add index */ foreach ($index['fields'] $subkey => $data) { $collection->ensureindex($subkey, array_merge($data, array('name' => $subkey))); } } /* return list */ return $indices; }
unfortunately cannot think of solution such dynamic nature indexes however, jira https://jira.mongodb.org/browse/server-3071 you.
i suggest watch jira ticket.
Comments
Post a Comment