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

Source: Internet
Author: User
When $ or and sort () are used in MongoDB, a solution with poor query performance is mentioned in the previous article. when $ or and sort () are used in MongoDB, query performance may be poor. for details, refer to mongodb's plan. this bug may be modified in version 2.5.w. I encountered this problem in my project, and then I thought of a solution.

When $ or and sort () are used in MongoDB, a solution with poor query performance is mentioned in the previous article. when $ or and sort () are used in MongoDB, query performance may be poor. for details, refer to mongodb's plan. this bug may be modified in version 2.5.w. I encountered this problem in my project, and then I thought of a solution.

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

As mentioned in the previous article, when $ or and sort () are used in MongoDB, the query performance will be poor. for details, see:

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 inspired by mongos source code. as we all know that mongodb is a distributed architecture, when we use mongos to query and sort data, mongos needs to send query requests to various shard instances, 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, which stores the list , Website space, and then initiate 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}, Hong Kong server, can be divided into two queries: {"age": 20 },{ "name": "li"}. after the query is executed, two cursor are obtained, there are two queues :,

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. after the encapsulation class, the server space 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.

Posted on

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.