A solution with poor query performance when $ or and sort () are used in MongoDB

Source: Internet
Author: User

In frontArticleOnce mentioned, when using $ or and sort () in MongoDB, the query performance will be poor, see: http://www.cnblogs.com/xinghebuluo/archive/2011/12/01/2270590.html

This bug may be modified in MongoDB 2.5.w.

I also encountered this problem in my project. Later I thought about a solution to avoid this problem temporarily. Now I will share this solution and discuss it with you.

This solution is affected by mongosSource codeAs we all know, MongoDB is a distributed architecture. When we use mongos for query and sorting, mongos needs to send query requests to various shard and query results for each shard.

It is stored in a queue (the order has been arranged in the queue ). Here we assume there are two shard instances (multiple shard instances have the same principle). The query condition is {"age": 20}, and the sorting condition is {"time": 1 }, mongos implementation is as follows:

1. mongos first sends the query sorting command to two shard instances.

2. Two shard return results are the two queues after sorting ,.

3. When the client retrieves the record, mongos extracts the first element of the two queues and returns the record with a small time value to the client.

4. When the client retrieves the record again, repeat Step 3 and return the record with a small time value from the two queues to the client.

Inspired by mongos, in the case of or query and sort, the or query condition is decomposed into multiple queries, and then a query class is implemented, the list <dbobject q> is saved, and then the system sends multiple query sorting requests to mongos,

At this time, multiple cursor are obtained. The cursor is similar to the above queue, that is, multiple ordered queues are obtained. After a simple comparison, the records are returned to the client in sequence.

For example, if {"$ or": [{"Age": 20 },{ "name": "Li"}]} is queried, the sorting condition is {"time ": 1}, which can be divided into two queries: {"Age": 20 },{ "name": "Li"}. After the query is executed, two cursor queues are obtained, as follows :,

In this case, you can repeat the mongos step. When the client retrieves the record, it compares the first element in the queue (cursor), retrieves the record with the minimum time value, and returns it to the client.

The advantages of this solution are as follows:

1. indexes can be used, which is fast.

2. The encapsulated class can be used by multiple businesses.

Disadvantages:

1. Some records will be cached in each queue, which may cause a waste of traffic and memory.

 

The above is my overall idea for this solution. You are welcome to discuss it.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.